change default alias, add logic class for publishName enum

This commit is contained in:
einhornimmond 2024-04-15 12:48:12 +02:00
parent b40ad3254a
commit 37e91f86fd
2 changed files with 68 additions and 4 deletions

View File

@ -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

View File

@ -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
}
}