From e13d26f8c10ff990959f4f137bfc62aca860f41f Mon Sep 17 00:00:00 2001 From: Sergo Date: Sun, 30 Jul 2023 09:11:33 +0300 Subject: [PATCH] adjust alias generation --- src/services/aliases/aliases.class.ts | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/src/services/aliases/aliases.class.ts b/src/services/aliases/aliases.class.ts index 489f41d..e64ab35 100644 --- a/src/services/aliases/aliases.class.ts +++ b/src/services/aliases/aliases.class.ts @@ -47,7 +47,7 @@ export interface AliasesParams extends Params { export class AliasesService implements ServiceInterface { - constructor(public options: AliasesServiceOptions) {} + constructor(public options: AliasesServiceOptions) { } async find(params: ServiceParams): Promise { const userId = await this.getUserIdByEmailAddress(params); @@ -61,21 +61,22 @@ export class AliasesService params: ServiceParams, ): Promise { const userId = await this.getUserIdByEmailAddress(params); - const aliasFirstPart = faker.animal - .crocodilia() - .replace(/\D/g, "") - .replace(/\s/g, "") - .slice(0, 10); - const aliasSecondPart = faker.git.commitSha({ length: 5 }); - const alias = `${aliasFirstPart}-${aliasSecondPart}@${config.get( - "wildDuck.domain", - )}`; + const randomString = faker.git.commitSha({ length: 4 }); + + // Replace all non-alphanumeric characters with nothing and spaces with dashes + const alias = + `${faker.color.human()}-${faker.animal.snake()}-${randomString}` + .replace(/\s+/g, "-") + .replace(/[^a-zA-Z0-9-]/g, "") + .toLowerCase(); + + const emailDomain = config.get("wildDuck.domain"); const createResult = await wildDuckClient.post( `/users/${userId}/addresses`, { - address: alias, + address: `${alias}@${emailDomain}`, }, ); @@ -116,7 +117,7 @@ export class AliasesService const allowedDomain: string = config.get("wildDuck.domain"); // If address does not match the allowed domain, throw an error - if (!addressInfoResponse.address.endsWith(allowedDomain)) { + if (!allowedDomain || !addressInfoResponse.address.endsWith(allowedDomain)) { throw new BadRequest("Unable to delete address"); }