diff --git a/src/users/users.controller.ts b/src/users/users.controller.ts index 0e77bc0..6026058 100644 --- a/src/users/users.controller.ts +++ b/src/users/users.controller.ts @@ -46,6 +46,7 @@ export class UsersController { const userId = req.user.id; return this.usersService.editProfile(userId, updateUserDto); } + //admin endpoints///////////////////////////////////////////////////////// //get users list (admin) @UseGuards(RoleGuard) @Get("users") @@ -58,4 +59,10 @@ export class UsersController { async findSpecificUserInfoByUser(@Param("id") id): Promise { return this.usersService.findSpecificUserInfoByUser(id); } + //delete a specific user by admin + @UseGuards(RoleGuard) + @Get("users/delete/:id") + async deleteUser(@Param("id") id){ + return this.usersService.deleteUser(id); + } } diff --git a/src/users/users.service.ts b/src/users/users.service.ts index da76aff..158b54b 100644 --- a/src/users/users.service.ts +++ b/src/users/users.service.ts @@ -119,7 +119,7 @@ export class UsersService { throw new HttpException("User not found.", HttpStatus.NOT_FOUND); } await this.userModel.update({ refreshToken: null }, { where: { id: userId } }); - return{message:"logout is successful"} + return { message: "logout is successful" }; } //get information user method async getProfile(userId: number): Promise { @@ -176,4 +176,17 @@ export class UsersService { throw new HttpException("An unexpected error occurred while fetching user information.", HttpStatus.INTERNAL_SERVER_ERROR); } } + //delete a specific user by admin + async deleteUser(userId: number) { + const user = await this.userModel.findOne({ + where: { id: userId }, + }); + if (!user) { + throw new HttpException("User not found.", HttpStatus.NOT_FOUND); + } + await this.userModel.destroy({ + where: { id: userId }, + }); + return { message: "user deleted successful" }; + } }