Skip to content

Commit

Permalink
refactor: applying PR review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
r-dmatos committed Apr 22, 2022
1 parent 197892b commit 2c91628
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 12 deletions.
1 change: 1 addition & 0 deletions backend/src/infrastructure/database/mongoose.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export const mongooseBoardUserModule = MongooseModule.forFeature([
export const mongooseUserModule = MongooseModule.forFeature([
{ name: User.name, schema: UserSchema },
]);

export const mongooseResetModule = MongooseModule.forFeature([
{ name: ResetPassword.name, schema: ResetPasswordSchema },
]);
Expand Down
3 changes: 1 addition & 2 deletions backend/src/modules/auth/controller/auth.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,7 @@ export default class AuthController {
}

@Post('recoverPassword')
async forgot(@Body() emailDto: EmailParam) {
const { email } = emailDto;
forgot(@Body() { email }: EmailParam) {
return this.createResetTokenAuthApp.create(email);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { InjectModel } from '@nestjs/mongoose';
import { Model, ClientSession } from 'mongoose';
import { INSERT_FAILED } from '../../../libs/exceptions/messages';
import { CreateResetTokenAuthService } from '../interfaces/services/create-reset-token.auth.service.interface';
import ResetPassword, {
ResetPasswordDocument,
Expand All @@ -20,10 +19,9 @@ export default class CreateResetTokenAuthServiceImpl
private configService: ConfigService,
) {}

private frontendUrl = this.configService.get<string>('frontend.url');

public async emailBody(token: string, emailAddress: string) {
const url = `${this.frontendUrl}?${token}`;
const url = `${this.configService.get<string>('frontend.url')}?${token}`;
const msg = 'please check your email';
await this.mailerService.sendMail({
to: emailAddress,
subject: 'You requested a password reset',
Expand All @@ -34,7 +32,7 @@ export default class CreateResetTokenAuthServiceImpl
If you did not make this request then please ignore this email.`,
});
return { message: 'please check your email' };
return { message: msg };
}

public tokenGenerator(emailAddress: string, session: ClientSession) {
Expand Down Expand Up @@ -65,16 +63,16 @@ export default class CreateResetTokenAuthServiceImpl
async create(emailAddress: string) {
const session = await this.resetModel.db.startSession();
session.startTransaction();

try {
const passwordModel = await this.resetModel.findOne({ emailAddress });
if (!passwordModel) throw Error(INSERT_FAILED);
this.tokenValidator(passwordModel?.updatedAt);

if (passwordModel) {
this.tokenValidator(passwordModel.updatedAt);
}
const { token } = await this.tokenGenerator(emailAddress, session);
if (token) {
const res = await this.emailBody(token, emailAddress);
await session.commitTransaction();
return await this.emailBody(token, emailAddress);
return res;
}
throw new InternalServerErrorException();
} catch (e) {
Expand Down

0 comments on commit 2c91628

Please sign in to comment.