Files
flashcards-api/src/modules/auth/use-cases/send-password-recovery-email-use-case.ts
2023-07-16 19:44:58 +02:00

42 lines
1.3 KiB
TypeScript

import { NotFoundException } from '@nestjs/common'
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { v4 as uuidv4 } from 'uuid'
import { UsersRepository } from '../../users/infrastructure/users.repository'
import { UsersService } from '../../users/services/users.service'
export class SendPasswordRecoveryEmailCommand {
constructor(public readonly email: string) {}
}
@CommandHandler(SendPasswordRecoveryEmailCommand)
export class SendPasswordRecoveryEmailHandler
implements ICommandHandler<SendPasswordRecoveryEmailCommand>
{
constructor(
private readonly usersRepository: UsersRepository,
private readonly usersService: UsersService
) {}
async execute(command: SendPasswordRecoveryEmailCommand) {
const user = await this.usersRepository.findUserByEmail(command.email)
if (!user) {
throw new NotFoundException('User not found')
}
const token = uuidv4()
const updatedUser = await this.usersRepository.createPasswordResetToken(user.id, token)
await this.usersService.sendPasswordRecoveryEmail({
email: updatedUser.user.email,
name: updatedUser.user.name,
passwordRecoveryToken: updatedUser.resetPasswordToken,
})
if (!updatedUser) {
throw new NotFoundException('User not found')
}
return null
}
}