create use cases for auth

This commit is contained in:
andres
2023-06-15 23:10:07 +02:00
parent 612b2326f9
commit 131fec67de
20 changed files with 315 additions and 239 deletions

View File

@@ -0,0 +1,41 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { BadRequestException, NotFoundException } from '@nestjs/common'
import { UsersRepository } from '../../users/infrastructure/users.repository'
import { UsersService } from '../../users/services/users.service'
export class ResendVerificationEmailCommand {
constructor(public readonly userId: string) {}
}
@CommandHandler(ResendVerificationEmailCommand)
export class ResendVerificationEmailHandler
implements ICommandHandler<ResendVerificationEmailCommand>
{
constructor(
private readonly usersRepository: UsersRepository,
private readonly usersService: UsersService
) {}
async execute(command: ResendVerificationEmailCommand) {
const user = await this.usersRepository.findUserById(command.userId)
console.log(user)
if (!user) {
throw new NotFoundException('User not found')
}
if (user.isEmailVerified) {
throw new BadRequestException('Email has already been verified')
}
const updatedUser = await this.usersRepository.updateVerificationToken(user.id)
await this.usersService.sendConfirmationEmail({
email: updatedUser.user.email,
name: updatedUser.user.name,
verificationToken: updatedUser.verificationToken,
})
if (!updatedUser) {
throw new NotFoundException('User not found')
}
return null
}
}