mirror of
https://github.com/ershisan99/flashcards-api.git
synced 2025-12-17 12:33:22 +00:00
feat: add delete current user account use case
This commit is contained in:
@@ -1,6 +1,7 @@
|
|||||||
import {
|
import {
|
||||||
Body,
|
Body,
|
||||||
Controller,
|
Controller,
|
||||||
|
Delete,
|
||||||
Get,
|
Get,
|
||||||
HttpCode,
|
HttpCode,
|
||||||
HttpStatus,
|
HttpStatus,
|
||||||
@@ -57,6 +58,7 @@ import {
|
|||||||
UpdateUserCommand,
|
UpdateUserCommand,
|
||||||
VerifyEmailCommand,
|
VerifyEmailCommand,
|
||||||
} from './use-cases'
|
} from './use-cases'
|
||||||
|
import { DeleteCurrentAccountCommand } from './use-cases/delete-current-user-account-use-case'
|
||||||
|
|
||||||
@ApiTags('Auth')
|
@ApiTags('Auth')
|
||||||
@Controller('auth')
|
@Controller('auth')
|
||||||
@@ -94,6 +96,23 @@ export class AuthController {
|
|||||||
return await this.commandBus.execute(new UpdateUserCommand(userId, body, files?.avatar?.[0]))
|
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({
|
@ApiOperation({
|
||||||
description: 'Sign in using email and password. Must have an account to do so.',
|
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.',
|
summary: 'Sign in using email and password. Must have an account to do so.',
|
||||||
|
|||||||
@@ -19,10 +19,12 @@ import {
|
|||||||
VerifyEmailHandler,
|
VerifyEmailHandler,
|
||||||
UpdateUserHandler,
|
UpdateUserHandler,
|
||||||
} from './use-cases'
|
} from './use-cases'
|
||||||
|
import { DeleteCurrentUserAccountHandler } from './use-cases/delete-current-user-account-use-case'
|
||||||
|
|
||||||
const commandHandlers = [
|
const commandHandlers = [
|
||||||
CreateUserHandler,
|
CreateUserHandler,
|
||||||
GetCurrentUserDataHandler,
|
GetCurrentUserDataHandler,
|
||||||
|
DeleteCurrentUserAccountHandler,
|
||||||
LogoutHandler,
|
LogoutHandler,
|
||||||
RefreshTokenHandler,
|
RefreshTokenHandler,
|
||||||
ResendVerificationEmailHandler,
|
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