mirror of
https://github.com/Alfresco/alfresco-ng2-components.git
synced 2026-09-09 18:03:21 +00:00
[AAE-15836] Include email in full name pipe if required
This commit is contained in:
@@ -9,6 +9,8 @@ Last reviewed: 2018-11-12
|
||||
|
||||
Joins the first and last name properties from a [`UserProcessModel`](../../core/models/user-process.model.md) object into a single string.
|
||||
|
||||
Optionally it can include the email of the users (if available).
|
||||
|
||||
## Basic Usage
|
||||
|
||||
<!-- {% raw %} -->
|
||||
@@ -21,10 +23,24 @@ Joins the first and last name properties from a [`UserProcessModel`](../../core/
|
||||
|
||||
<!-- {% endraw %} -->
|
||||
|
||||
## Include the email of the user
|
||||
|
||||
<!-- {% raw %} -->
|
||||
|
||||
```HTML
|
||||
<div>
|
||||
Project Leader: {{ user | fullName: true }}
|
||||
</div>
|
||||
```
|
||||
|
||||
<!-- {% endraw %} -->
|
||||
|
||||
## Details
|
||||
|
||||
The pipe offers a convenient way to extract the name from a [User process model](../models/user-process.model.md) object.
|
||||
|
||||
If you want to include also the email of the user (when available) by default for your whole application, then you need to provide the injection token `ADF_FULL_NAME_PIPE_INCLUDE_EMAIL` in your angular application module with `true` value.
|
||||
|
||||
## See also
|
||||
|
||||
- [User initial pipe](user-initial.pipe.md)
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
/*!
|
||||
* @license
|
||||
* Copyright © 2005-2023 Hyland Software, Inc. and its affiliates. All rights reserved.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
/* eslint-disable rxjs/no-subject-value */
|
||||
/* eslint-disable @typescript-eslint/naming-convention */
|
||||
|
||||
import { InjectionToken } from '@angular/core';
|
||||
|
||||
export const ADF_FULL_NAME_PIPE_INCLUDE_EMAIL = new InjectionToken<boolean>('ADF_FULL_NAME_PIPE_INCLUDE_EMAIL');
|
||||
@@ -16,42 +16,168 @@
|
||||
*/
|
||||
|
||||
import { FullNamePipe } from './full-name.pipe';
|
||||
import { UserLike } from './user-like.interface';
|
||||
|
||||
interface TestCases {
|
||||
[key: string]: {
|
||||
title: string;
|
||||
includeEmailToken: boolean | undefined;
|
||||
includeEmailParameter: boolean | undefined;
|
||||
testCases: TestCase[];
|
||||
};
|
||||
};
|
||||
|
||||
interface TestCase {
|
||||
title: string;
|
||||
user: UserLike;
|
||||
result: string;
|
||||
};
|
||||
|
||||
describe('FullNamePipe', () => {
|
||||
|
||||
let pipe: FullNamePipe;
|
||||
|
||||
beforeEach(() => {
|
||||
pipe = new FullNamePipe();
|
||||
});
|
||||
const cosntBaseTestCases: TestCase[] = [
|
||||
{
|
||||
title: 'should return empty string when there is no name',
|
||||
user: { firstName: '', lastName: '', username: '', email: '' },
|
||||
result: ''
|
||||
},
|
||||
{
|
||||
title: 'should return only firstName as fullName when there is no lastName',
|
||||
user: { firstName: 'Abc', lastName: '', username: '', email: '' },
|
||||
result: 'Abc'
|
||||
},
|
||||
{
|
||||
title: 'should return only lastName as fullName when there is no firstName',
|
||||
user: { firstName: '', lastName: 'Xyz', username: '', email: '' },
|
||||
result: 'Xyz'
|
||||
},
|
||||
{
|
||||
title: 'should return fullName when firstName and lastName are available',
|
||||
user: { firstName: 'Abc', lastName: 'Xyz', username: '', email: '' },
|
||||
result: 'Abc Xyz'
|
||||
},
|
||||
{
|
||||
title: 'should return username when firstName and lastName are not available',
|
||||
user: { firstName: '', lastName: '', username: 'username', email: '' },
|
||||
result: 'username'
|
||||
},
|
||||
{
|
||||
title: 'should return user email when firstName, lastName and username are not available',
|
||||
user: { firstName: '', lastName: '', username: '', email: 'abcXyz@gmail.com' },
|
||||
result: 'abcXyz@gmail.com'
|
||||
}
|
||||
];
|
||||
|
||||
it('should return empty string when there is no name', () => {
|
||||
const user = {};
|
||||
expect(pipe.transform(user)).toBe('');
|
||||
});
|
||||
function patchEmailAddress(testCase: TestCase): TestCase {
|
||||
return {
|
||||
...testCase,
|
||||
user: { ...testCase.user, email: 'abcXyz@gmail.com' }
|
||||
};
|
||||
}
|
||||
|
||||
it('should return only firstName as fullName when there is no lastName ', () => {
|
||||
const user = {firstName : 'Abc'};
|
||||
expect(pipe.transform(user)).toBe('Abc');
|
||||
});
|
||||
function patchEmailResult(testCase: TestCase): TestCase {
|
||||
return {
|
||||
...testCase,
|
||||
result: testCase.result + ' <abcXyz@gmail.com>'
|
||||
};
|
||||
}
|
||||
|
||||
it('should return only lastName as fullName when there is no firstName ', () => {
|
||||
const user = {lastName : 'Xyz'};
|
||||
expect(pipe.transform(user)).toBe('Xyz');
|
||||
});
|
||||
function patchEmail(testCase: TestCase): TestCase {
|
||||
return patchEmailResult(patchEmailAddress(testCase));
|
||||
}
|
||||
|
||||
it('should return fullName when firstName and lastName are available', () => {
|
||||
const user = {firstName : 'Abc', lastName : 'Xyz'};
|
||||
expect(pipe.transform(user)).toBe('Abc Xyz');
|
||||
});
|
||||
function getTestCasesWithEmailPatched(): TestCase[] {
|
||||
return cosntBaseTestCases.slice(1, cosntBaseTestCases.length - 1).map(testCase => patchEmail(testCase)).concat([{
|
||||
title: 'should return user email when firstName, lastName and username are not available',
|
||||
user: { firstName: '', lastName: '', username: '', email: 'abcXyz@gmail.com' },
|
||||
result: 'abcXyz@gmail.com'
|
||||
}]);
|
||||
}
|
||||
|
||||
it('should return username when firstName and lastName are not available', () => {
|
||||
const user = {firstName : '', lastName : '', username: 'username'};
|
||||
expect(pipe.transform(user)).toBe('username');
|
||||
});
|
||||
const testCases: TestCases = {
|
||||
undefinedIncludeEmailTokenUndefinedIncludeEmailParameter: {
|
||||
title: 'and include email token is undefined and include email is undefined',
|
||||
includeEmailToken: undefined,
|
||||
includeEmailParameter: undefined,
|
||||
testCases: cosntBaseTestCases
|
||||
},
|
||||
undefinedIncludeEmailTokenFalseIncludeEmailParameter: {
|
||||
title: 'and include email token is undefined and include email is false',
|
||||
includeEmailToken: undefined,
|
||||
includeEmailParameter: false,
|
||||
testCases: cosntBaseTestCases
|
||||
},
|
||||
undefinedIncludeEmailTokenFTrueIncludeEmailParameter: {
|
||||
title: 'and include email token is undefined and include email is true',
|
||||
includeEmailToken: undefined,
|
||||
includeEmailParameter: true,
|
||||
testCases: getTestCasesWithEmailPatched()
|
||||
},
|
||||
falseIncludeEmailTokenUndefinedIncludeEmailParameter: {
|
||||
title: 'and include email token is false and include email is undefined',
|
||||
includeEmailToken: false,
|
||||
includeEmailParameter: undefined,
|
||||
testCases: cosntBaseTestCases
|
||||
},
|
||||
falseIncludeEmailTokenFalseIncludeEmailParameter: {
|
||||
title: 'and include email token is false and include email is false',
|
||||
includeEmailToken: false,
|
||||
includeEmailParameter: false,
|
||||
testCases: cosntBaseTestCases
|
||||
},
|
||||
falseIncludeEmailTokenTrueIncludeEmailParameter: {
|
||||
title: 'and include email token is false and include email is true',
|
||||
includeEmailToken: false,
|
||||
includeEmailParameter: true,
|
||||
testCases: getTestCasesWithEmailPatched()
|
||||
},
|
||||
trueIncludeEmailTokennUndefinedIncludeEmailParameterButEmailAddressNotPresent: {
|
||||
title: 'and include email token is true and include email is undefined but email is not provided',
|
||||
includeEmailToken: true,
|
||||
includeEmailParameter: undefined,
|
||||
testCases: cosntBaseTestCases.slice(0, cosntBaseTestCases.length - 1)
|
||||
},
|
||||
trueIncludeEmailTokennFalseIncludeEmailParameterButEmailAddressNotPresent: {
|
||||
title: 'and include email token is true and include email is false but email is not provided',
|
||||
includeEmailToken: true,
|
||||
includeEmailParameter: false,
|
||||
testCases: cosntBaseTestCases.slice(0, cosntBaseTestCases.length - 1)
|
||||
},
|
||||
trueIncludeEmailTokennTrueIncludeEmailParameterButEmailAddressNotPresent: {
|
||||
title: 'and include email token is true and include email is true but email is not provided',
|
||||
includeEmailToken: true,
|
||||
includeEmailParameter: true,
|
||||
testCases: cosntBaseTestCases.slice(0, cosntBaseTestCases.length - 1)
|
||||
},
|
||||
trueIncludeEmailTokenUndefinedIncludeEmailParameterAndEmailAddressPresent: {
|
||||
title: 'and include email token is true and include email is undefined and email is provided',
|
||||
includeEmailToken: true,
|
||||
includeEmailParameter: undefined,
|
||||
testCases: getTestCasesWithEmailPatched()
|
||||
},
|
||||
trueIncludeEmailTokenFalseIncludeEmailParameterAndEmailAddressPresent: {
|
||||
title: 'and include email token is true and include email is false and email is provided',
|
||||
includeEmailToken: true,
|
||||
includeEmailParameter: false,
|
||||
testCases: cosntBaseTestCases.slice(1, cosntBaseTestCases.length - 1).map(testCase => patchEmailAddress(testCase))
|
||||
},
|
||||
trueIncludeEmailTokenTrueIncludeEmailParameterAndEmailAddressPresent: {
|
||||
title: 'and include email token is true and include email is true and email is provided',
|
||||
includeEmailToken: true,
|
||||
includeEmailParameter: true,
|
||||
testCases: getTestCasesWithEmailPatched()
|
||||
}
|
||||
};
|
||||
|
||||
it('should return user eamil when firstName, lastName and username are not available', () => {
|
||||
const user = {firstName : '', lastName : '', username: '', email: 'abcXyz@gmail.com'};
|
||||
expect(pipe.transform(user)).toBe('abcXyz@gmail.com');
|
||||
Object.keys(testCases).forEach(block => {
|
||||
const testCasesToExecute = testCases[block].testCases;
|
||||
testCasesToExecute.forEach(testCase => {
|
||||
it(`${testCase.title} ${testCases[block].title}`, () => {
|
||||
pipe = new FullNamePipe(testCases[block].includeEmailToken);
|
||||
expect(pipe.transform(testCase.user, testCases[block].includeEmailParameter)).toBe(testCase.result);
|
||||
});
|
||||
});
|
||||
});
|
||||
});
|
||||
|
||||
@@ -15,26 +15,52 @@
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
import { Pipe, PipeTransform } from '@angular/core';
|
||||
import { Inject, Optional, Pipe, PipeTransform } from '@angular/core';
|
||||
import { UserLike } from './user-like.interface';
|
||||
import { ADF_FULL_NAME_PIPE_INCLUDE_EMAIL } from './full-name-email-required.token';
|
||||
|
||||
@Pipe({ name: 'fullName' })
|
||||
export class FullNamePipe implements PipeTransform {
|
||||
|
||||
transform(user: UserLike): string {
|
||||
return this.buildFullName(user) ? this.buildFullName(user) : this.buildFromUsernameOrEmail(user);
|
||||
constructor(@Optional() @Inject(ADF_FULL_NAME_PIPE_INCLUDE_EMAIL) private includeEmail = false) {
|
||||
}
|
||||
|
||||
buildFullName(user: UserLike): string {
|
||||
transform(user: UserLike, includeEmail: boolean | undefined): string {
|
||||
return this.buildFullName(user, includeEmail) ? this.buildFullName(user, includeEmail) : this.buildFromUsernameOrEmail(user, includeEmail);
|
||||
}
|
||||
|
||||
private includeEmailInFullName(includeEmail: boolean | undefined) {
|
||||
return includeEmail === undefined ? this.includeEmail : includeEmail;
|
||||
}
|
||||
|
||||
private buildFullName(user: UserLike, includeEmail: boolean | undefined): string {
|
||||
const fullName: string[] = [];
|
||||
let hasName = false;
|
||||
|
||||
fullName.push(user?.firstName);
|
||||
fullName.push(user?.lastName);
|
||||
if (user?.firstName) {
|
||||
hasName = true;
|
||||
fullName.push(user?.firstName);
|
||||
}
|
||||
|
||||
return fullName.join(' ').trim();
|
||||
if (user?.lastName) {
|
||||
hasName = true;
|
||||
fullName.push(user?.lastName);
|
||||
}
|
||||
|
||||
if (this.includeEmailInFullName(includeEmail) && hasName && user?.email) {
|
||||
fullName.push(`<${user.email}>`);
|
||||
}
|
||||
|
||||
return fullName.join(' ');
|
||||
}
|
||||
|
||||
buildFromUsernameOrEmail(user: UserLike): string {
|
||||
return (user?.username || user?.email) ?? '' ;
|
||||
private buildFromUsernameOrEmail(user: UserLike, includeEmail: boolean | undefined): string {
|
||||
let fullName = (user?.username || user?.email) ?? '';
|
||||
|
||||
if (this.includeEmailInFullName(includeEmail) && user.username && user.email) {
|
||||
fullName += ` <${user.email}>`;
|
||||
}
|
||||
|
||||
return fullName;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -20,7 +20,7 @@ import { TestBed } from '@angular/core/testing';
|
||||
import { CoreTestingModule } from '../testing/core.testing.module';
|
||||
import { TranslateModule } from '@ngx-translate/core';
|
||||
|
||||
describe('FullNamePipe', () => {
|
||||
describe('MultiValuePipe', () => {
|
||||
|
||||
let pipe: MultiValuePipe;
|
||||
|
||||
|
||||
@@ -32,3 +32,4 @@ export * from './moment-date.pipe';
|
||||
export * from './moment-datetime.pipe';
|
||||
export * from './filter-string.pipe';
|
||||
export * from './filter-out-every-object-by-prop.pipe';
|
||||
export * from './full-name-email-required.token';
|
||||
|
||||
Reference in New Issue
Block a user