From 37e91f86fd5d1d85be016b544b447bea65ce81fe Mon Sep 17 00:00:00 2001 From: einhornimmond Date: Mon, 15 Apr 2024 12:48:12 +0200 Subject: [PATCH] change default alias, add logic class for publishName enum --- backend/src/apis/humhub/model/Account.ts | 5 +- backend/src/data/PublishName.logic.ts | 67 ++++++++++++++++++++++++ 2 files changed, 68 insertions(+), 4 deletions(-) create mode 100644 backend/src/data/PublishName.logic.ts diff --git a/backend/src/apis/humhub/model/Account.ts b/backend/src/apis/humhub/model/Account.ts index 7b5e794bb..52777e8c8 100644 --- a/backend/src/apis/humhub/model/Account.ts +++ b/backend/src/apis/humhub/model/Account.ts @@ -7,10 +7,7 @@ export class Account { if (user.alias && user.alias.length > 2) { this.username = user.alias } else { - // Use the replace method to remove the part after '+' and before '@' - // source: https://people.cs.rutgers.edu/~watrous/plus-signs-in-email-addresses.html - // email address with + exist but humhub doesn't allow username with + - this.username = user.emailContact.email.replace(/\+(.*)@/, '@') + this.username = user.gradidoID } this.email = user.emailContact.email diff --git a/backend/src/data/PublishName.logic.ts b/backend/src/data/PublishName.logic.ts new file mode 100644 index 000000000..e68a29f86 --- /dev/null +++ b/backend/src/data/PublishName.logic.ts @@ -0,0 +1,67 @@ +import { User } from '@entity/User' + +import { GmsPublishNameType } from '@/graphql/enum/GmsPublishNameType'; + +export class PublishNameLogic { + constructor(private user: User) {} + + /** + * get first name based on publishNameType: GmsPublishNameType value + * @param publishNameType + * @returns user.firstName for GMS_PUBLISH_NAME_FIRST, GMS_PUBLISH_NAME_FIRST_INITIAL or GMS_PUBLISH_NAME_FULL + * first initial from user.firstName for GMS_PUBLISH_NAME_INITIALS or GMS_PUBLISH_NAME_ALIAS_OR_INITALS and empty alias + */ + public getFirstName(publishNameType: GmsPublishNameType): string | undefined { + if ( + [ + GmsPublishNameType.GMS_PUBLISH_NAME_FIRST, + GmsPublishNameType.GMS_PUBLISH_NAME_FIRST_INITIAL, + GmsPublishNameType.GMS_PUBLISH_NAME_FULL, + ].includes(publishNameType) + ) { + return this.user.firstName + } + if ( + (!this.user.alias && + publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_ALIAS_OR_INITALS) || + publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_INITIALS + ) { + return this.user.firstName.substring(0, 1) + } + } + + /** + * get last name based on publishNameType: GmsPublishNameType value + * @param publishNameType + * @returns user.lastName for GMS_PUBLISH_NAME_FULL + * first initial from user.lastName for GMS_PUBLISH_NAME_FIRST_INITIAL, GMS_PUBLISH_NAME_INITIALS or GMS_PUBLISH_NAME_ALIAS_OR_INITALS and empty alias + */ + public getLastName(publishNameType: GmsPublishNameType): string | undefined { + if (publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_FULL) { + return this.user.lastName + } + if ( + (!this.user.alias && + publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_ALIAS_OR_INITALS) || + publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_FIRST_INITIAL || + publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_INITIALS + ) { + return this.user.lastName.substring(0, 1) + } + } + + public getUsername(publishNameType: GmsPublishNameType): string { + if ( + this.user.alias && + publishNameType === GmsPublishNameType.GMS_PUBLISH_NAME_ALIAS_OR_INITALS + ) { + return this.user.alias + } + const firstName = this.getFirstName(publishNameType) + const lastName = this.getLastName(publishNameType) + if (firstName && lastName) { + return `${firstName} ${lastName}` + } + return this.user.gradidoID + } +}