Create endpoint to delete a user by admin

master
nicekid1 1 month ago
parent ed88f811ef
commit 9e84860f48
  1. 7
      src/users/users.controller.ts
  2. 15
      src/users/users.service.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<User> {
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);
}
}

@ -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<User> {
@ -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" };
}
}

Loading…
Cancel
Save