Skip to content

Commit

Permalink
feat: added dynamic parameter implementation in email-template for se…
Browse files Browse the repository at this point in the history
…nding verification email.

feat: added dynamic parameter implementation in email-template for sending verification email.
  • Loading branch information
ganeshawle25 authored Jun 25, 2024
2 parents ece2b79 + 78a546e commit f675411
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 11 deletions.
22 changes: 20 additions & 2 deletions apps/api-gateway/src/user/dto/create-user.dto.ts
Original file line number Diff line number Diff line change
@@ -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 {
Expand All @@ -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;
}
11 changes: 6 additions & 5 deletions apps/user/src/user.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ export class UserService {
*/
async sendVerificationMail(userEmailVerification: ISendVerificationEmail): Promise<user> {
try {
const { email } = userEmailVerification;
const { email, brandLogoUrl, platformName } = userEmailVerification;

if ('PROD' === process.env.PLATFORM_PROFILE_MODE) {
// eslint-disable-next-line prefer-destructuring
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -165,17 +165,18 @@ export class UserService {
* @returns
*/

async sendEmailForVerification(email: string, verificationCode: string, redirectUrl: string, clientId: string): Promise<boolean> {
async sendEmailForVerification(email: string, verificationCode: string, redirectUrl: string, clientId: string, brandLogoUrl:string, platformName: string): Promise<boolean> {
try {
const platformConfigData = await this.prisma.platform_config.findMany();

const urlEmailTemplate = new URLUserEmailTemplate();
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;
Expand Down
11 changes: 7 additions & 4 deletions apps/user/templates/user-email-template.ts
Original file line number Diff line number Diff line change
@@ -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' : '',
Expand All @@ -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 `<!DOCTYPE html>
<html lang="en">
Expand All @@ -25,7 +28,7 @@ export class URLUserEmailTemplate {
<div style="margin: auto; max-width: 450px; padding: 20px 30px; background-color: #FFFFFF; display:block;">
<div style="display: block; text-align:center;">
<img src="${process.env.BRAND_LOGO}" alt="${process.env.PLATFORM_NAME} logo" style="max-width:100px; background: white; padding: 5px;border-radius: 5px;" width="100%" height="fit-content" class="CToWUd" data-bit="iit">
<img src="${logoUrl}" alt="${platform} logo" style="max-width:100px; background: white; padding: 5px;border-radius: 5px;" width="100%" height="fit-content" class="CToWUd" data-bit="iit">
</div>
<div style="font-family: Montserrat; font-style: normal; font-weight: 500;
Expand All @@ -34,7 +37,7 @@ export class URLUserEmailTemplate {
Hello ${email},
</p>
<p>
We are excited to welcome you to the ${process.env.PLATFORM_NAME} Platform. Your user account ${email} has been successfully created.
We are excited to welcome you to the ${platform} Platform. Your user account ${email} has been successfully created.
</p><p>
To complete the verification process, please click on the "Verify" button or use the provided verification link below:
</p>
Expand All @@ -54,7 +57,7 @@ export class URLUserEmailTemplate {
</div>
<p style="margin-top: 6px;">
© ${process.env.POWERED_BY}
© ${poweredBy}
</p>
</footer>
</div>
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/interfaces/user.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ export interface ISignInUser {
clientId?: string;
clientSecret?: string;
username?: string;
brandLogoUrl?: string;
platformName?: string;
}

export interface IUserInvitations {
Expand Down

0 comments on commit f675411

Please sign in to comment.