identity-admin 1.25.22 → 1.25.24
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -1,9 +1,9 @@
|
|
|
1
|
-
import { Response } from
|
|
2
|
-
import { Document } from
|
|
3
|
-
import { IResourceFile } from
|
|
4
|
-
import { IDashboardRepository } from
|
|
5
|
-
import { IRequest } from
|
|
6
|
-
import { IModelConfigurationDocument } from
|
|
1
|
+
import { Response } from 'express';
|
|
2
|
+
import { Document } from 'mongoose';
|
|
3
|
+
import { IResourceFile } from '../types/IResourceFile';
|
|
4
|
+
import { IDashboardRepository } from '../repositories/Repository';
|
|
5
|
+
import { IRequest } from '../middlewares/isAuth';
|
|
6
|
+
import { IModelConfigurationDocument } from '../models/modelConfiguration/IModelConfigurations';
|
|
7
7
|
export interface PaginateParams {
|
|
8
8
|
page: number;
|
|
9
9
|
perPage: number;
|
|
@@ -31,6 +31,7 @@ export default class DashboardController {
|
|
|
31
31
|
[key: string]: any;
|
|
32
32
|
};
|
|
33
33
|
protected getVisibileExtraActions(currentUser: Document, record: any, resource: IResourceFile, repository: any, modelName: string): Promise<string[] | undefined>;
|
|
34
|
+
private getModelConfiguration;
|
|
34
35
|
index(req: IRequest, res: Response): Promise<void | Response<any, Record<string, any>>>;
|
|
35
36
|
create(req: IRequest, res: Response): Promise<void>;
|
|
36
37
|
update(req: IRequest, res: Response): Promise<void | Response<any, Record<string, any>>>;
|
|
@@ -57,19 +57,15 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
57
57
|
return true;
|
|
58
58
|
}
|
|
59
59
|
paginateParams(request) {
|
|
60
|
-
const page = request.query.page
|
|
61
|
-
|
|
62
|
-
: DashboardController_1.DEFAULT_PAGE;
|
|
63
|
-
const perPage = request.query.perPage
|
|
64
|
-
? +request.query.perPage
|
|
65
|
-
: DashboardController_1.DEFAULT_PER_PAGE;
|
|
60
|
+
const page = request.query.page ? +request.query.page : DashboardController_1.DEFAULT_PAGE;
|
|
61
|
+
const perPage = request.query.perPage ? +request.query.perPage : DashboardController_1.DEFAULT_PER_PAGE;
|
|
66
62
|
return { page, perPage };
|
|
67
63
|
}
|
|
68
64
|
getSearchableSubStringFilter(resource, filter, subString) {
|
|
69
65
|
var _a;
|
|
70
66
|
const schema = resource.properties.resource.schema.paths;
|
|
71
67
|
const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource, (_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(resource.properties.modelName));
|
|
72
|
-
if (searchBy ===
|
|
68
|
+
if (searchBy === '_id') {
|
|
73
69
|
if (!mongoose_1.default.isValidObjectId(subString.source)) {
|
|
74
70
|
return filter;
|
|
75
71
|
}
|
|
@@ -77,10 +73,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
77
73
|
return filter;
|
|
78
74
|
}
|
|
79
75
|
if (LocalizedStringHelper_1.default.checkLocalizedStringType(resource, searchBy)) {
|
|
80
|
-
filter.$or = [
|
|
81
|
-
LocalizedStringHelper_1.default.getFilterObject(searchBy, "0", subString),
|
|
82
|
-
LocalizedStringHelper_1.default.getFilterObject(searchBy, "1", subString),
|
|
83
|
-
];
|
|
76
|
+
filter.$or = [LocalizedStringHelper_1.default.getFilterObject(searchBy, '0', subString), LocalizedStringHelper_1.default.getFilterObject(searchBy, '1', subString)];
|
|
84
77
|
}
|
|
85
78
|
else {
|
|
86
79
|
filter[searchBy] = subString;
|
|
@@ -120,6 +113,16 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
120
113
|
return extraActionsArray;
|
|
121
114
|
});
|
|
122
115
|
}
|
|
116
|
+
getModelConfiguration(modelName) {
|
|
117
|
+
var _a, _b;
|
|
118
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
119
|
+
if ((_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(modelName)) {
|
|
120
|
+
return (_b = this.modelConfigurations) === null || _b === void 0 ? void 0 : _b.get(modelName);
|
|
121
|
+
}
|
|
122
|
+
const collection = mongoose_1.default.connection.db.collection('modelconfigurations');
|
|
123
|
+
return yield collection.findOne({ modelName: modelName });
|
|
124
|
+
});
|
|
125
|
+
}
|
|
123
126
|
index(req, res) {
|
|
124
127
|
var _a, _b, _c;
|
|
125
128
|
return __awaiter(this, void 0, void 0, function* () {
|
|
@@ -127,26 +130,20 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
127
130
|
return;
|
|
128
131
|
}
|
|
129
132
|
const paginateParams = this.paginateParams(req);
|
|
130
|
-
const searchableSubString = req.query.filter
|
|
131
|
-
? new RegExp(req.query.filter, "i")
|
|
132
|
-
: undefined;
|
|
133
|
+
const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
|
|
133
134
|
const scope = req.query.scope;
|
|
134
135
|
const currentUser = req.user;
|
|
135
136
|
const modelName = req.params.resource;
|
|
136
137
|
const filtersQuery = req.query.filters;
|
|
137
|
-
const filters = filtersQuery
|
|
138
|
-
? filtersQuery.split("^^")
|
|
139
|
-
: undefined;
|
|
138
|
+
const filters = filtersQuery ? filtersQuery.split('^^') : undefined;
|
|
140
139
|
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
141
140
|
if (!resource) {
|
|
142
|
-
return ResponseUtils_1.default.notFound(res,
|
|
141
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
143
142
|
}
|
|
144
143
|
if (!currentUser) {
|
|
145
144
|
return ResponseUtils_1.default.unauthorized(res);
|
|
146
145
|
}
|
|
147
|
-
const permissionCheck = resource.properties.isAllowed
|
|
148
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
149
|
-
: true;
|
|
146
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
150
147
|
if (!permissionCheck) {
|
|
151
148
|
return ResponseUtils_1.default.forbidden(res);
|
|
152
149
|
}
|
|
@@ -156,14 +153,11 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
156
153
|
const sortBy = req.query.orderBy;
|
|
157
154
|
const sortQuery = {};
|
|
158
155
|
sortQuery[sortBy] = sort;
|
|
159
|
-
if (sortBy !==
|
|
160
|
-
sortQuery._id =
|
|
156
|
+
if (sortBy !== '_id') {
|
|
157
|
+
sortQuery._id = 'asc';
|
|
161
158
|
}
|
|
162
159
|
var filter = {};
|
|
163
|
-
if (scope &&
|
|
164
|
-
resource.properties.filters &&
|
|
165
|
-
resource.properties.filters.scopes &&
|
|
166
|
-
resource.properties.filters.scopes.isAccessible) {
|
|
160
|
+
if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
|
|
167
161
|
if (resource.properties.filters.scopes.manual) {
|
|
168
162
|
filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
|
|
169
163
|
}
|
|
@@ -229,7 +223,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
229
223
|
if (crudOperations && crudOperations.index && crudOperations.index.after) {
|
|
230
224
|
documents = yield crudOperations.index.after(req, documents, currentUser);
|
|
231
225
|
}
|
|
232
|
-
return ResponseUtils_1.default.send(res, 200,
|
|
226
|
+
return ResponseUtils_1.default.send(res, 200, 'OK', {
|
|
233
227
|
records: documents,
|
|
234
228
|
pageInfo,
|
|
235
229
|
//options: modifiedResource
|
|
@@ -246,14 +240,12 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
246
240
|
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
247
241
|
const currentUser = req.user;
|
|
248
242
|
if (!resource) {
|
|
249
|
-
return ResponseUtils_1.default.notFound(res,
|
|
243
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
250
244
|
}
|
|
251
245
|
if (!currentUser) {
|
|
252
246
|
return ResponseUtils_1.default.unauthorized(res);
|
|
253
247
|
}
|
|
254
|
-
const permissionCheck = resource.properties.isAllowed
|
|
255
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
256
|
-
: true;
|
|
248
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
257
249
|
if (!permissionCheck) {
|
|
258
250
|
return ResponseUtils_1.default.forbidden(res);
|
|
259
251
|
}
|
|
@@ -266,9 +258,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
266
258
|
var recordParams = req.body;
|
|
267
259
|
recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
|
|
268
260
|
const crudOperations = resource.properties.crudOperations;
|
|
269
|
-
if (crudOperations &&
|
|
270
|
-
crudOperations.create &&
|
|
271
|
-
crudOperations.create.before) {
|
|
261
|
+
if (crudOperations && crudOperations.create && crudOperations.create.before) {
|
|
272
262
|
recordParams = yield crudOperations.create.before(req, recordParams, currentUser);
|
|
273
263
|
}
|
|
274
264
|
if (crudOperations && crudOperations.create && crudOperations.create.validators) {
|
|
@@ -291,11 +281,9 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
291
281
|
record = yield repository.save(recordParams);
|
|
292
282
|
}
|
|
293
283
|
if (!record.isValid() || !record.document) {
|
|
294
|
-
return ResponseUtils_1.default.unprocessable(res,
|
|
284
|
+
return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', record.getErrors());
|
|
295
285
|
}
|
|
296
|
-
if (crudOperations &&
|
|
297
|
-
crudOperations.create &&
|
|
298
|
-
crudOperations.create.after) {
|
|
286
|
+
if (crudOperations && crudOperations.create && crudOperations.create.after) {
|
|
299
287
|
record = yield crudOperations.create.after(req, record, currentUser, recordParams);
|
|
300
288
|
}
|
|
301
289
|
return ResponseUtils_1.default.created(res, {
|
|
@@ -312,16 +300,14 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
312
300
|
const modelName = req.params.resource;
|
|
313
301
|
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
314
302
|
if (!resource) {
|
|
315
|
-
return ResponseUtils_1.default.notFound(res,
|
|
303
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
316
304
|
}
|
|
317
305
|
var recordParams = req.body;
|
|
318
306
|
const currentUser = req.user;
|
|
319
307
|
if (!currentUser) {
|
|
320
308
|
return ResponseUtils_1.default.unauthorized(res);
|
|
321
309
|
}
|
|
322
|
-
const permissionCheck = resource.properties.isAllowed
|
|
323
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
324
|
-
: true;
|
|
310
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
325
311
|
if (!permissionCheck) {
|
|
326
312
|
return ResponseUtils_1.default.forbidden(res);
|
|
327
313
|
}
|
|
@@ -334,12 +320,10 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
334
320
|
const recordId = req.params.id;
|
|
335
321
|
var record = yield repository.findById(recordId);
|
|
336
322
|
if (!record) {
|
|
337
|
-
return ResponseUtils_1.default.send(res, 404,
|
|
323
|
+
return ResponseUtils_1.default.send(res, 404, 'record Not Found');
|
|
338
324
|
}
|
|
339
325
|
const crudOperations = resource.properties.crudOperations;
|
|
340
|
-
if (crudOperations &&
|
|
341
|
-
crudOperations.update &&
|
|
342
|
-
crudOperations.update.before) {
|
|
326
|
+
if (crudOperations && crudOperations.update && crudOperations.update.before) {
|
|
343
327
|
recordParams = yield crudOperations.update.before(req, recordParams, currentUser);
|
|
344
328
|
}
|
|
345
329
|
if (crudOperations && crudOperations.update && crudOperations.update.validators) {
|
|
@@ -359,14 +343,12 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
359
343
|
recordSaveResult = yield repository.update(record, recordParams);
|
|
360
344
|
}
|
|
361
345
|
if (!recordSaveResult.isValid()) {
|
|
362
|
-
return ResponseUtils_1.default.unprocessable(res,
|
|
346
|
+
return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', recordSaveResult.getErrors());
|
|
363
347
|
}
|
|
364
348
|
// if (resource.properties.modelName === ModelNames.Settings) {
|
|
365
349
|
// await AppSettings.run()
|
|
366
350
|
// }
|
|
367
|
-
if (crudOperations &&
|
|
368
|
-
crudOperations.update &&
|
|
369
|
-
crudOperations.update.after) {
|
|
351
|
+
if (crudOperations && crudOperations.update && crudOperations.update.after) {
|
|
370
352
|
recordSaveResult = yield crudOperations.update.after(req, recordSaveResult, recordParams, currentUser);
|
|
371
353
|
}
|
|
372
354
|
return ResponseUtils_1.default.ok(res, {
|
|
@@ -380,26 +362,20 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
380
362
|
if (!this.validateRequest(req, res)) {
|
|
381
363
|
return;
|
|
382
364
|
}
|
|
383
|
-
const searchableSubString = req.query.filter
|
|
384
|
-
? new RegExp(req.query.filter, "i")
|
|
385
|
-
: undefined;
|
|
365
|
+
const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
|
|
386
366
|
const scope = req.query.scope;
|
|
387
367
|
const currentUser = req.user;
|
|
388
368
|
const modelName = req.params.resource;
|
|
389
369
|
const filtersQuery = req.query.filters;
|
|
390
|
-
const filters = filtersQuery
|
|
391
|
-
? filtersQuery.split("^^")
|
|
392
|
-
: undefined;
|
|
370
|
+
const filters = filtersQuery ? filtersQuery.split('^^') : undefined;
|
|
393
371
|
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
394
372
|
if (!resource) {
|
|
395
|
-
return ResponseUtils_1.default.notFound(res,
|
|
373
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
396
374
|
}
|
|
397
375
|
if (!currentUser) {
|
|
398
376
|
return ResponseUtils_1.default.unauthorized(res);
|
|
399
377
|
}
|
|
400
|
-
const permissionCheck = resource.properties.isAllowed
|
|
401
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
402
|
-
: true;
|
|
378
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
403
379
|
if (!permissionCheck) {
|
|
404
380
|
return ResponseUtils_1.default.forbidden(res);
|
|
405
381
|
}
|
|
@@ -409,14 +385,11 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
409
385
|
const sortBy = req.query.orderBy;
|
|
410
386
|
const sortQuery = {};
|
|
411
387
|
sortQuery[sortBy] = sort;
|
|
412
|
-
if (sortBy !==
|
|
413
|
-
sortQuery._id =
|
|
388
|
+
if (sortBy !== '_id') {
|
|
389
|
+
sortQuery._id = 'asc';
|
|
414
390
|
}
|
|
415
391
|
var filter = {};
|
|
416
|
-
if (scope &&
|
|
417
|
-
resource.properties.filters &&
|
|
418
|
-
resource.properties.filters.scopes &&
|
|
419
|
-
resource.properties.filters.scopes.isAccessible) {
|
|
392
|
+
if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
|
|
420
393
|
if (resource.properties.filters.scopes.manual) {
|
|
421
394
|
filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
|
|
422
395
|
}
|
|
@@ -454,7 +427,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
454
427
|
if (crudOperations && crudOperations.index && crudOperations.index.after) {
|
|
455
428
|
documents = yield crudOperations.index.after(req, documents, currentUser);
|
|
456
429
|
}
|
|
457
|
-
const fields = req.query.fields.map(v => JSON.parse(v));
|
|
430
|
+
const fields = req.query.fields.map((v) => JSON.parse(v));
|
|
458
431
|
const fileType = req.query.fileType;
|
|
459
432
|
if (fileType === 'xlsx') {
|
|
460
433
|
const buffer = ReportsGenerator_1.default.CreateXlsxFile(fields, documents, modelName);
|
|
@@ -488,14 +461,12 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
488
461
|
const currentUser = req.user;
|
|
489
462
|
const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
|
|
490
463
|
if (!resource) {
|
|
491
|
-
return ResponseUtils_1.default.notFound(res,
|
|
464
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
492
465
|
}
|
|
493
466
|
if (!currentUser) {
|
|
494
467
|
return ResponseUtils_1.default.unauthorized(res);
|
|
495
468
|
}
|
|
496
|
-
const permissionCheck = resource.properties.isAllowed
|
|
497
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
498
|
-
: true;
|
|
469
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
499
470
|
if (!permissionCheck) {
|
|
500
471
|
return ResponseUtils_1.default.forbidden(res);
|
|
501
472
|
}
|
|
@@ -511,7 +482,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
511
482
|
populate: modifiedPopulatedObject,
|
|
512
483
|
});
|
|
513
484
|
if (!record) {
|
|
514
|
-
return ResponseUtils_1.default.send(res, 404,
|
|
485
|
+
return ResponseUtils_1.default.send(res, 404, 'record not found');
|
|
515
486
|
}
|
|
516
487
|
record = record.toObject();
|
|
517
488
|
//record = await this.getExtras(record._id.toString(), record, this.getExtraRepository())
|
|
@@ -525,7 +496,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
525
496
|
});
|
|
526
497
|
}
|
|
527
498
|
deleteAll(req, res) {
|
|
528
|
-
var _a, _b, _c
|
|
499
|
+
var _a, _b, _c;
|
|
529
500
|
return __awaiter(this, void 0, void 0, function* () {
|
|
530
501
|
if (!this.validateRequest(req, res)) {
|
|
531
502
|
return;
|
|
@@ -534,24 +505,23 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
534
505
|
const recordIds = req.body;
|
|
535
506
|
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
536
507
|
if (!resource) {
|
|
537
|
-
return ResponseUtils_1.default.notFound(res,
|
|
508
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
538
509
|
}
|
|
539
510
|
const currentUser = req.user;
|
|
540
511
|
if (!currentUser) {
|
|
541
512
|
return ResponseUtils_1.default.unauthorized(res);
|
|
542
513
|
}
|
|
543
|
-
const permissionCheck = resource.properties.isAllowed
|
|
544
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
545
|
-
: true;
|
|
514
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
546
515
|
if (!permissionCheck) {
|
|
547
516
|
return ResponseUtils_1.default.forbidden(res);
|
|
548
517
|
}
|
|
549
|
-
const
|
|
518
|
+
const configuration = yield this.getModelConfiguration(resource.properties.modelName);
|
|
519
|
+
const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, configuration);
|
|
550
520
|
const bulkDeletePermission = actions.bulkDelete.isAccessible;
|
|
551
521
|
if (!bulkDeletePermission) {
|
|
552
522
|
return ResponseUtils_1.default.forbidden(res);
|
|
553
523
|
}
|
|
554
|
-
const repository = (
|
|
524
|
+
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
555
525
|
for (var i = 0; i < recordIds.length; i++) {
|
|
556
526
|
const recordId = recordIds[i];
|
|
557
527
|
const record = yield repository.findById(recordId);
|
|
@@ -560,7 +530,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
560
530
|
}
|
|
561
531
|
yield repository.remove({ _id: record._id });
|
|
562
532
|
}
|
|
563
|
-
return ResponseUtils_1.default.send(res, 200,
|
|
533
|
+
return ResponseUtils_1.default.send(res, 200, 'OK');
|
|
564
534
|
});
|
|
565
535
|
}
|
|
566
536
|
delete(req, res) {
|
|
@@ -573,15 +543,13 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
573
543
|
const recordId = req.params.id;
|
|
574
544
|
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
575
545
|
if (!resource) {
|
|
576
|
-
return ResponseUtils_1.default.notFound(res,
|
|
546
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
577
547
|
}
|
|
578
548
|
const currentUser = req.user;
|
|
579
549
|
if (!currentUser) {
|
|
580
550
|
return ResponseUtils_1.default.unauthorized(res);
|
|
581
551
|
}
|
|
582
|
-
const permissionCheck = resource.properties.isAllowed
|
|
583
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
584
|
-
: true;
|
|
552
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
585
553
|
if (!permissionCheck) {
|
|
586
554
|
return ResponseUtils_1.default.forbidden(res);
|
|
587
555
|
}
|
|
@@ -593,10 +561,10 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
593
561
|
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
594
562
|
const record = yield repository.findById(recordId);
|
|
595
563
|
if (!record) {
|
|
596
|
-
return ResponseUtils_1.default.send(res, 404,
|
|
564
|
+
return ResponseUtils_1.default.send(res, 404, 'record Not Found');
|
|
597
565
|
}
|
|
598
566
|
yield repository.remove({ _id: record._id });
|
|
599
|
-
return ResponseUtils_1.default.send(res, 200,
|
|
567
|
+
return ResponseUtils_1.default.send(res, 200, 'OK');
|
|
600
568
|
});
|
|
601
569
|
}
|
|
602
570
|
getNeighbors(req, res) {
|
|
@@ -612,20 +580,17 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
612
580
|
const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
|
|
613
581
|
const neighborRecordType = req.query.type;
|
|
614
582
|
if (!resource || !neighborRecordType) {
|
|
615
|
-
return ResponseUtils_1.default.notFound(res,
|
|
583
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
616
584
|
}
|
|
617
585
|
if (!currentUser) {
|
|
618
586
|
return ResponseUtils_1.default.unauthorized(res);
|
|
619
587
|
}
|
|
620
|
-
const permissionCheck = resource.properties.isAllowed
|
|
621
|
-
? yield resource.properties.isAllowed(currentUser)
|
|
622
|
-
: true;
|
|
588
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
623
589
|
if (!permissionCheck) {
|
|
624
590
|
return ResponseUtils_1.default.forbidden(res);
|
|
625
591
|
}
|
|
626
|
-
if (neighborRecordType !== helpers_1.NeighborTypes.NEXT &&
|
|
627
|
-
|
|
628
|
-
return ResponseUtils_1.default.notFound(res, "Type in query should match NEXT or PREVIOUS", []);
|
|
592
|
+
if (neighborRecordType !== helpers_1.NeighborTypes.NEXT && neighborRecordType !== helpers_1.NeighborTypes.PREVIOUS) {
|
|
593
|
+
return ResponseUtils_1.default.notFound(res, 'Type in query should match NEXT or PREVIOUS', []);
|
|
629
594
|
}
|
|
630
595
|
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
631
596
|
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
@@ -639,12 +604,11 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
639
604
|
populate: modifiedPopulatedObject,
|
|
640
605
|
});
|
|
641
606
|
if (!record) {
|
|
642
|
-
return ResponseUtils_1.default.send(res, 404,
|
|
607
|
+
return ResponseUtils_1.default.send(res, 404, 'record not found');
|
|
643
608
|
}
|
|
644
609
|
record = record.toObject();
|
|
645
610
|
record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
|
|
646
|
-
const sortBy = modifiedResource.properties
|
|
647
|
-
.defaultOrderBy;
|
|
611
|
+
const sortBy = modifiedResource.properties.defaultOrderBy;
|
|
648
612
|
const sort = modifiedResource.properties.defaultOrder;
|
|
649
613
|
const nextQuery = {};
|
|
650
614
|
const prevQuery = {};
|
|
@@ -656,18 +620,21 @@ let DashboardController = DashboardController_1 = class DashboardController {
|
|
|
656
620
|
prevQuery[sortBy] = sort === 'asc' ? lessThanQuery : greaterThanQuery;
|
|
657
621
|
nextSortQuery[sortBy] = sort;
|
|
658
622
|
previousSortQuery[sortBy] = sort === 'asc' ? 'desc' : 'asc';
|
|
623
|
+
const handler = resource.properties.crudOperations && resource.properties.crudOperations.show && resource.properties.crudOperations.show.nextPreviousButtonHandler
|
|
624
|
+
? resource.properties.crudOperations.show.nextPreviousButtonHandler
|
|
625
|
+
: undefined;
|
|
659
626
|
var neighbor;
|
|
660
627
|
if (neighborRecordType === helpers_1.NeighborTypes.NEXT) {
|
|
661
|
-
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(nextQuery).sort(nextSortQuery).limit(1));
|
|
628
|
+
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(handler ? handler(req, nextQuery, currentUser, neighborRecordType) : nextQuery).sort(nextSortQuery).limit(1));
|
|
662
629
|
}
|
|
663
630
|
else {
|
|
664
|
-
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(prevQuery).sort(previousSortQuery).limit(1));
|
|
631
|
+
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(handler ? handler(req, prevQuery, currentUser, neighborRecordType) : prevQuery).sort(previousSortQuery).limit(1));
|
|
665
632
|
}
|
|
666
633
|
const hasNeighbor = record && neighbor && neighbor._id ? true : false;
|
|
667
634
|
const neighborRecordId = hasNeighbor ? neighbor._id : undefined;
|
|
668
635
|
return ResponseUtils_1.default.ok(res, {
|
|
669
636
|
hasNeighbor,
|
|
670
|
-
neighborRecordId
|
|
637
|
+
neighborRecordId,
|
|
671
638
|
});
|
|
672
639
|
});
|
|
673
640
|
}
|
|
@@ -684,37 +651,37 @@ __decorate([
|
|
|
684
651
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
685
652
|
], DashboardController.prototype, "index", null);
|
|
686
653
|
__decorate([
|
|
687
|
-
(0, inversify_express_utils_1.httpPost)(
|
|
654
|
+
(0, inversify_express_utils_1.httpPost)('/'),
|
|
688
655
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
689
656
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
690
657
|
], DashboardController.prototype, "create", null);
|
|
691
658
|
__decorate([
|
|
692
|
-
(0, inversify_express_utils_1.httpPatch)(
|
|
659
|
+
(0, inversify_express_utils_1.httpPatch)('/:id'),
|
|
693
660
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
694
661
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
695
662
|
], DashboardController.prototype, "update", null);
|
|
696
663
|
__decorate([
|
|
697
|
-
(0, inversify_express_utils_1.httpGet)(
|
|
664
|
+
(0, inversify_express_utils_1.httpGet)('/report'),
|
|
698
665
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
699
666
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
700
667
|
], DashboardController.prototype, "report", null);
|
|
701
668
|
__decorate([
|
|
702
|
-
(0, inversify_express_utils_1.httpGet)(
|
|
669
|
+
(0, inversify_express_utils_1.httpGet)('/:id'),
|
|
703
670
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
704
671
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
705
672
|
], DashboardController.prototype, "show", null);
|
|
706
673
|
__decorate([
|
|
707
|
-
(0, inversify_express_utils_1.httpDelete)(
|
|
674
|
+
(0, inversify_express_utils_1.httpDelete)('/all'),
|
|
708
675
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
709
676
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
710
677
|
], DashboardController.prototype, "deleteAll", null);
|
|
711
678
|
__decorate([
|
|
712
|
-
(0, inversify_express_utils_1.httpDelete)(
|
|
679
|
+
(0, inversify_express_utils_1.httpDelete)('/:id'),
|
|
713
680
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
714
681
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
715
682
|
], DashboardController.prototype, "delete", null);
|
|
716
683
|
__decorate([
|
|
717
|
-
(0, inversify_express_utils_1.httpGet)(
|
|
684
|
+
(0, inversify_express_utils_1.httpGet)('/neighbors/:id'),
|
|
718
685
|
__param(0, (0, inversify_express_utils_1.request)()),
|
|
719
686
|
__param(1, (0, inversify_express_utils_1.response)())
|
|
720
687
|
], DashboardController.prototype, "getNeighbors", null);
|
|
@@ -3,7 +3,7 @@ import { Document, Model } from 'mongoose';
|
|
|
3
3
|
import { IRequest } from '../middlewares/isAuth';
|
|
4
4
|
import { IRepository, PageInfo, PaginateParams } from '../repositories/Repository';
|
|
5
5
|
import SaveResult from '../repositories/SaveResult';
|
|
6
|
-
import { ActionNames, ActionTypes, FieldTypes, FileTypes, HandlerStrategy, Virtuals } from './helpers';
|
|
6
|
+
import { ActionNames, ActionTypes, FieldTypes, FileTypes, HandlerStrategy, NeighborTypes, Virtuals } from './helpers';
|
|
7
7
|
declare type orderTypes = 'asc' | 'desc';
|
|
8
8
|
declare type VirtualFieldTypes = 'password' | 'ref' | 'Array';
|
|
9
9
|
declare type Severity = 'success' | 'info' | 'warning' | 'error';
|
|
@@ -136,6 +136,15 @@ interface ICrudOperations {
|
|
|
136
136
|
record: Document;
|
|
137
137
|
[key: string]: any;
|
|
138
138
|
}>;
|
|
139
|
+
/**
|
|
140
|
+
* A function that is called before getting the next or the previous record. It gives you the access to the filter object.
|
|
141
|
+
* @returns The filter object
|
|
142
|
+
*/
|
|
143
|
+
nextPreviousButtonHandler?: (req: IRequest, filter: {
|
|
144
|
+
[key: string]: any;
|
|
145
|
+
}, currentUser: Document, direction: NeighborTypes) => Promise<{
|
|
146
|
+
[key: string]: any;
|
|
147
|
+
}>;
|
|
139
148
|
};
|
|
140
149
|
}
|
|
141
150
|
export interface ActionData {
|