-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathevent-shift-controller.ts
281 lines (261 loc) · 9.39 KB
/
event-shift-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
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
/**
* SudoSOS back-end API service.
* Copyright (C) 2020 Study association GEWIS
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
import BaseController, { BaseControllerOptions } from './base-controller';
import { Response } from 'express';
import log4js, { Logger } from 'log4js';
import Policy from './policy';
import { RequestWithToken } from '../middleware/token-middleware';
import EventService, { ShiftSelectedCountParams } from '../service/event-service';
import { EventShiftRequest } from './request/event-request';
import EventShift from '../entity/event/event-shift';
import { parseRequestPagination } from '../helpers/pagination';
import { asDate, asEventType } from '../helpers/validators';
export default class EventShiftController extends BaseController {
private logger: Logger = log4js.getLogger('EventShiftLogger');
/**
* Create a new user controller instance.
* @param options - The options passed to the base controller.
*/
public constructor(
options: BaseControllerOptions,
) {
super(options);
this.logger.level = process.env.LOG_LEVEL;
}
public getPolicy(): Policy {
return {
'/': {
GET: {
policy: async (req) => this.roleManager.can(
req.token.roles, 'get', 'all', 'Event', ['*'],
),
handler: this.getAllShifts.bind(this),
},
POST: {
policy: async (req) => this.roleManager.can(
req.token.roles, 'create', 'all', 'Event', ['*'],
),
handler: this.createShift.bind(this),
},
},
'/:id(\\d+)': {
PATCH: {
policy: async (req) => this.roleManager.can(req.token.roles, 'update', 'all', 'Event', ['*']),
handler: this.updateShift.bind(this),
},
DELETE: {
policy: async (req) => this.roleManager.can(req.token.roles, 'delete', 'all', 'Event', ['*']),
handler: this.deleteShift.bind(this),
},
},
'/:id(\\d+)/counts': {
GET: {
policy: async (req) => this.roleManager.can(req.token.roles, 'update', 'all', 'Event', ['*']),
handler: this.getShiftSelectedCount.bind(this),
},
},
};
}
/**
* GET /eventshifts
* @summary Get all event shifts
* @tags events - Operations of the event controller
* @operationId getAllEventShifts
* @security JWT
* @param {integer} take.query - How many entries the endpoint should return
* @param {integer} skip.query - How many entries should be skipped (for pagination)
* @return {PaginatedEventShiftResponse} 200 - All existing event shifts
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async getAllShifts(req: RequestWithToken, res: Response) {
this.logger.trace('Get all shifts by user', req.token.user);
let take;
let skip;
try {
const pagination = parseRequestPagination(req);
take = pagination.take;
skip = pagination.skip;
} catch (e) {
res.status(400).send(e.message);
return;
}
try {
const shifts = await EventService.getEventShifts({ take, skip });
res.json(shifts);
} catch (e) {
this.logger.error('Could not return all shifts:', e);
res.status(500).json('Internal server error.');
}
}
/**
* POST /eventshifts
* @summary Create an event shift
* @tags events - Operations of the event controller
* @operationId createEventShift
* @security JWT
* @param {CreateShiftRequest} request.body.required
* @return {EventShiftResponse} 200 - Created event shift
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async createShift(req: RequestWithToken, res: Response) {
const body = req.body as EventShiftRequest;
this.logger.trace('Create shift', body, 'by user', req.token.user);
let params: EventShiftRequest;
try {
params = {
name: req.body.name.toString(),
roles: req.body.roles,
};
if (params.name === '' || !Array.isArray(params.roles)) {
res.status(400).json('Invalid shift.');
return;
}
} catch (e) {
res.status(400).json('Invalid shift.');
return;
}
// handle request
try {
res.json(await EventService.createEventShift(params));
} catch (error) {
this.logger.error('Could not create event shift:', error);
res.status(500).json('Internal server error.');
}
}
/**
* PATCH /eventshifts/{id}
* @summary Update an event shift
* @tags events - Operations of the event controller
* @operationId updateEventShift
* @security JWT
* @param {integer} id.path.required - The id of the event which should be returned
* @param {UpdateShiftRequest} request.body.required
* @return {EventShiftResponse} 200 - Created event shift
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async updateShift(req: RequestWithToken, res: Response) {
const { id: rawId } = req.params;
const body = req.body as EventShiftRequest;
this.logger.trace('Update shift', rawId, 'with body', body, 'by user', req.token.user);
let id = Number.parseInt(rawId, 10);
try {
const shift = await EventShift.findOne({ where: { id } });
if (shift == null) {
res.status(404).send();
return;
}
} catch (error) {
this.logger.error('Could not update event:', error);
res.status(500).json('Internal server error.');
}
let param: Partial<EventShiftRequest>;
try {
param = {
name: req.body.name?.toString(),
roles: req.body.roles,
};
if (param.name === '' || (param.roles !== undefined && !Array.isArray(param.roles))) {
res.status(400).json('Invalid shift.');
return;
}
} catch (e) {
res.status(400).json('Invalid shift.');
return;
}
// handle request
try {
res.json(await EventService.updateEventShift(id, param));
} catch (error) {
this.logger.error('Could not update event shift:', error);
res.status(500).json('Internal server error.');
}
}
/**
* DELETE /eventshifts/{id}
* @summary Delete an event shift with its answers
* @tags events - Operations of the event controller
* @operationId deleteEventShift
* @security JWT
* @param {integer} id.path.required - The id of the event which should be deleted
* @return 204 - Success
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async deleteShift(req: RequestWithToken, res: Response) {
const { id: rawId } = req.params;
this.logger.trace('Delete shift with ID', rawId, 'by user', req.token.user);
try {
const id = Number.parseInt(rawId, 10);
const shift = await EventShift.findOne({ where: { id }, withDeleted: true });
if (shift == null) {
res.status(404).send();
return;
}
await EventService.deleteEventShift(id);
res.status(204).send();
} catch (error) {
this.logger.error('Could not delete event shift:', error);
res.status(500).json('Internal server error.');
}
}
/**
* GET /eventshifts/{id}/counts
* @summary Get the number of times a user has been selected for the given shift
* @tags events - Operations of the event controller
* @operationId getEventShiftCount
* @security JWT
* @param {integer} id.path.required - The id of the event shift
* @param {string} eventType.query - Only include events of this type
* @param {string} afterDate.query - Only include events after this date
* @param {string} beforeDate.query - Only include events before this date
* @return {Array<PaginatedEventShiftResponse>} 200 - All existing event shifts
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async getShiftSelectedCount(req: RequestWithToken, res: Response) {
const { id: rawId } = req.params;
this.logger.trace('Delete shift with ID', rawId, 'by user', req.token.user);
try {
const id = Number.parseInt(rawId, 10);
const shift = await EventShift.findOne({ where: { id } });
if (shift == null) {
res.status(404).send();
return;
}
let params: ShiftSelectedCountParams;
try {
params = {
eventType: asEventType(req.query.eventType),
afterDate: asDate(req.query.afterDate),
beforeDate: asDate(req.query.beforeDate),
};
} catch (e) {
res.status(400).send(e.message);
return;
}
const counts = await EventService.getShiftSelectedCount(id, params);
res.json(counts);
} catch (error) {
this.logger.error('Could not get event shift counts:', error);
res.status(500).json('Internal server error.');
}
}
}