|
1 | | -import { Controller, Get, Post, Body, Param, UseGuards } from "@nestjs/common"; |
| 1 | +import { Controller, Get, Patch, Delete, Body, Param, UseGuards, Req } from "@nestjs/common"; |
2 | 2 | import { UserService } from "./user.service"; |
3 | 3 | import { User } from "../../../middle-layer/types/User"; |
4 | 4 | import { UserStatus } from "../../../middle-layer/types/UserStatus"; |
5 | | -import { VerifyAdminRoleGuard, VerifyUserGuard } from "../guards/auth.guard"; |
| 5 | +import { VerifyAdminRoleGuard, VerifyUserGuard, VerifyAdminOrEmployeeRoleGuard } from "../guards/auth.guard"; |
| 6 | +import { ApiResponse, ApiParam , ApiBearerAuth} from "@nestjs/swagger"; |
| 7 | +import { ChangeRoleBody } from "./types/user.types"; |
6 | 8 |
|
7 | 9 | @Controller("user") |
8 | 10 | export class UserController { |
9 | 11 | constructor(private readonly userService: UserService) {} |
10 | 12 |
|
| 13 | + /** |
| 14 | + * Get all users |
| 15 | + */ |
11 | 16 | @Get() |
12 | | - @UseGuards(VerifyUserGuard) |
| 17 | + @ApiResponse({ |
| 18 | + status : 200, |
| 19 | + description : "All users retrieved successfully" |
| 20 | + }) |
| 21 | + @ApiResponse({ |
| 22 | + status : 403, |
| 23 | + description : "Forbidden" |
| 24 | + }) |
| 25 | + @ApiResponse({ |
| 26 | + status : 500, |
| 27 | + description : "Internal Server Error" |
| 28 | + }) |
| 29 | + @UseGuards(VerifyAdminOrEmployeeRoleGuard) |
| 30 | + @ApiBearerAuth() |
13 | 31 | async getAllUsers() { |
14 | 32 | return await this.userService.getAllUsers(); |
15 | 33 | } |
16 | 34 |
|
| 35 | + |
| 36 | + /** |
| 37 | + * Get all inactive users |
| 38 | + */ |
17 | 39 | @Get("inactive") |
18 | | - @UseGuards(VerifyUserGuard) |
| 40 | + @ApiResponse({ |
| 41 | + status : 200, |
| 42 | + description : "All inactive users retrieved successfully" |
| 43 | + }) |
| 44 | + @ApiResponse({ |
| 45 | + status : 403, |
| 46 | + description : "Forbidden" |
| 47 | + }) |
| 48 | + @ApiResponse({ |
| 49 | + status : 500, |
| 50 | + description : "Internal Server Error" |
| 51 | + }) |
| 52 | + @UseGuards(VerifyAdminOrEmployeeRoleGuard) |
| 53 | + @ApiBearerAuth() |
19 | 54 | async getAllInactiveUsers(): Promise<User[]> { |
20 | 55 | return await this.userService.getAllInactiveUsers(); |
21 | 56 | } |
22 | 57 |
|
| 58 | + /** |
| 59 | + * Get all active users |
| 60 | + */ |
23 | 61 | @Get("active") |
24 | | - @UseGuards(VerifyUserGuard) |
| 62 | + @ApiResponse({ |
| 63 | + status : 200, |
| 64 | + description : "All active users retrieved successfully" |
| 65 | + }) |
| 66 | + @ApiResponse({ |
| 67 | + status : 403, |
| 68 | + description : "Forbidden" |
| 69 | + }) |
| 70 | + @ApiResponse({ |
| 71 | + status : 500, |
| 72 | + description : "Internal Server Error" |
| 73 | + }) |
| 74 | + @UseGuards(VerifyAdminOrEmployeeRoleGuard) |
| 75 | + @ApiBearerAuth() |
25 | 76 | async getAllActiveUsers(): Promise<User[]> { |
26 | 77 | console.log("Fetching all active users"); |
27 | 78 | return await this.userService.getAllActiveUsers(); |
28 | 79 | } |
29 | | - // Make sure to put a guard on this route |
30 | | - @Post("change-role") |
| 80 | + |
| 81 | + /** |
| 82 | + * Change a user's role (make sure guard is on this route) |
| 83 | + */ |
| 84 | + @Patch("change-role") |
| 85 | + @ApiResponse({ |
| 86 | + status : 200, |
| 87 | + description : "User role changed successfully" |
| 88 | + }) |
| 89 | + @ApiResponse({ |
| 90 | + status : 400, |
| 91 | + description : "{Error encountered}" |
| 92 | + }) |
| 93 | + @ApiResponse({ |
| 94 | + status : 401, |
| 95 | + description : "Unauthorized" |
| 96 | + }) |
| 97 | + @ApiResponse({ |
| 98 | + status : 403, |
| 99 | + description : "Forbidden" |
| 100 | + }) |
| 101 | + @ApiResponse({ |
| 102 | + status : 404, |
| 103 | + description : "Not Found" |
| 104 | + }) |
| 105 | + @ApiResponse({ |
| 106 | + status : 500, |
| 107 | + description : "Internal Server Error" |
| 108 | + }) |
31 | 109 | @UseGuards(VerifyAdminRoleGuard) |
| 110 | + @ApiBearerAuth() |
32 | 111 | async addToGroup( |
33 | | - @Body("user") user: User, |
34 | | - @Body("groupName") groupName: UserStatus, |
35 | | - @Body("requestedBy") requestedBy: User |
| 112 | + @Body() changeRoleBody: ChangeRoleBody, |
| 113 | + @Req() req: any |
36 | 114 | ): Promise<User> { |
| 115 | + // Get the requesting admin from the authenticated session (attached by guard) |
| 116 | + const requestedBy: User = req.user; |
| 117 | + |
37 | 118 | let newUser: User = await this.userService.addUserToGroup( |
38 | | - user, |
39 | | - groupName, |
| 119 | + changeRoleBody.user, |
| 120 | + changeRoleBody.groupName, |
40 | 121 | requestedBy |
41 | 122 | ); |
42 | 123 | return newUser as User; |
43 | 124 | } |
44 | 125 |
|
45 | | - @Post("delete-user") |
| 126 | + /** |
| 127 | + * Delete a user |
| 128 | + */ |
| 129 | + @Delete("delete-user/:userId") |
| 130 | + @ApiParam({ |
| 131 | + name: 'userId', |
| 132 | + description: 'ID of the user to delete', |
| 133 | + required: true, |
| 134 | + type: String |
| 135 | + }) |
| 136 | + @ApiResponse({ |
| 137 | + status : 200, |
| 138 | + description : "User deleted successfully" |
| 139 | + }) |
| 140 | + @ApiResponse({ |
| 141 | + status : 400, |
| 142 | + description : "{Error encountered}" |
| 143 | + }) |
| 144 | + @ApiResponse({ |
| 145 | + status : 401, |
| 146 | + description : "Unauthorized" |
| 147 | + }) |
| 148 | + @ApiResponse({ |
| 149 | + status : 403, |
| 150 | + description : "Forbidden" |
| 151 | + }) |
| 152 | + @ApiResponse({ |
| 153 | + status : 404, |
| 154 | + description : "Not Found" |
| 155 | + }) |
| 156 | + @ApiResponse({ |
| 157 | + status : 500, |
| 158 | + description : "Internal Server Error" |
| 159 | + }) |
46 | 160 | @UseGuards(VerifyAdminRoleGuard) |
| 161 | + @ApiBearerAuth() |
47 | 162 | async deleteUser( |
48 | | - @Body("user") user: User, |
49 | | - @Body("requestedBy") requestedBy: User |
| 163 | + @Param('userId') userId: string, |
| 164 | + @Req() req: any |
50 | 165 | ): Promise<User> { |
51 | | - let deletedUser = await this.userService.deleteUser(user, requestedBy); |
52 | | - return user as User; |
| 166 | + // Get the requesting admin from the authenticated session (attached by guard) |
| 167 | + const requestedBy: User = req.user; |
| 168 | + |
| 169 | + // Fetch the user to delete from the database |
| 170 | + const userToDelete: User = await this.userService.getUserById(userId); |
| 171 | + |
| 172 | + return await this.userService.deleteUser(userToDelete, requestedBy); |
53 | 173 | } |
54 | 174 |
|
| 175 | + /** |
| 176 | + * Get user by ID |
| 177 | + */ |
55 | 178 | @Get(":id") |
56 | | - @UseGuards(VerifyUserGuard) |
57 | | - async getUserById(@Param("id") userId: string) { |
| 179 | + @ApiParam({ |
| 180 | + name: 'id', |
| 181 | + description: 'User ID to retrieve', |
| 182 | + required: true, |
| 183 | + type: String |
| 184 | + }) |
| 185 | + @ApiResponse({ |
| 186 | + status : 200, |
| 187 | + description : "User retrieved successfully" |
| 188 | + }) |
| 189 | + @ApiResponse({ |
| 190 | + status : 403, |
| 191 | + description : "Forbidden" |
| 192 | + }) |
| 193 | + @ApiResponse({ |
| 194 | + status : 500, |
| 195 | + description : "Internal Server Error" |
| 196 | + }) |
| 197 | + @UseGuards(VerifyAdminOrEmployeeRoleGuard) |
| 198 | + @ApiBearerAuth() |
| 199 | + async getUserById(@Param('id') userId: string): Promise<User> { |
58 | 200 | return await this.userService.getUserById(userId); |
59 | 201 | } |
60 | 202 | } |
0 commit comments