feat: add delete current user account use case

This commit is contained in:
2024-07-26 11:49:50 +02:00
parent f697747124
commit 70bed1c1bb
3 changed files with 39 additions and 0 deletions

View File

@@ -1,6 +1,7 @@
import {
Body,
Controller,
Delete,
Get,
HttpCode,
HttpStatus,
@@ -57,6 +58,7 @@ import {
UpdateUserCommand,
VerifyEmailCommand,
} from './use-cases'
import { DeleteCurrentAccountCommand } from './use-cases/delete-current-user-account-use-case'
@ApiTags('Auth')
@Controller('auth')
@@ -94,6 +96,23 @@ export class AuthController {
return await this.commandBus.execute(new UpdateUserCommand(userId, body, files?.avatar?.[0]))
}
@ApiOperation({
description:
'Delete current user account. All the user data will be deleted forever. This action can not be undone',
summary: 'Delete current user account',
})
@ApiUnauthorizedResponse({ description: 'Not logged in' })
@ApiBadRequestResponse({ description: 'User not found' })
@ApiBearerAuth()
@UseGuards(JwtAuthGuard)
@HttpCode(HttpStatus.NO_CONTENT)
@Delete('me')
async deleteUserAccount(@Request() req): Promise<UserEntity> {
const userId = req.user.id
return await this.commandBus.execute(new DeleteCurrentAccountCommand(userId))
}
@ApiOperation({
description: 'Sign in using email and password. Must have an account to do so.',
summary: 'Sign in using email and password. Must have an account to do so.',

View File

@@ -19,10 +19,12 @@ import {
VerifyEmailHandler,
UpdateUserHandler,
} from './use-cases'
import { DeleteCurrentUserAccountHandler } from './use-cases/delete-current-user-account-use-case'
const commandHandlers = [
CreateUserHandler,
GetCurrentUserDataHandler,
DeleteCurrentUserAccountHandler,
LogoutHandler,
RefreshTokenHandler,
ResendVerificationEmailHandler,

View File

@@ -0,0 +1,18 @@
import { CommandHandler, ICommandHandler } from '@nestjs/cqrs'
import { UsersRepository } from '../../users/infrastructure/users.repository'
export class DeleteCurrentAccountCommand {
constructor(public readonly userId: string) {}
}
@CommandHandler(DeleteCurrentAccountCommand)
export class DeleteCurrentUserAccountHandler
implements ICommandHandler<DeleteCurrentAccountCommand>
{
constructor(private readonly usersRepository: UsersRepository) {}
async execute(command: DeleteCurrentAccountCommand): Promise<boolean> {
return await this.usersRepository.deleteUserById(command.userId)
}
}