-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgroup.controller.ts
More file actions
330 lines (315 loc) · 9 KB
/
group.controller.ts
File metadata and controls
330 lines (315 loc) · 9 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
import {
Body,
Controller,
Delete,
Get,
Param,
Patch,
Post,
Put,
Query,
Req,
Res,
} from '@nestjs/common';
import {
ApiBearerAuth,
ApiBody,
ApiOperation,
ApiParam,
ApiResponse,
ApiTags,
} from '@nestjs/swagger';
import { Request, Response } from 'express';
import {
CreateGroupDto,
BulkCreateGroupDto,
BulkCreateGroupResponseDto,
GroupResponseDto,
UpdateGroupDto,
PatchGroupDto,
GroupCriteria,
GetGroupCriteria,
GetGroupResponseDto,
GroupWithSubAndParentResponseDto,
} from 'src/dto/group.dto';
import { UserRole } from 'src/shared/enums/userRole.enum';
import { Roles } from 'src/shared/guards/tokenRoles.guard';
import { Scopes } from 'src/shared/decorators/scopes.decorator';
import { Scope } from 'src/shared/enums/scopes.enum';
import { JwtUser, isAdmin } from 'src/shared/modules/global/jwt.service';
import { GroupService } from './group.service';
import { setResHeader } from 'src/shared/helper';
@ApiTags('Group')
@Controller('/groups')
export class GroupController {
constructor(private readonly service: GroupService) {}
@ApiOperation({
summary: 'Search groups',
})
@ApiResponse({
status: 200,
description: 'Group list',
type: [GroupWithSubAndParentResponseDto],
headers: {
'X-Next-Page': {
description: 'The index of the next page',
schema: {
type: 'integer',
},
},
'X-Page': {
description: 'The index of the current page (starting at 1)',
schema: {
type: 'integer',
},
},
'X-Per-Page': {
description: 'The number of items to list per page',
schema: {
type: 'integer',
},
},
'X-Total': {
description: 'The total number of items',
schema: {
type: 'integer',
},
},
'X-Total-Pages': {
description: 'The total number of pages',
schema: {
type: 'integer',
},
},
Link: {
description: 'Pagination link header',
schema: {
type: 'integer',
},
},
},
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Get()
@ApiBearerAuth()
@Roles(UserRole.Admin, UserRole.User)
@Scopes(Scope.ReadGroups, Scope.WriteGroups, Scope.AllGroups)
async search(
@Req() req: Request,
@Res({ passthrough: true }) res: Response,
@Query() criteria: GroupCriteria,
) {
const authUser: JwtUser = req['user'] as JwtUser;
const result = await this.service.search(criteria, isAdmin(authUser));
setResHeader(req, res, result.page, result.perPage, result.total);
return result.data;
}
@ApiOperation({
summary: 'flush cache',
})
@ApiResponse({
status: 200,
description: 'The success message',
})
@Get('/flushCache')
@ApiBearerAuth()
@Roles(UserRole.Admin)
@Scopes(Scope.WriteGroups)
async flushCache() {
return Promise.resolve({
message: 'all cached data has been removed',
});
}
@ApiOperation({
summary: 'Get group by id',
})
@ApiParam({
name: 'id',
description: 'group id',
})
@ApiResponse({
status: 200,
description: 'Group details',
type: GetGroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Get('/:id')
@ApiBearerAuth()
@Roles(UserRole.Admin, UserRole.User)
@Scopes(Scope.ReadGroups, Scope.WriteGroups, Scope.AllGroups)
async getById(
@Req() req: Request,
@Param('id') id: string,
@Query() criteria: GetGroupCriteria,
) {
const authUser: JwtUser = req['user'] as JwtUser;
return await this.service.getGroup(authUser, id, criteria);
}
@ApiOperation({
summary: 'Create group',
description: 'Roles: Admin | Copilot',
})
@ApiBody({
description: 'Group data',
type: CreateGroupDto,
})
@ApiResponse({
status: 201,
description: 'Group details',
type: GroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Post()
@ApiBearerAuth()
@Roles(UserRole.Admin, UserRole.TGAdmin)
@Scopes(Scope.WriteGroups, Scope.AllGroups)
async create(@Req() req: Request, @Body() dto: CreateGroupDto) {
const authUser: JwtUser = req['user'] as JwtUser;
return await this.service.createGroup(authUser, dto);
}
@ApiOperation({
summary: 'Bulk create group with members',
description: 'Roles: Admin | Copilot | Project Manager | Taleng Manager',
})
@ApiBody({
description: 'Group data',
type: BulkCreateGroupDto,
})
@ApiResponse({
status: 201,
description: 'Group details',
type: BulkCreateGroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Post('/bulk-create')
@ApiBearerAuth()
@Roles(
UserRole.Admin,
UserRole.Copilot,
UserRole.ProjectManager,
UserRole.TalentManager,
)
@Scopes(Scope.WriteGroups, Scope.AllGroups)
async bulkCreateGroup(@Req() req: Request, @Body() dto: BulkCreateGroupDto) {
const authUser: JwtUser = req['user'] as JwtUser;
return await this.service.bulkCreateGroup(authUser, dto);
}
@ApiOperation({
summary: 'Update group by id',
})
@ApiBody({
description: 'Group update data',
type: UpdateGroupDto,
})
@ApiResponse({
status: 200,
description: 'Group details',
type: GroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Put('/:id')
@ApiBearerAuth()
@Roles(UserRole.Admin)
@Scopes(Scope.WriteGroups, Scope.AllGroups)
async updateGroup(
@Req() req: Request,
@Param('id') id: string,
@Body() dto: UpdateGroupDto,
) {
const authUser: JwtUser = req['user'] as JwtUser;
return await this.service.updateGroup(authUser, id, dto);
}
@ApiOperation({
summary: 'Patch group by id',
})
@ApiBody({
description: 'Group patch data',
type: PatchGroupDto,
})
@ApiResponse({
status: 200,
description: 'Group details',
type: GroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Patch('/:id')
@ApiBearerAuth()
@Roles(UserRole.Admin)
@Scopes(Scope.WriteGroups, Scope.AllGroups)
async patchGroup(
@Req() req: Request,
@Param('id') id: string,
@Body() dto: PatchGroupDto,
) {
const authUser: JwtUser = req['user'] as JwtUser;
return await this.service.patchGroup(authUser, id, dto);
}
@ApiOperation({
summary: 'Delete group by id',
})
@ApiResponse({
status: 200,
description: 'Deleted group details',
type: GroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 406, description: 'Not Acceptable' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Delete('/:id')
@ApiBearerAuth()
@Roles(UserRole.Admin)
@Scopes(Scope.WriteGroups, Scope.AllGroups)
async deleteGroup(@Req() req: Request, @Param('id') id: string) {
const authUser: JwtUser = req['user'] as JwtUser;
//eslint-disable-next-line @typescript-eslint/no-unsafe-return
return await this.service.deleteGroup(id, isAdmin(authUser));
}
@ApiOperation({
summary: 'Get group by old id',
})
@ApiParam({
name: 'oldId',
description: 'group old id',
})
@ApiResponse({
status: 200,
description: 'Group details',
type: GetGroupResponseDto,
})
@ApiResponse({ status: 400, description: 'Bad Request' })
@ApiResponse({ status: 401, description: 'Unauthorized' })
@ApiResponse({ status: 403, description: 'Forbidden' })
@ApiResponse({ status: 500, description: 'Internal Error' })
@Get('/oldId/:oldId')
@ApiBearerAuth()
@Roles(UserRole.Admin, UserRole.User)
@Scopes(Scope.ReadGroups, Scope.WriteGroups, Scope.AllGroups)
async getGroupByOldId(
@Req() req: Request,
@Param('oldId') oldId: string,
@Query() criteria: GetGroupCriteria,
) {
const authUser: JwtUser = req['user'] as JwtUser;
return await this.service.getGroup(authUser, '', criteria, oldId);
}
}