From b7d87b2fef065bb5aefa4860fb6b4eda118004e3 Mon Sep 17 00:00:00 2001 From: Andres Date: Fri, 21 Jul 2023 16:59:03 +0200 Subject: [PATCH] add html to signup endpoint --- src/modules/auth/dto/registration.dto.ts | 23 ++++++++++++++++++- .../auth/use-cases/create-user-use-case.ts | 9 ++++++++ src/modules/users/dto/create-user.dto.ts | 3 +++ src/modules/users/services/users.service.ts | 11 ++++++--- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/src/modules/auth/dto/registration.dto.ts b/src/modules/auth/dto/registration.dto.ts index 92530ac..cd8742e 100644 --- a/src/modules/auth/dto/registration.dto.ts +++ b/src/modules/auth/dto/registration.dto.ts @@ -1,4 +1,5 @@ -import { IsEmail, IsOptional, Length } from 'class-validator' +import { ApiProperty } from '@nestjs/swagger' +import { IsBoolean, IsEmail, IsOptional, IsString, Length } from 'class-validator' export class RegistrationDto { @Length(3, 30) @@ -10,4 +11,24 @@ export class RegistrationDto { @IsEmail() email: string + + @ApiProperty({ + description: `HTML template to be sent in the email;\n ##name## will be replaced with the user's name; \n ##token## will be replaced with the password recovery token`, + example: `Hello, ##name##!
Please confirm your email by clicking on the link below:
Confirm email. If it doesn't work, copy and paste the following link in your browser:
http://localhost:3000/confirm-email/##token##`, + }) + @IsOptional() + @IsString() + html?: string + + /** Email subject */ + @IsOptional() + @IsString() + subject?: string + /** Whether to send a confirmation email or not. + * Defaults to false + * @example false + */ + @IsOptional() + @IsBoolean() + sendConfirmationEmail?: boolean } diff --git a/src/modules/auth/use-cases/create-user-use-case.ts b/src/modules/auth/use-cases/create-user-use-case.ts index a2a0d37..0ee316a 100644 --- a/src/modules/auth/use-cases/create-user-use-case.ts +++ b/src/modules/auth/use-cases/create-user-use-case.ts @@ -35,10 +35,19 @@ export class CreateUserHandler implements ICommandHandler { if (!createdUser) { return null } + if (!command.user.sendConfirmationEmail) { + return { + id: createdUser.id, + name: createdUser.name, + email: createdUser.email, + } + } await this.usersService.sendConfirmationEmail({ email: createdUser.email, name: createdUser.name, verificationToken: verificationToken, + html: command.user.html, + subject: command.user.subject, }) return { diff --git a/src/modules/users/dto/create-user.dto.ts b/src/modules/users/dto/create-user.dto.ts index 03050c8..2527bad 100644 --- a/src/modules/users/dto/create-user.dto.ts +++ b/src/modules/users/dto/create-user.dto.ts @@ -3,8 +3,11 @@ import { Length, Matches } from 'class-validator' export class CreateUserDto { @Length(3, 10) name: string + @Length(6, 20) password: string + + /** User's email address */ @Matches(/^[\w-.]+@([\w-]+\.)+[\w-]{2,4}$/) email: string } diff --git a/src/modules/users/services/users.service.ts b/src/modules/users/services/users.service.ts index 8ce33ef..1bfbb3a 100644 --- a/src/modules/users/services/users.service.ts +++ b/src/modules/users/services/users.service.ts @@ -32,18 +32,23 @@ export class UsersService { email, name, verificationToken, + html = `Hello, ##name##!
Please confirm your email by clicking on the link below:
Confirm email. If it doesn't work, copy and paste the following link in your browser:
http://localhost:3000/confirm-email/##token##`, + subject = 'E-mail confirmation', }: { email: string name: string verificationToken: string + html?: string + subject?: string }) { + const htmlFinal = html.replace('##token##', verificationToken)?.replace('##name##', name) + try { await this.emailService.sendMail({ from: 'andrii ', to: email, - text: 'hello and welcome, token is: ' + verificationToken, - html: `Hello ${name}!
Please confirm your email by clicking on the link below:
Confirm email`, - subject: 'E-mail confirmation', + html: htmlFinal, + subject, }) } catch (e) { this.logger.error(e?.message || e)