From 7ef3196307a23c0dd2ac2dc22ba1e6c588528f3d Mon Sep 17 00:00:00 2001 From: Ganesh Nawle Date: Tue, 25 Jun 2024 17:31:25 +0530 Subject: [PATCH] feat: added dynamic parameter implementation for email-template Signed-off-by: Ganesh Nawle --- .../src/user/dto/create-user.dto.ts | 22 +++++++++++++++++-- apps/user/src/user.service.ts | 11 +++++----- apps/user/templates/user-email-template.ts | 11 ++++++---- libs/common/src/interfaces/user.interface.ts | 2 ++ 4 files changed, 35 insertions(+), 11 deletions(-) diff --git a/apps/api-gateway/src/user/dto/create-user.dto.ts b/apps/api-gateway/src/user/dto/create-user.dto.ts index bd0161bf4..2b10c5595 100644 --- a/apps/api-gateway/src/user/dto/create-user.dto.ts +++ b/apps/api-gateway/src/user/dto/create-user.dto.ts @@ -1,7 +1,7 @@ -import { IsEmail, IsNotEmpty, IsString, MaxLength } from 'class-validator'; +import { IsEmail, IsNotEmpty, IsOptional, IsString, IsUrl, MaxLength } from 'class-validator'; import { toLowerCase, trim } from '@credebl/common/cast.helper'; -import { ApiProperty } from '@nestjs/swagger'; +import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger'; import { Transform } from 'class-transformer'; export class UserEmailVerificationDto { @@ -20,4 +20,22 @@ export class UserEmailVerificationDto { @ApiProperty({ example: 'xxxx-xxxxx-xxxxx' }) @IsString({ message: 'clientSecret should be string' }) clientSecret: string; + + @ApiPropertyOptional({ example: 'https://example.com/logo.png' }) + @Transform(({ value }) => trim(value)) + @IsOptional() + @IsUrl({ + // eslint-disable-next-line camelcase + require_protocol: true, + // eslint-disable-next-line camelcase + require_tld: true + }, + { message: 'brandLogoUrl should be a valid URL' }) + brandLogoUrl?: string; + + @ApiPropertyOptional({ example: 'MyPlatform' }) + @Transform(({ value }) => trim(value)) + @IsOptional() + @IsString({ message: 'platformName should be string' }) + platformName?: string; } diff --git a/apps/user/src/user.service.ts b/apps/user/src/user.service.ts index cc023fd60..6f8675235 100644 --- a/apps/user/src/user.service.ts +++ b/apps/user/src/user.service.ts @@ -89,7 +89,7 @@ export class UserService { */ async sendVerificationMail(userEmailVerification: ISendVerificationEmail): Promise { try { - const { email } = userEmailVerification; + const { email, brandLogoUrl, platformName } = userEmailVerification; if ('PROD' === process.env.PLATFORM_PROFILE_MODE) { // eslint-disable-next-line prefer-destructuring @@ -123,7 +123,7 @@ export class UserService { throw new NotFoundException(ResponseMessages.user.error.redirectUrlNotFound); } - await this.sendEmailForVerification(email, resUser.verificationCode, redirectUrl, resUser.clientId); + await this.sendEmailForVerification(email, resUser.verificationCode, redirectUrl, resUser.clientId, brandLogoUrl, platformName); } catch (error) { throw new InternalServerErrorException(ResponseMessages.user.error.emailSend); } @@ -165,7 +165,7 @@ export class UserService { * @returns */ - async sendEmailForVerification(email: string, verificationCode: string, redirectUrl: string, clientId: string): Promise { + async sendEmailForVerification(email: string, verificationCode: string, redirectUrl: string, clientId: string, brandLogoUrl:string, platformName: string): Promise { try { const platformConfigData = await this.prisma.platform_config.findMany(); @@ -173,9 +173,10 @@ export class UserService { const emailData = new EmailDto(); emailData.emailFrom = platformConfigData[0].emailFrom; emailData.emailTo = email; - emailData.emailSubject = `[${process.env.PLATFORM_NAME}] Verify your email to activate your account`; + const platform = platformName || process.env.PLATFORM_NAME; + emailData.emailSubject = `[${platform}] Verify your email to activate your account`; - emailData.emailHtml = await urlEmailTemplate.getUserURLTemplate(email, verificationCode, redirectUrl, clientId); + emailData.emailHtml = await urlEmailTemplate.getUserURLTemplate(email, verificationCode, redirectUrl, clientId, brandLogoUrl, platformName); const isEmailSent = await sendEmail(emailData); if (isEmailSent) { return isEmailSent; diff --git a/apps/user/templates/user-email-template.ts b/apps/user/templates/user-email-template.ts index c68d342c4..31cfbcdab 100644 --- a/apps/user/templates/user-email-template.ts +++ b/apps/user/templates/user-email-template.ts @@ -1,5 +1,5 @@ export class URLUserEmailTemplate { - public getUserURLTemplate(email: string, verificationCode: string, redirectUrl: string, clientId: string): string { + public getUserURLTemplate(email: string, verificationCode: string, redirectUrl: string, clientId: string, brandLogoUrl:string, platformName:string): string { const apiUrl = new URL( clientId === process.env.KEYCLOAK_MANAGEMENT_CLIENT_ID ? '/verify-email-success' : '', @@ -11,6 +11,9 @@ export class URLUserEmailTemplate { const validUrl = apiUrl.href; + const logoUrl = brandLogoUrl || process.env.BRAND_LOGO; + const platform = platformName || process.env.PLATFORM_NAME; + const poweredBy = platformName || process.env.POWERED_BY; try { return ` @@ -25,7 +28,7 @@ export class URLUserEmailTemplate {
- ${process.env.PLATFORM_NAME} logo + ${platform} logo
@@ -54,7 +57,7 @@ export class URLUserEmailTemplate {

- © ${process.env.POWERED_BY} + © ${poweredBy}

diff --git a/libs/common/src/interfaces/user.interface.ts b/libs/common/src/interfaces/user.interface.ts index db61440a8..998be0e70 100644 --- a/libs/common/src/interfaces/user.interface.ts +++ b/libs/common/src/interfaces/user.interface.ts @@ -15,6 +15,8 @@ export interface ISignInUser { clientId?: string; clientSecret?: string; username?: string; + brandLogoUrl?: string; + platformName?: string; } export interface IUserInvitations {