From 9e84860f4835cecf349459aef29d42c655b9ca24 Mon Sep 17 00:00:00 2001 From: nicekid1 <86746988+nicekid1@users.noreply.github.com> Date: Sat, 11 Jan 2025 11:31:36 +0330 Subject: [PATCH] Create endpoint to delete a user by admin --- src/users/users.controller.ts | 7 +++++++ src/users/users.service.ts | 15 ++++++++++++++- 2 files changed, 21 insertions(+), 1 deletion(-) 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" }; + } }