mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
add test, update code
This commit is contained in:
parent
e802bac152
commit
675b3868a1
@ -2,15 +2,13 @@
|
||||
import { User } from '@entity/User'
|
||||
|
||||
import { convertGradidoLanguageToHumhub } from '@/apis/humhub/convertLanguage'
|
||||
import { PublishNameLogic } from '@/data/PublishName.logic'
|
||||
import { PublishNameType } from '@/graphql/enum/PublishNameType'
|
||||
|
||||
export class Account {
|
||||
public constructor(user: User) {
|
||||
if (user.alias && user.alias.length > 2) {
|
||||
this.username = user.alias
|
||||
} else {
|
||||
this.username = user.gradidoID
|
||||
}
|
||||
|
||||
const publishNameLogic = new PublishNameLogic(user)
|
||||
this.username = publishNameLogic.getUsername(user.humhubPublishName as PublishNameType)
|
||||
this.email = user.emailContact.email
|
||||
this.language = convertGradidoLanguageToHumhub(user.language)
|
||||
this.status = 1
|
||||
|
||||
@ -10,7 +10,10 @@ export class Profile {
|
||||
const publishNameLogic = new PublishNameLogic(user)
|
||||
this.firstname = publishNameLogic.getFirstName(user.humhubPublishName as PublishNameType)
|
||||
this.lastname = publishNameLogic.getLastName(user.humhubPublishName as PublishNameType)
|
||||
this.gradido_address = CONFIG.COMMUNITY_NAME + '/' + user.gradidoID
|
||||
|
||||
this.gradido_address = `${CONFIG.COMMUNITY_NAME}/${
|
||||
publishNameLogic.hasAlias() ? user.alias : user.gradidoID
|
||||
}`
|
||||
}
|
||||
|
||||
firstname: string
|
||||
|
||||
23
backend/src/data/PublishName.logic.test.ts
Normal file
23
backend/src/data/PublishName.logic.test.ts
Normal file
@ -0,0 +1,23 @@
|
||||
import { User } from '@entity/User'
|
||||
|
||||
import { PublishNameType } from '@/graphql/enum/PublishNameType'
|
||||
|
||||
import { PublishNameLogic } from './PublishName.logic'
|
||||
|
||||
describe('test publish name logic', () => {
|
||||
describe('test username', () => {
|
||||
it('alias or initials with alias set', () => {
|
||||
const user = new User()
|
||||
user.alias = 'alias'
|
||||
const logic = new PublishNameLogic(user)
|
||||
expect(logic.getUsername(PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS)).toBe(user.alias)
|
||||
})
|
||||
it('alias or initials with empty alias', () => {
|
||||
const user = new User()
|
||||
user.firstName = 'John'
|
||||
user.lastName = 'Smith'
|
||||
const logic = new PublishNameLogic(user)
|
||||
expect(logic.getUsername(PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS)).toBe('JoSm')
|
||||
})
|
||||
})
|
||||
})
|
||||
@ -7,59 +7,68 @@ export class PublishNameLogic {
|
||||
constructor(private user: User) {}
|
||||
|
||||
private firstUpperCaseSecondLowerCase(substring: string) {
|
||||
if (substring.length !== 2) {
|
||||
throw new LogError("substring hasn't expected length of 2")
|
||||
if (!substring || substring.length < 2) {
|
||||
throw new LogError('substring is to small, it need at least two characters', { substring })
|
||||
}
|
||||
return substring.charAt(0).toUpperCase() + substring.charAt(1).toLocaleLowerCase()
|
||||
}
|
||||
|
||||
public hasAlias(): boolean {
|
||||
if (this.user.alias && this.user.alias.length >= 3) {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
/**
|
||||
* get first name based on publishNameType: PublishNameType value
|
||||
* @param publishNameType
|
||||
* @returns user.firstName for PUBLISH_NAME_FIRST, PUBLISH_NAME_FIRST_INITIAL or PUBLISH_NAME_FULL
|
||||
* first initial from user.firstName for PUBLISH_NAME_INITIALS or PUBLISH_NAME_INITIAL_LAST
|
||||
*/
|
||||
public getFirstName(publishNameType: PublishNameType): string {
|
||||
if (
|
||||
[
|
||||
PublishNameType.PUBLISH_NAME_FIRST,
|
||||
PublishNameType.PUBLISH_NAME_FIRST_INITIAL,
|
||||
PublishNameType.PUBLISH_NAME_FULL,
|
||||
].includes(publishNameType)
|
||||
) {
|
||||
return this.user.firstName
|
||||
}
|
||||
if (PublishNameType.PUBLISH_NAME_INITIALS === publishNameType) {
|
||||
return this.firstUpperCaseSecondLowerCase(this.user.firstName.substring(0, 2))
|
||||
}
|
||||
if (PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS === publishNameType) {
|
||||
if (this.user.alias) {
|
||||
return this.user.alias
|
||||
} else {
|
||||
return this.firstUpperCaseSecondLowerCase(this.user.firstName.substring(0, 2))
|
||||
}
|
||||
}
|
||||
return ''
|
||||
return [
|
||||
PublishNameType.PUBLISH_NAME_FIRST,
|
||||
PublishNameType.PUBLISH_NAME_FIRST_INITIAL,
|
||||
PublishNameType.PUBLISH_NAME_FULL,
|
||||
].includes(publishNameType)
|
||||
? this.user.firstName
|
||||
: ''
|
||||
}
|
||||
|
||||
/**
|
||||
* get last name based on publishNameType: GmsPublishNameType value
|
||||
* @param publishNameType
|
||||
* @returns user.lastName for PUBLISH_NAME_LAST, PUBLISH_NAME_INITIAL_LAST, PUBLISH_NAME_FULL
|
||||
* first initial from user.lastName for PUBLISH_NAME_FIRST_INITIAL, PUBLISH_NAME_INITIALS
|
||||
* @returns user.lastName for PUBLISH_NAME_LAST, PUBLISH_NAME_FULL
|
||||
* first initial from user.lastName for PUBLISH_NAME_FIRST_INITIAL
|
||||
*/
|
||||
public getLastName(publishNameType: PublishNameType): string {
|
||||
if (PublishNameType.PUBLISH_NAME_FULL === publishNameType) {
|
||||
return this.user.lastName
|
||||
} else if (PublishNameType.PUBLISH_NAME_FIRST_INITIAL === publishNameType) {
|
||||
return this.user.lastName.substring(0, 1)
|
||||
} else if (
|
||||
(PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS === publishNameType && !this.user.alias) ||
|
||||
PublishNameType.PUBLISH_NAME_INITIALS === publishNameType
|
||||
) {
|
||||
return this.firstUpperCaseSecondLowerCase(this.user.lastName.substring(0, 2))
|
||||
}
|
||||
return publishNameType === PublishNameType.PUBLISH_NAME_FULL
|
||||
? this.user.lastName
|
||||
: publishNameType === PublishNameType.PUBLISH_NAME_FIRST_INITIAL
|
||||
? this.user.lastName.charAt(0)
|
||||
: ''
|
||||
}
|
||||
|
||||
return ''
|
||||
/**
|
||||
* get username from user.alias for PUBLISH_NAME_ALIAS_OR_INITALS and if user has alias
|
||||
* get first name first two characters and last name first two characters for PUBLISH_NAME_ALIAS_OR_INITALS
|
||||
* if no alias or PUBLISH_NAME_INITIALS
|
||||
* @param publishNameType
|
||||
* @returns user.alias for publishNameType = PUBLISH_NAME_ALIAS_OR_INITALS and user has alias
|
||||
* else return user.firstName[0,2] + user.lastName[0,2] for publishNameType = [PUBLISH_NAME_ALIAS_OR_INITALS, PUBLISH_NAME_INITIALS]
|
||||
*/
|
||||
public getUsername(publishNameType: PublishNameType): string {
|
||||
if (
|
||||
[
|
||||
PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS,
|
||||
PublishNameType.PUBLISH_NAME_INITIALS,
|
||||
].includes(publishNameType)
|
||||
) {
|
||||
return publishNameType === PublishNameType.PUBLISH_NAME_ALIAS_OR_INITALS && this.hasAlias()
|
||||
? this.user.alias
|
||||
: this.firstUpperCaseSecondLowerCase(this.user.firstName) +
|
||||
this.firstUpperCaseSecondLowerCase(this.user.lastName)
|
||||
}
|
||||
return this.hasAlias() ? this.user.alias : this.user.gradidoID
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user