From 11d32ff373400ee45f7c4906cd9f96b63a43ccd1 Mon Sep 17 00:00:00 2001 From: NicoDora Date: Fri, 13 Oct 2023 00:29:03 +0900 Subject: [PATCH] =?UTF-8?q?refactor(#10):=20=EA=B3=84=EC=A0=95=20=EC=82=AD?= =?UTF-8?q?=EC=A0=9C=20=EB=AA=85=EC=84=B8=EC=84=9C=20=EC=9E=91=EC=84=B1?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/auth/controllers/auth.controller.ts | 5 +++++ src/auth/services/auth.service.ts | 4 ++-- src/users/repositories/user.repository.ts | 18 ++++-------------- 3 files changed, 11 insertions(+), 16 deletions(-) diff --git a/src/auth/controllers/auth.controller.ts b/src/auth/controllers/auth.controller.ts index 3203d17..61152db 100644 --- a/src/auth/controllers/auth.controller.ts +++ b/src/auth/controllers/auth.controller.ts @@ -143,6 +143,11 @@ export class AuthController { return await this.authService.naverUnlink(naverAccessToken); } + @ApiOperation({ summary: '계정 삭제 API', description: '계정 삭제 API' }) + @ApiResponse({ status: 200, description: '성공적으로 계정이 삭제 된 경우', content: { JSON: { example: { message: "사용자 계정 삭제에 성공했습니다." } } } }) + @ApiResponse({ status: 403, description: '만료된 액세스 토큰인 경우', content: { JSON: { example: { statusCode: 403, message: '만료된 토큰입니다.' } } } }) + @ApiResponse({ status: 404, description: 'DB에서 사용자를 찾을 수 없는 경우', content: { JSON: { example: { statusCode: 404, message: '사용자를 찾을 수 없습니다.' } } } }) + @ApiHeaders([{ name: 'access_token', description: '액세스 토큰', required: true, example: '여기에 액세스 토큰' }]) @Delete('account') async accountDelete(@Headers('access_token') accessToken: string) { const userId = await this.tokenService.decodeToken(accessToken); diff --git a/src/auth/services/auth.service.ts b/src/auth/services/auth.service.ts index 9d67556..7df0506 100644 --- a/src/auth/services/auth.service.ts +++ b/src/auth/services/auth.service.ts @@ -204,8 +204,8 @@ export class AuthService { async accountDelete(userId: number) { const deleteUser = await this.userRepository.deleteUser(userId); if (!deleteUser) { - return "사용자 계정 삭제에 실패했습니다."; + throw new HttpException('사용자를 찾을 수 없습니다.', HttpStatus.NOT_FOUND); } - return "사용자 계정 삭제에 성공했습니다."; + return { message: "사용자 계정 삭제가 완료되었습니다." }; } } diff --git a/src/users/repositories/user.repository.ts b/src/users/repositories/user.repository.ts index 06e8ae1..a7cbe22 100644 --- a/src/users/repositories/user.repository.ts +++ b/src/users/repositories/user.repository.ts @@ -1,5 +1,5 @@ import { Injectable, NotFoundException } from '@nestjs/common'; -import { EntityManager } from 'typeorm'; +import { DeleteResult, EntityManager } from 'typeorm'; import { User } from '../entities/user.entity'; @Injectable() @@ -34,18 +34,8 @@ export class UserRepository { return this.entityManager.save(user); } - async deleteUser(userId: number): Promise { - try { - const user = await this.entityManager.findOne(User, { where: { id: userId } }); - if (!user) { - throw new NotFoundException('사용자를 찾을 수 없습니다.'); - } else { - await this.entityManager.delete(User, { id: userId }); - return user; - } - } catch (error) { - console.error('사용자 삭제 오류:', error); - return null; - } + async deleteUser(userId: number): Promise { + await this.entityManager.findOne(User, { where: { id: userId } }); + return await this.entityManager.delete(User, { id: userId }); } }