mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +00:00
create use cases for auth
This commit is contained in:
@@ -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
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user