-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflagged-transaction-controller.ts
457 lines (439 loc) · 17.8 KB
/
flagged-transaction-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
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
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
/**
* 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 { Response } from 'express';
import log4js, { Logger } from 'log4js';
import BaseController, { BaseControllerOptions } from './base-controller';
import Policy from './policy';
import { RequestWithToken } from '../middleware/token-middleware';
import FileService from '../service/file-service';
import { parseRequestPagination } from '../helpers/pagination';
import FlaggedTransactionRequest from "./request/flagged-transaction-request";
import FlaggedTransactionService, {CreateFlaggedTransactionParams} from "../service/flagged-transaction-service";
import FlaggedTransaction, {FlagStatus} from "../entity/transactions/flagged-transaction";
import {EventShiftRequest} from "./request/event-request";
import UpdateFlaggedTransactionRequest from "./request/UpdateFlaggedTransactionRequest";
export default class FlaggedTransactionController extends BaseController {
private logger: Logger = log4js.getLogger('FlaggedTransactionController');
private fileService: FileService;
/**
* Creates a new flagged transaction 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', 'FlaggedTransactions', ['*']),
handler: this.returnPendingFlaggedTransactions.bind(this),
},
POST: {
body: { modelName: 'FlaggedTransactionRequest' },
policy: async (req) => this.roleManager.can(req.token.roles, 'create', 'all', 'FlaggedTransactions', ['*']),
handler: this.createFlaggedTransaction.bind(this),
},
},
'/:id(\\d+)': {
GET: {
policy: async (req: RequestWithToken) => this.roleManager.can(req.token.roles, 'get', 'all', 'FlaggedTransactions', ['*']),
handler: this.getSingleFlaggedTransaction.bind(this),
},
PATCH: {
policy: async (req) => this.roleManager.can(req.token.roles, 'update', 'all', 'FlaggedTransactions', ['*']),
handler: this.updateFlaggedTransaction.bind(this),
},
DELETE: {
policy: async (req) => this.roleManager.can(req.token.roles, 'delete', 'all', 'FlaggedTransactions', ['*']),
handler: this.deleteFlaggedTransaction.bind(this),
},
},
// '/all': {
// GET: {
// policy: async (req) => this.roleManager.can(req.token.roles, 'get', 'all', 'FlaggedTransactions', ['*']),
// handler: this.returnAllFlaggedTransactions.bind(this),
// },
// },
// '/:id(\\d+)': {
// GET: {
// policy: async (req) => this.roleManager.can(req.token.roles, 'get', 'all', 'FlaggedTransactions', ['*']),
// handler: this.returnSingleBanner.bind(this),
// },
// PATCH: {
// body: { modelName: 'BannerRequest' },
// policy: async (req) => this.roleManager.can(req.token.roles, 'update', 'all', 'FlaggedTransactions', ['*']),
// handler: this.updateBanner.bind(this),
// },
// },
};
}
/**
* GET /flaggedtransactions
* @summary Returns all existing flagged transactions
* @operationId getPendingFlaggedTransactions
* @tags flagged - Operations of the flagged transactions controller
* @security JWT
* @param {integer} take.query - How many flagged transactions the endpoint should return
* @param {integer} skip.query - How many flagged transactions should be skipped (for pagination)
* @return {PaginatedFlaggedTransactionResponse} 200 - All existing flagged transactions
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async returnPendingFlaggedTransactions(req: RequestWithToken, res: Response): Promise<void> {
this.logger.trace('Get all pending flagged transactions by', 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 {
res.json(await FlaggedTransactionService.getFlaggedTransactions({}, { take, skip }));
} catch (error) {
this.logger.error('Could not return all flagged transactions:', error);
res.status(500).json('Internal server error.');
}
}
/**
* POST /flaggedtransactions
* @summary Creates a flagged transaction
* @operationId createFlaggedTransaction
* @tags flagged - Operations of the flagged transactions controller
* @security JWT
* @param {FlaggedTransactionRequest} request.body.required - The flagged transaction which should be created
* @return {FlaggedTransactionResponse} 200 - The created flagged transaction entity.
* @return {string} 400 - Validation error
* @return {string} 500 - Internal server error
*/
public async createFlaggedTransaction(request: RequestWithToken, res: Response): Promise<void>{
const body = request.body as FlaggedTransactionRequest;
this.logger.trace('Create flagged transaction', body, 'by user', request.token.user);
const params: CreateFlaggedTransactionParams = {
status: FlagStatus.TODO,
reason: body.reason,
flaggedById: request.token.user.id,
transactionId: body.transactionId,
};
try {
res.json(await FlaggedTransactionService.createFlaggedTransaction(params));
} catch (error) {
this.logger.error('Could not create flagged transaction:', error);
this.logger.error(params);
res.status(500).json('Internal server error.');
}
}
/**
* PATCH /flaggedtransactions/{id}
* @summary Update a flagged transaction
* @tags flagged - Operations of the flagged transactions controller
* @operationId updateFlaggedTransaction
* @security JWT
* @param {integer} id.path.required - The id of the flagged transaction to be updated
* @param {UpdateFlaggedTransactionRequest} request.body.required
* @return {FlaggedTransactionResponse} 200 - Created Flagged Transaction
* @return {string} 400 - Validation Error
* @return {string} 500 - Internal Server error
*/
public async updateFlaggedTransaction(req: RequestWithToken, res: Response) {
const { id: rawId } = req.params;
const body = req.body as UpdateFlaggedTransactionRequest;
this.logger.trace('Update flaggedTransaction', rawId, 'with body', body, 'by user', req.token.user);
let id = Number.parseInt(rawId, 10);
try {
const flaggedTransaction = await FlaggedTransaction.findOne({where: { id }});
if (flaggedTransaction == null) {
res.status(404).send();
return;
}
} catch (error) {
this.logger.error('Could not update flagged transaction:', error);
res.status(500).json('Internal server error.');
}
try {
res.json(await FlaggedTransactionService.updateFlaggedTransaction(id, body));
} catch (error) {
this.logger.error('Could not update flagged transaction:', error);
res.status(500).json('Internal server error.');
}
}
/**
* GET /flaggedtransactions/{id}
* @summary - Returns a single flagged transaction
* @operationId getSingleFlaggedTransactions
* @tags flagged - Operations of the flagged transactions controller
* @param {integer} id.path.required - The id of the flagged transaction which should be returned
* @security JWT
* @return {FlaggedTransactionResponse} 200 - The requested flagged transaction
* @return {string} 404 - Not found error
* @return {string} 500 - Internal server error
*/
public async getSingleFlaggedTransaction(req: RequestWithToken, res: Response): Promise<void> {
const { id } = req.params;
this.logger.trace('Get single flagged transaction', id, 'by user', req.token.user);
const flaggedTransactionId = parseInt(id, 10);
// Handle request
try {
const flaggedTransaction = await FlaggedTransactionService.getSingleFlaggedTransaction(flaggedTransactionId);
if (!flaggedTransaction) {
res.status(404).json('Flagged transaction not found.');
return;
} else {
res.json(flaggedTransaction);
}
} catch (error) {
this.logger.error('Could not return single flagged transaction:', error);
res.status(500).json('Internal server error.');
}
}
/**
* DELETE /flaggedtransactions/{id}
* @summary Deletes a flagged transaction.
* @operationId deleteFlaggedTransaction
* @tags flagged - Operations of the flagged transactions controller
* @security JWT
* @param {integer} id.path.required - The id of the flagged transaction which should be deleted
* @return {string} 404 - Invoice not found
* @return {string} 204 - Update success
* @return {string} 500 - Internal server error
*/
public async deleteFlaggedTransaction(req: RequestWithToken, res: Response): Promise<void> {
const { id } = req.params;
const flaggedTransactionId = parseInt(id, 10);
this.logger.trace('Delete Flagged Transaction', id, 'by user', req.token.user);
try {
const flaggedTransaction = await FlaggedTransactionService.deleteFlaggedTransaction(flaggedTransactionId);
if (flaggedTransaction) {
res.status(204).json();
} else {
res.status(404).json('Flagged transaction not found');
}
} catch (error) {
this.logger.error('Could not remove flagged transaction:', error);
res.status(500).json('Internal server error.');
}
}
// /**
// * POST /banners
// * @summary Saves a banner to the database
// * @operationId create
// * @tags banners - Operations of banner controller
// * @param {BannerRequest} request.body.required - The banner which should be created
// * @security JWT
// * @return {BannerResponse} 200 - The created banner entity
// * @return {string} 400 - Validation error
// * @return {string} 500 - Internal server error
// */
// public async createBanner(req: RequestWithToken, res: Response): Promise<void> {
// const body = req.body as BannerRequest;
// this.logger.trace('Create banner', body, 'by user', req.token.user);
//
// // handle request
// try {
// if (BannerService.verifyBanner(body)) {
// res.json(await BannerService.createBanner(body));
// } else {
// res.status(400).json('Invalid banner.');
// }
// } catch (error) {
// this.logger.error('Could not create banner:', error);
// res.status(500).json('Internal server error.');
// }
// }
//
// /**
// * POST /banners/{id}/image
// * @summary Uploads a banner image to the given banner
// * @operationId updateImage
// * @tags banners - Operations of banner controller
// * @param {integer} id.path.required - The id of the banner
// * @param {FileRequest} request.body.required - banner image - multipart/form-data
// * @security JWT
// * @return 204 - Success
// * @return {string} 400 - Validation error
// * @return {string} 500 - Internal server error
// */
// public async uploadBannerImage(req: RequestWithToken, res: Response): Promise<void> {
// const { id } = req.params;
// const { files } = req;
// this.logger.trace('Upload banner image for banner', id, 'by user', req.token.user);
//
// if (!req.files || Object.keys(files).length !== 1) {
// res.status(400).send('No file or too many files were uploaded');
// return;
// }
// if (files.file === undefined) {
// res.status(400).send("No file is uploaded in the 'file' field");
// return;
// }
// const file = files.file as UploadedFile;
// if (file.data === undefined) {
// res.status(400).send('File body data is missing from request');
// return;
// }
// if (file.name === undefined) {
// res.status(400).send('File name is missing from request');
// return;
// }
//
// const bannerId = parseInt(id, 10);
//
// try {
// const banner = await Banner.findOne({ where: { id: bannerId }, relations: ['image'] });
// if (banner) {
// await this.fileService.uploadEntityImage(
// banner, file, req.token.user,
// );
// res.status(204).send();
// return;
// }
// res.status(404).json('Banner not found');
// return;
// } catch (error) {
// this.logger.error('Could not upload image:', error);
// res.status(500).json('Internal server error');
// }
// }
//
// /**
// * GET /banners/{id}
// * @summary Returns the requested banner
// * @operationId getBanner
// * @tags banners - Operations of banner controller
// * @param {integer} id.path.required - The id of the banner which should be returned
// * @security JWT
// * @return {BannerResponse} 200 - The requested banner entity
// * @return {string} 404 - Not found error
// * @return {string} 500 - Internal server error
// */
// public async returnSingleBanner(req: RequestWithToken, res: Response): Promise<void> {
// const { id } = req.params;
// this.logger.trace('Get single banner', id, 'by user', req.token.user);
//
// // handle request
// try {
// // check if banner in database
// const { records } = await BannerService.getBanners({ bannerId: Number.parseInt(id, 10) });
// if (records.length > 0) {
// res.json(records[0]);
// } else {
// res.status(404).json('Banner not found.');
// }
// } catch (error) {
// this.logger.error('Could not return banner:', error);
// res.status(500).json('Internal server error.');
// }
// }
//
// /**
// * PATCH /banners/{id}
// * @summary Updates the requested banner
// * @operationId update
// * @tags banners - Operations of banner controller
// * @param {integer} id.path.required - The id of the banner which should be updated
// * @param {BannerRequest} request.body.required - The updated banner
// * @security JWT
// * @return {BannerResponse} 200 - The requested banner entity
// * @return {string} 400 - Validation error
// * @return {string} 404 - Not found error
// * @return {string} 500 - Internal server error
// */
// public async updateBanner(req: RequestWithToken, res: Response): Promise<void> {
// const body = req.body as BannerRequest;
// const { id } = req.params;
// this.logger.trace('Update banner', id, 'by user', req.token.user);
//
// // handle request
// try {
// if (BannerService.verifyBanner(body)) {
// // try patching the banner
// const banner = await BannerService.updateBanner(Number.parseInt(id, 10), body);
// if (banner) {
// res.json(banner);
// } else {
// res.status(404).json('Banner not found.');
// }
// } else {
// res.status(400).json('Invalid banner.');
// }
// } catch (error) {
// this.logger.error('Could not update banner:', error);
// res.status(500).json('Internal server error.');
// }
// }
//
// /**
// * DELETE /banners/{id}
// * @summary Deletes the requested banner
// * @operationId delete
// * @tags banners - Operations of banner controller
// * @param {integer} id.path.required - The id of the banner which should be deleted
// * @security JWT
// * @return {BannerResponse} 200 - The deleted banner entity
// * @return {string} 404 - Not found error
// */
// public async removeBanner(req: RequestWithToken, res: Response): Promise<void> {
// const { id } = req.params;
// this.logger.trace('Remove banner', id, 'by user', req.token.user);
//
// // handle request
// try {
// // check if banner in database
// const banner = await BannerService.deleteBanner(Number.parseInt(id, 10), this.fileService);
// if (banner) {
// res.json(banner);
// } else {
// res.status(404).json('Banner not found.');
// }
// } catch (error) {
// this.logger.error('Could not remove banner:', error);
// res.status(500).json('Internal server error.');
// }
// }
//
// /**
// * GET /banners/active
// * @summary Returns all active banners
// * @operationId getActive
// * @tags banners - Operations of banner controller
// * @security JWT
// * @param {integer} take.query - How many banners the endpoint should return
// * @param {integer} skip.query - How many banners should be skipped (for pagination)
// * @return {PaginatedBannerResponse} 200 - All active banners
// * @return {string} 400 - Validation error
// */
// public async returnActiveBanners(req: RequestWithToken, res: Response): Promise<void> {
// const { body } = req;
// this.logger.trace('Get active banners', body, 'by user', req.token.user);
//
// const { take, skip } = parseRequestPagination(req);
//
// // handle request
// try {
// res.json(await BannerService.getBanners({ active: true }, { take, skip }));
// } catch (error) {
// this.logger.error('Could not return active banners:', error);
// res.status(500).json('Internal server error.');
// }
// }
}