mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-16 20:59:26 +00:00
feat: add delete current user account use case
This commit is contained in:
@@ -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.',
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user