-
Notifications
You must be signed in to change notification settings - Fork 70
/
task-user-task.controller.ts
176 lines (169 loc) · 4.69 KB
/
task-user-task.controller.ts
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
import {inject} from '@loopback/core';
import {
Count,
CountSchema,
Filter,
FilterExcludingWhere,
Where,
WhereBuilder,
repository,
} from '@loopback/repository';
import {
HttpErrors,
del,
get,
getModelSchemaRef,
param,
patch,
} from '@loopback/rest';
import {
CONTENT_TYPE,
OPERATION_SECURITY_SPEC,
STATUS_CODE,
} from '@sourceloop/core';
import {STRATEGY, authenticate} from 'loopback4-authentication';
import {authorize} from 'loopback4-authorization';
import {TaskPermssionKey} from '../enums/permission-key.enum';
import {IUserTaskService} from '../interfaces';
import {TaskServiceBindings} from '../keys';
import {UserTask} from '../models';
import {TaskRepository, UserTaskRepository} from '../repositories';
const baseUrl = 'tasks/{taskId}/user-tasks';
export class TaskUserTaskController {
constructor(
@inject(TaskServiceBindings.SUB_TASK_SERVICE)
private readonly userTaskService: IUserTaskService,
@repository(TaskRepository)
private readonly taskRepo: TaskRepository,
@repository(UserTaskRepository)
private readonly userTaskRepo: UserTaskRepository,
) {}
@authenticate(STRATEGY.BEARER)
@authorize({permissions: [TaskPermssionKey.CompleteUserTask]})
@patch(`${baseUrl}/{userTaskId}/complete`, {
security: OPERATION_SECURITY_SPEC,
responses: {},
})
async completeTask(
@param.path.string('taskId') id: string,
@param.path.string('userTaskId') userTaskId: string,
): Promise<void> {
// need to maintain and validate mapping of task and user task
await this.userTaskService.complete(id, userTaskId);
}
@authenticate(STRATEGY.BEARER)
@authorize({
permissions: [TaskPermssionKey.ViewUserTask],
})
@get(baseUrl, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Array of UserTask model instances',
content: {
[CONTENT_TYPE.JSON]: {
schema: {
type: 'array',
items: getModelSchemaRef(UserTask, {includeRelations: true}),
},
},
},
},
},
})
async find(
@param.path.string('taskId') taskId: string,
@param.filter(UserTask) filter?: Filter<UserTask>,
) {
return this.taskRepo.userTasks(taskId).find(filter);
}
@authenticate(STRATEGY.BEARER)
@authorize({
permissions: [TaskPermssionKey.ViewUserTask],
})
@get(`${baseUrl}/{userTaskId}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'UserTask model instance',
content: {
[CONTENT_TYPE.JSON]: {
schema: getModelSchemaRef(UserTask, {includeRelations: true}),
},
},
},
},
})
async findById(
@param.path.string('taskId') taskId: string,
@param.path.string('userTaskId') userTaskId: string,
@param.filter(UserTask, {exclude: 'where'})
filter?: FilterExcludingWhere<UserTask>,
): Promise<UserTask> {
const userTask = await this.taskRepo.userTasks(taskId).find({
where: {
id: userTaskId,
},
...filter,
});
if (!userTask.length) {
throw new HttpErrors.NotFound('User Task not found');
}
return userTask[0];
}
@authenticate(STRATEGY.BEARER)
@authorize({
permissions: [TaskPermssionKey.ViewUserTask],
})
@get(`${baseUrl}/count`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.OK]: {
description: 'Task model count',
content: {[CONTENT_TYPE.JSON]: {schema: CountSchema}},
},
},
})
async count(
@param.path.string('taskId') taskId: string,
@param.where(UserTask) where?: Where<UserTask>,
): Promise<Count> {
const whereBuilder = new WhereBuilder<UserTask>();
whereBuilder.and({taskId}).and({...where});
return this.userTaskRepo.count(whereBuilder.build());
}
@authorize({
permissions: [TaskPermssionKey.DeleteUserTask],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@del(`${baseUrl}/{id}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'UserTasks DELETE by id success',
},
},
})
async deleteById(@param.path.string('id') id: string): Promise<void> {
await this.userTaskRepo.deleteByIdHard(id);
}
@authorize({
permissions: [TaskPermssionKey.DeleteUserTask],
})
@authenticate(STRATEGY.BEARER, {
passReqToCallback: true,
})
@del(`${baseUrl}`, {
security: OPERATION_SECURITY_SPEC,
responses: {
[STATUS_CODE.NO_CONTENT]: {
description: 'UserTasks DELETE success',
},
},
})
async delete(@param.where(UserTask) where?: Where<UserTask>): Promise<void> {
await this.userTaskRepo.deleteAllHard(where);
}
}