identity-admin 1.5.0 → 1.6.0

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.
Files changed (97) hide show
  1. package/README.md +126 -126
  2. package/lib/Dashboard.d.ts +14 -14
  3. package/lib/Dashboard.js +95 -88
  4. package/lib/container/helpers/HelperInversify.d.ts +6 -6
  5. package/lib/container/helpers/HelperInversify.js +10 -10
  6. package/lib/container/helpers/HelperTypes.d.ts +5 -5
  7. package/lib/container/helpers/HelperTypes.js +7 -7
  8. package/lib/container/index.d.ts +1 -1
  9. package/lib/container/index.js +14 -14
  10. package/lib/container/repositories/RepositoryInversify.d.ts +4 -4
  11. package/lib/container/repositories/RepositoryInversify.js +13 -13
  12. package/lib/container/repositories/RepositoryTypes.d.ts +4 -4
  13. package/lib/container/repositories/RepositoryTypes.js +6 -6
  14. package/lib/container/types.d.ts +8 -8
  15. package/lib/container/types.js +9 -9
  16. package/lib/controllers/ActionController.d.ts +8 -8
  17. package/lib/controllers/ActionController.js +79 -79
  18. package/lib/controllers/DashboardController.d.ts +36 -36
  19. package/lib/controllers/DashboardController.js +349 -337
  20. package/lib/controllers/ResourceController.d.ts +11 -9
  21. package/lib/controllers/ResourceController.js +109 -104
  22. package/lib/helpers/ActionsGenerator.d.ts +9 -9
  23. package/lib/helpers/ActionsGenerator.js +101 -77
  24. package/lib/helpers/FiltersHelper.d.ts +7 -0
  25. package/lib/helpers/FiltersHelper.js +64 -0
  26. package/lib/helpers/LocalesHelper.d.ts +4 -4
  27. package/lib/helpers/LocalesHelper.js +73 -73
  28. package/lib/helpers/ResourceGenerator.d.ts +4 -4
  29. package/lib/helpers/ResourceGenerator.js +110 -68
  30. package/lib/helpers/ResourceHelper.d.ts +23 -22
  31. package/lib/helpers/ResourceHelper.js +305 -286
  32. package/lib/helpers/SchemaGenerator.d.ts +5 -5
  33. package/lib/helpers/SchemaGenerator.js +92 -87
  34. package/lib/helpers/SchemaHelper.d.ts +5 -5
  35. package/lib/helpers/SchemaHelper.js +21 -21
  36. package/lib/locales/ar.json +1 -0
  37. package/lib/locales/en.json +50 -10
  38. package/lib/middlewares/isAuth.d.ts +13 -13
  39. package/lib/middlewares/isAuth.js +43 -43
  40. package/lib/models/ModelNames.d.ts +4 -4
  41. package/lib/models/ModelNames.js +7 -7
  42. package/lib/models/request-log/IRequestLog.d.ts +22 -22
  43. package/lib/models/request-log/IRequestLog.js +2 -2
  44. package/lib/models/request-log/RequestLog.d.ts +3 -3
  45. package/lib/models/request-log/RequestLog.js +51 -51
  46. package/lib/repositories/DashboardRepository.d.ts +5 -5
  47. package/lib/repositories/DashboardRepository.js +12 -12
  48. package/lib/repositories/Repository.d.ts +68 -68
  49. package/lib/repositories/Repository.js +212 -212
  50. package/lib/repositories/RequestLogRepository.d.ts +10 -10
  51. package/lib/repositories/RequestLogRepository.js +54 -54
  52. package/lib/repositories/SaveResult.d.ts +14 -14
  53. package/lib/repositories/SaveResult.js +18 -18
  54. package/lib/router/index.d.ts +8 -8
  55. package/lib/router/index.js +97 -97
  56. package/lib/types/DashbordConfig.d.ts +21 -19
  57. package/lib/types/DashbordConfig.js +2 -2
  58. package/lib/types/IConfigurationFile.d.ts +5 -0
  59. package/lib/types/IConfigurationFile.js +2 -0
  60. package/lib/types/IResourceFile.d.ts +145 -131
  61. package/lib/types/IResourceFile.js +2 -2
  62. package/lib/types/helpers.d.ts +28 -15
  63. package/lib/types/helpers.js +35 -21
  64. package/lib/utils/ResourceUtils.d.ts +2 -2
  65. package/lib/utils/ResourceUtils.js +7 -7
  66. package/lib/utils/ResponseUtils.d.ts +10 -10
  67. package/lib/utils/ResponseUtils.js +38 -38
  68. package/lib/utils/StringUtils.d.ts +9 -9
  69. package/lib/utils/StringUtils.js +46 -46
  70. package/lib/view/asset-manifest.json +13 -13
  71. package/lib/view/assets/icons/delete_icon.svg +3 -3
  72. package/lib/view/assets/icons/flags/ic_flag_cn.svg +9 -9
  73. package/lib/view/assets/icons/flags/ic_flag_sa.svg +9 -9
  74. package/lib/view/assets/icons/flags/ic_flag_vn.svg +9 -9
  75. package/lib/view/assets/icons/info_icon.svg +3 -3
  76. package/lib/view/assets/icons/navbar/ic_banking.svg +5 -5
  77. package/lib/view/assets/icons/navbar/ic_invoice.svg +4 -4
  78. package/lib/view/assets/icons/navbar/ic_kanban.svg +7 -7
  79. package/lib/view/assets/icons/navbar/ic_menu_item.svg +8 -8
  80. package/lib/view/assets/icons/small_info_icon.svg +3 -3
  81. package/lib/view/assets/illustrations/Group 16.svg +4 -4
  82. package/lib/view/assets/illustrations/logo.svg +5 -5
  83. package/lib/view/favicon/browserconfig.xml +9 -9
  84. package/lib/view/favicon/safari-pinned-tab.svg +182 -182
  85. package/lib/view/favicon/site.webmanifest +19 -19
  86. package/lib/view/fonts/index.css +18 -18
  87. package/lib/view/index.html +1 -1
  88. package/lib/view/manifest.json +20 -20
  89. package/lib/view/robots.txt +3 -3
  90. package/lib/view/static/js/{148.b6f24fc9.chunk.js → 148.b05fe2c8.chunk.js} +1 -1
  91. package/lib/view/static/js/{705.b2b55cc3.chunk.js → 705.f86db82e.chunk.js} +1 -1
  92. package/lib/view/static/js/802.3f287a2c.chunk.js +1 -0
  93. package/lib/view/static/js/main.da8f09ec.js +2 -0
  94. package/lib/view/static/js/{main.bc5ec798.js.LICENSE.txt → main.da8f09ec.js.LICENSE.txt} +225 -223
  95. package/package.json +51 -51
  96. package/lib/view/static/js/939.f35326c5.chunk.js +0 -1
  97. package/lib/view/static/js/main.bc5ec798.js +0 -2
@@ -1,337 +1,349 @@
1
- "use strict";
2
- var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
- var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
- if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
- else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
- return c > 3 && r && Object.defineProperty(target, key, r), r;
7
- };
8
- var __param = (this && this.__param) || function (paramIndex, decorator) {
9
- return function (target, key) { decorator(target, key, paramIndex); }
10
- };
11
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
- return new (P || (P = Promise))(function (resolve, reject) {
14
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
- step((generator = generator.apply(thisArg, _arguments || [])).next());
18
- });
19
- };
20
- var __importDefault = (this && this.__importDefault) || function (mod) {
21
- return (mod && mod.__esModule) ? mod : { "default": mod };
22
- };
23
- var DashboardController_1;
24
- Object.defineProperty(exports, "__esModule", { value: true });
25
- const inversify_express_utils_1 = require("inversify-express-utils");
26
- const mongoose_1 = require("mongoose");
27
- const ResponseUtils_1 = __importDefault(require("../utils/ResponseUtils"));
28
- const express_validator_1 = require("express-validator");
29
- const ResourceGenerator_1 = __importDefault(require("../helpers/ResourceGenerator"));
30
- const ResourceHelper_1 = __importDefault(require("../helpers/ResourceHelper"));
31
- const StringUtils_1 = __importDefault(require("../utils/StringUtils"));
32
- const mongoose_2 = __importDefault(require("mongoose"));
33
- const inversify_1 = require("inversify");
34
- const Repository_1 = __importDefault(require("../repositories/Repository"));
35
- const ResourceUtils_1 = require("../utils/ResourceUtils");
36
- let DashboardController = DashboardController_1 = class DashboardController {
37
- constructor(resource, repository, resources) {
38
- this.resource = resource;
39
- this.repository = repository;
40
- this.resources = resources;
41
- if (!repository && resource) {
42
- this.repository = new Repository_1.default(resource.properties.resource);
43
- }
44
- }
45
- validateRequest(req, res) {
46
- const errors = (0, express_validator_1.validationResult)(req);
47
- if (!errors.isEmpty()) {
48
- ResponseUtils_1.default.send(res, 422, errors.array()[0].msg, errors);
49
- return false;
50
- }
51
- return true;
52
- }
53
- paginateParams(request) {
54
- const page = request.query.page ? +request.query.page : DashboardController_1.DEFAULT_PAGE;
55
- const perPage = request.query.perPage ? +request.query.perPage : DashboardController_1.DEFAULT_PER_PAGE;
56
- return { page, perPage };
57
- }
58
- getSearchableSubStringFilter(resource, filter, subString) {
59
- const schema = resource.properties.resource.schema.paths;
60
- const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource);
61
- if (searchBy === '_id') {
62
- if (!mongoose_2.default.isValidObjectId(subString.source)) {
63
- return filter;
64
- }
65
- filter[searchBy] = subString.source;
66
- return filter;
67
- }
68
- filter[searchBy] = subString;
69
- return filter;
70
- }
71
- getScopeFilter(filter, scope) {
72
- return {};
73
- }
74
- getVisibileExtrsActions(currentUser, record, resource, repository, modelName) {
75
- return __awaiter(this, void 0, void 0, function* () {
76
- const data = {
77
- record: record.toObject(),
78
- currentUser,
79
- resource: {
80
- name: modelName,
81
- path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
82
- repository
83
- }
84
- };
85
- if (!resource.properties.actions || !resource.properties.actions.extras) {
86
- return undefined;
87
- }
88
- var extraActionsArray = [];
89
- const extraActions = resource.properties.actions.extras;
90
- for (var i = 0; i < extraActions.length; i++) {
91
- const extraAction = extraActions[i];
92
- if (!extraAction.isVisible) {
93
- break;
94
- }
95
- const isVisible = extraAction.isVisible(data);
96
- if (isVisible) {
97
- extraActionsArray.push(extraAction.key);
98
- }
99
- }
100
- return extraActionsArray;
101
- });
102
- }
103
- index(req, res) {
104
- var _a, _b, _c;
105
- return __awaiter(this, void 0, void 0, function* () {
106
- if (!this.validateRequest(req, res)) {
107
- return;
108
- }
109
- const paginateParams = this.paginateParams(req);
110
- const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
111
- const scope = req.query.scope;
112
- const currentUser = req.user;
113
- const modelName = req.params.resource;
114
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
115
- if (!resource) {
116
- return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
117
- }
118
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
119
- const modifiedResource = ResourceGenerator_1.default.generate(resource);
120
- const sort = req.query.order;
121
- const sortBy = req.query.orderBy;
122
- const sortQuery = {};
123
- sortQuery[sortBy] = sort;
124
- if (sortBy !== '_id') {
125
- sortQuery._id = 'asc';
126
- }
127
- const populate = req.query.populate;
128
- var filter = {};
129
- const crudOperations = resource.properties.crudOperations;
130
- if (crudOperations && crudOperations.index) {
131
- filter = crudOperations.index.before(filter, currentUser);
132
- }
133
- if (scope) {
134
- filter = this.getScopeFilter(filter, scope);
135
- }
136
- if (searchableSubString) {
137
- filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
138
- }
139
- var records = [];
140
- var pageInfo = undefined;
141
- if (!req.query.page && !req.query.perPage) {
142
- records = yield repository.findMany({
143
- sort: sortQuery,
144
- filter,
145
- populate: modifiedResource.properties.populatedString
146
- });
147
- }
148
- else {
149
- const page = yield repository.paginate({
150
- sort: sortQuery,
151
- filter,
152
- paginateParams: paginateParams,
153
- populate: modifiedResource.properties.populatedString
154
- });
155
- records = page.records;
156
- pageInfo = page.pageInfo;
157
- }
158
- var documents = [];
159
- for (var i = 0; i < records.length; i++) {
160
- const record = records[i];
161
- const extraActionKeys = yield this.getVisibileExtrsActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
162
- if (extraActionKeys) {
163
- const recordFlatten = record.toObject();
164
- recordFlatten.extraActionKeys = extraActionKeys;
165
- documents.push(recordFlatten);
166
- }
167
- else {
168
- documents.push(record);
169
- }
170
- }
171
- return ResponseUtils_1.default.send(res, 200, 'OK', {
172
- records: documents,
173
- pageInfo,
174
- //options: modifiedResource
175
- });
176
- });
177
- }
178
- create(req, res) {
179
- var _a, _b, _c;
180
- return __awaiter(this, void 0, void 0, function* () {
181
- if (!this.validateRequest(req, res)) {
182
- return;
183
- }
184
- const modelName = req.params.resource;
185
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
186
- if (!resource) {
187
- return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
188
- }
189
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
190
- const modifiedResource = ResourceGenerator_1.default.generate(resource);
191
- var recordParams = req.body;
192
- const currentUser = req.user;
193
- const crudOperations = resource.properties.crudOperations;
194
- if (crudOperations && crudOperations.create) {
195
- recordParams = crudOperations.create.before(recordParams, currentUser);
196
- }
197
- var record;
198
- if (recordParams.password) {
199
- const user = new mongoose_1.Mongoose.prototype.model(modifiedResource.properties.modelName)(recordParams);
200
- const set = yield user.setPassword(recordParams.password);
201
- record = yield repository.saveInstance(user);
202
- }
203
- else {
204
- record = yield repository.save(recordParams);
205
- }
206
- if (!record.isValid() || !record.document) {
207
- return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', record.getErrors());
208
- }
209
- return ResponseUtils_1.default.created(res, {
210
- record
211
- });
212
- });
213
- }
214
- update(req, res) {
215
- var _a, _b, _c;
216
- return __awaiter(this, void 0, void 0, function* () {
217
- if (!this.validateRequest(req, res)) {
218
- return;
219
- }
220
- const modelName = req.params.resource;
221
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
222
- if (!resource) {
223
- return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
224
- }
225
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
226
- const recordId = req.params.id;
227
- var recordParams = req.body;
228
- var record = yield repository.findById(recordId);
229
- if (!record) {
230
- return ResponseUtils_1.default.send(res, 404, 'record Not Found');
231
- }
232
- const recordSaveResult = yield repository.update(record, recordParams);
233
- if (!recordSaveResult.isValid()) {
234
- return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', recordSaveResult.getErrors());
235
- }
236
- // if (resource.properties.modelName === ModelNames.Settings) {
237
- // await AppSettings.run()
238
- // }
239
- return ResponseUtils_1.default.ok(res, {
240
- record: recordSaveResult
241
- });
242
- });
243
- }
244
- show(req, res) {
245
- var _a, _b, _c, _d, _e;
246
- return __awaiter(this, void 0, void 0, function* () {
247
- if (!this.validateRequest(req, res)) {
248
- return;
249
- }
250
- const modelName = req.params.resource;
251
- const recordId = req.params.id;
252
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
253
- if (!resource) {
254
- return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
255
- }
256
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
257
- const modifiedResource = ResourceGenerator_1.default.generate(resource);
258
- var record = yield repository.findOne({
259
- filter: {
260
- _id: recordId
261
- },
262
- populate: modifiedResource.properties.populatedString
263
- });
264
- if (!record) {
265
- return ResponseUtils_1.default.send(res, 404, 'record not found');
266
- }
267
- record = record.toObject();
268
- //record = await this.getExtras(record._id.toString(), record, this.getExtraRepository())
269
- record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
270
- const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
271
- let extras = {};
272
- if (afterMethod && record) {
273
- extras = yield afterMethod(record);
274
- }
275
- return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null }));
276
- });
277
- }
278
- delete(req, res) {
279
- var _a, _b, _c;
280
- return __awaiter(this, void 0, void 0, function* () {
281
- if (!this.validateRequest(req, res)) {
282
- return;
283
- }
284
- const modelName = req.params.resource;
285
- const recordId = req.params.id;
286
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
287
- if (!resource) {
288
- return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
289
- }
290
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
291
- const record = yield repository.findById(recordId);
292
- if (!record) {
293
- return ResponseUtils_1.default.send(res, 404, 'record Not Found');
294
- }
295
- yield repository.remove({ _id: record._id });
296
- return ResponseUtils_1.default.send(res, 200, 'OK');
297
- });
298
- }
299
- };
300
- DashboardController.DEFAULT_PAGE = 1;
301
- DashboardController.DEFAULT_PER_PAGE = 30;
302
- __decorate([
303
- __param(0, (0, inversify_express_utils_1.request)()),
304
- __param(1, (0, inversify_express_utils_1.response)())
305
- ], DashboardController.prototype, "validateRequest", null);
306
- __decorate([
307
- (0, inversify_express_utils_1.httpGet)('/'),
308
- __param(0, (0, inversify_express_utils_1.request)()),
309
- __param(1, (0, inversify_express_utils_1.response)())
310
- ], DashboardController.prototype, "index", null);
311
- __decorate([
312
- (0, inversify_express_utils_1.httpPost)('/'),
313
- __param(0, (0, inversify_express_utils_1.request)()),
314
- __param(1, (0, inversify_express_utils_1.response)())
315
- ], DashboardController.prototype, "create", null);
316
- __decorate([
317
- (0, inversify_express_utils_1.httpPatch)('/:id'),
318
- __param(0, (0, inversify_express_utils_1.request)()),
319
- __param(1, (0, inversify_express_utils_1.response)())
320
- ], DashboardController.prototype, "update", null);
321
- __decorate([
322
- (0, inversify_express_utils_1.httpGet)('/:id'),
323
- __param(0, (0, inversify_express_utils_1.request)()),
324
- __param(1, (0, inversify_express_utils_1.response)())
325
- ], DashboardController.prototype, "show", null);
326
- __decorate([
327
- (0, inversify_express_utils_1.httpDelete)('/:id'),
328
- __param(0, (0, inversify_express_utils_1.request)()),
329
- __param(1, (0, inversify_express_utils_1.response)())
330
- ], DashboardController.prototype, "delete", null);
331
- DashboardController = DashboardController_1 = __decorate([
332
- (0, inversify_1.injectable)(),
333
- __param(0, (0, inversify_1.unmanaged)()),
334
- __param(1, (0, inversify_1.unmanaged)()),
335
- __param(2, (0, inversify_1.unmanaged)())
336
- ], DashboardController);
337
- exports.default = DashboardController;
1
+ "use strict";
2
+ var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
3
+ var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
4
+ if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
5
+ else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
6
+ return c > 3 && r && Object.defineProperty(target, key, r), r;
7
+ };
8
+ var __param = (this && this.__param) || function (paramIndex, decorator) {
9
+ return function (target, key) { decorator(target, key, paramIndex); }
10
+ };
11
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
12
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
13
+ return new (P || (P = Promise))(function (resolve, reject) {
14
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
15
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
16
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
17
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
18
+ });
19
+ };
20
+ var __importDefault = (this && this.__importDefault) || function (mod) {
21
+ return (mod && mod.__esModule) ? mod : { "default": mod };
22
+ };
23
+ var DashboardController_1;
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ const inversify_express_utils_1 = require("inversify-express-utils");
26
+ const mongoose_1 = require("mongoose");
27
+ const ResponseUtils_1 = __importDefault(require("../utils/ResponseUtils"));
28
+ const express_validator_1 = require("express-validator");
29
+ const ResourceGenerator_1 = __importDefault(require("../helpers/ResourceGenerator"));
30
+ const ResourceHelper_1 = __importDefault(require("../helpers/ResourceHelper"));
31
+ const StringUtils_1 = __importDefault(require("../utils/StringUtils"));
32
+ const mongoose_2 = __importDefault(require("mongoose"));
33
+ const inversify_1 = require("inversify");
34
+ const Repository_1 = __importDefault(require("../repositories/Repository"));
35
+ const ResourceUtils_1 = require("../utils/ResourceUtils");
36
+ const FiltersHelper_1 = __importDefault(require("../helpers/FiltersHelper"));
37
+ let DashboardController = DashboardController_1 = class DashboardController {
38
+ constructor(resource, repository, resources) {
39
+ this.resource = resource;
40
+ this.repository = repository;
41
+ this.resources = resources;
42
+ if (!repository && resource) {
43
+ this.repository = new Repository_1.default(resource.properties.resource);
44
+ }
45
+ }
46
+ validateRequest(req, res) {
47
+ const errors = (0, express_validator_1.validationResult)(req);
48
+ if (!errors.isEmpty()) {
49
+ ResponseUtils_1.default.send(res, 422, errors.array()[0].msg, errors);
50
+ return false;
51
+ }
52
+ return true;
53
+ }
54
+ paginateParams(request) {
55
+ const page = request.query.page ? +request.query.page : DashboardController_1.DEFAULT_PAGE;
56
+ const perPage = request.query.perPage ? +request.query.perPage : DashboardController_1.DEFAULT_PER_PAGE;
57
+ return { page, perPage };
58
+ }
59
+ getSearchableSubStringFilter(resource, filter, subString) {
60
+ const schema = resource.properties.resource.schema.paths;
61
+ const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource);
62
+ if (searchBy === '_id') {
63
+ if (!mongoose_2.default.isValidObjectId(subString.source)) {
64
+ return filter;
65
+ }
66
+ filter[searchBy] = subString.source;
67
+ return filter;
68
+ }
69
+ filter[searchBy] = subString;
70
+ return filter;
71
+ }
72
+ getScopeFilter(filter, scope) {
73
+ return {};
74
+ }
75
+ getVisibileExtrsActions(currentUser, record, resource, repository, modelName) {
76
+ return __awaiter(this, void 0, void 0, function* () {
77
+ const data = {
78
+ record: record.toObject(),
79
+ currentUser,
80
+ resource: {
81
+ name: modelName,
82
+ path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
83
+ repository
84
+ }
85
+ };
86
+ if (!resource.properties.actions || !resource.properties.actions.extras) {
87
+ return undefined;
88
+ }
89
+ var extraActionsArray = [];
90
+ const extraActions = resource.properties.actions.extras;
91
+ for (var i = 0; i < extraActions.length; i++) {
92
+ const extraAction = extraActions[i];
93
+ if (!extraAction.isVisible) {
94
+ break;
95
+ }
96
+ const isVisible = extraAction.isVisible(data);
97
+ if (isVisible) {
98
+ extraActionsArray.push(extraAction.key);
99
+ }
100
+ }
101
+ return extraActionsArray;
102
+ });
103
+ }
104
+ index(req, res) {
105
+ var _a, _b, _c;
106
+ return __awaiter(this, void 0, void 0, function* () {
107
+ if (!this.validateRequest(req, res)) {
108
+ return;
109
+ }
110
+ const paginateParams = this.paginateParams(req);
111
+ const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
112
+ const scope = req.query.scope;
113
+ const currentUser = req.user;
114
+ const modelName = req.params.resource;
115
+ const filtersQuery = req.query.filters;
116
+ const filters = filtersQuery ? filtersQuery.split("^^") : undefined;
117
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
118
+ if (!resource) {
119
+ return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
120
+ }
121
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
122
+ const modifiedResource = ResourceGenerator_1.default.generate(resource);
123
+ const sort = req.query.order;
124
+ const sortBy = req.query.orderBy;
125
+ const sortQuery = {};
126
+ sortQuery[sortBy] = sort;
127
+ if (sortBy !== '_id') {
128
+ sortQuery._id = 'asc';
129
+ }
130
+ const populate = req.query.populate;
131
+ var filter = {};
132
+ const crudOperations = resource.properties.crudOperations;
133
+ if (crudOperations && crudOperations.index) {
134
+ filter = crudOperations.index.before(filter, currentUser);
135
+ }
136
+ if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
137
+ if (resource.properties.filters.scopes.manual) {
138
+ filter = resource.properties.filters.scopes.manual.handler(filter, scope);
139
+ }
140
+ else if (resource.properties.filters.scopes.auto) {
141
+ const key = resource.properties.filters.scopes.auto.key;
142
+ if (key) {
143
+ filter[key] = scope;
144
+ }
145
+ }
146
+ }
147
+ if (searchableSubString) {
148
+ filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
149
+ }
150
+ filter = FiltersHelper_1.default.appendFilters(filter, filters);
151
+ var records = [];
152
+ var pageInfo = undefined;
153
+ if (!req.query.page && !req.query.perPage) {
154
+ records = yield repository.findMany({
155
+ sort: sortQuery,
156
+ filter,
157
+ populate: modifiedResource.properties.populatedString
158
+ });
159
+ }
160
+ else {
161
+ const page = yield repository.paginate({
162
+ sort: sortQuery,
163
+ filter,
164
+ paginateParams: paginateParams,
165
+ populate: modifiedResource.properties.populatedString
166
+ });
167
+ records = page.records;
168
+ pageInfo = page.pageInfo;
169
+ }
170
+ var documents = [];
171
+ for (var i = 0; i < records.length; i++) {
172
+ const record = records[i];
173
+ const extraActionKeys = yield this.getVisibileExtrsActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
174
+ if (extraActionKeys) {
175
+ const recordFlatten = record.toObject();
176
+ recordFlatten.extraActionKeys = extraActionKeys;
177
+ documents.push(recordFlatten);
178
+ }
179
+ else {
180
+ documents.push(record);
181
+ }
182
+ }
183
+ return ResponseUtils_1.default.send(res, 200, 'OK', {
184
+ records: documents,
185
+ pageInfo,
186
+ //options: modifiedResource
187
+ });
188
+ });
189
+ }
190
+ create(req, res) {
191
+ var _a, _b, _c;
192
+ return __awaiter(this, void 0, void 0, function* () {
193
+ if (!this.validateRequest(req, res)) {
194
+ return;
195
+ }
196
+ const modelName = req.params.resource;
197
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
198
+ if (!resource) {
199
+ return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
200
+ }
201
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
202
+ const modifiedResource = ResourceGenerator_1.default.generate(resource);
203
+ var recordParams = req.body;
204
+ const currentUser = req.user;
205
+ const crudOperations = resource.properties.crudOperations;
206
+ if (crudOperations && crudOperations.create) {
207
+ recordParams = crudOperations.create.before(recordParams, currentUser);
208
+ }
209
+ var record;
210
+ if (recordParams.password) {
211
+ const user = new mongoose_1.Mongoose.prototype.model(modifiedResource.properties.modelName)(recordParams);
212
+ const set = yield user.setPassword(recordParams.password);
213
+ record = yield repository.saveInstance(user);
214
+ }
215
+ else {
216
+ record = yield repository.save(recordParams);
217
+ }
218
+ if (!record.isValid() || !record.document) {
219
+ return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', record.getErrors());
220
+ }
221
+ return ResponseUtils_1.default.created(res, {
222
+ record
223
+ });
224
+ });
225
+ }
226
+ update(req, res) {
227
+ var _a, _b, _c;
228
+ return __awaiter(this, void 0, void 0, function* () {
229
+ if (!this.validateRequest(req, res)) {
230
+ return;
231
+ }
232
+ const modelName = req.params.resource;
233
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
234
+ if (!resource) {
235
+ return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
236
+ }
237
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
238
+ const recordId = req.params.id;
239
+ var recordParams = req.body;
240
+ var record = yield repository.findById(recordId);
241
+ if (!record) {
242
+ return ResponseUtils_1.default.send(res, 404, 'record Not Found');
243
+ }
244
+ const recordSaveResult = yield repository.update(record, recordParams);
245
+ if (!recordSaveResult.isValid()) {
246
+ return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', recordSaveResult.getErrors());
247
+ }
248
+ // if (resource.properties.modelName === ModelNames.Settings) {
249
+ // await AppSettings.run()
250
+ // }
251
+ return ResponseUtils_1.default.ok(res, {
252
+ record: recordSaveResult
253
+ });
254
+ });
255
+ }
256
+ show(req, res) {
257
+ var _a, _b, _c, _d, _e;
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ if (!this.validateRequest(req, res)) {
260
+ return;
261
+ }
262
+ const modelName = req.params.resource;
263
+ const recordId = req.params.id;
264
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
265
+ if (!resource) {
266
+ return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
267
+ }
268
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
269
+ const modifiedResource = ResourceGenerator_1.default.generate(resource);
270
+ var record = yield repository.findOne({
271
+ filter: {
272
+ _id: recordId
273
+ },
274
+ populate: modifiedResource.properties.populatedString
275
+ });
276
+ if (!record) {
277
+ return ResponseUtils_1.default.send(res, 404, 'record not found');
278
+ }
279
+ record = record.toObject();
280
+ //record = await this.getExtras(record._id.toString(), record, this.getExtraRepository())
281
+ record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
282
+ const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
283
+ let extras = {};
284
+ if (afterMethod && record) {
285
+ extras = yield afterMethod(record);
286
+ }
287
+ return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null }));
288
+ });
289
+ }
290
+ delete(req, res) {
291
+ var _a, _b, _c;
292
+ return __awaiter(this, void 0, void 0, function* () {
293
+ if (!this.validateRequest(req, res)) {
294
+ return;
295
+ }
296
+ const modelName = req.params.resource;
297
+ const recordId = req.params.id;
298
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
299
+ if (!resource) {
300
+ return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
301
+ }
302
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
303
+ const record = yield repository.findById(recordId);
304
+ if (!record) {
305
+ return ResponseUtils_1.default.send(res, 404, 'record Not Found');
306
+ }
307
+ yield repository.remove({ _id: record._id });
308
+ return ResponseUtils_1.default.send(res, 200, 'OK');
309
+ });
310
+ }
311
+ };
312
+ DashboardController.DEFAULT_PAGE = 1;
313
+ DashboardController.DEFAULT_PER_PAGE = 30;
314
+ __decorate([
315
+ __param(0, (0, inversify_express_utils_1.request)()),
316
+ __param(1, (0, inversify_express_utils_1.response)())
317
+ ], DashboardController.prototype, "validateRequest", null);
318
+ __decorate([
319
+ (0, inversify_express_utils_1.httpGet)('/'),
320
+ __param(0, (0, inversify_express_utils_1.request)()),
321
+ __param(1, (0, inversify_express_utils_1.response)())
322
+ ], DashboardController.prototype, "index", null);
323
+ __decorate([
324
+ (0, inversify_express_utils_1.httpPost)('/'),
325
+ __param(0, (0, inversify_express_utils_1.request)()),
326
+ __param(1, (0, inversify_express_utils_1.response)())
327
+ ], DashboardController.prototype, "create", null);
328
+ __decorate([
329
+ (0, inversify_express_utils_1.httpPatch)('/:id'),
330
+ __param(0, (0, inversify_express_utils_1.request)()),
331
+ __param(1, (0, inversify_express_utils_1.response)())
332
+ ], DashboardController.prototype, "update", null);
333
+ __decorate([
334
+ (0, inversify_express_utils_1.httpGet)('/:id'),
335
+ __param(0, (0, inversify_express_utils_1.request)()),
336
+ __param(1, (0, inversify_express_utils_1.response)())
337
+ ], DashboardController.prototype, "show", null);
338
+ __decorate([
339
+ (0, inversify_express_utils_1.httpDelete)('/:id'),
340
+ __param(0, (0, inversify_express_utils_1.request)()),
341
+ __param(1, (0, inversify_express_utils_1.response)())
342
+ ], DashboardController.prototype, "delete", null);
343
+ DashboardController = DashboardController_1 = __decorate([
344
+ (0, inversify_1.injectable)(),
345
+ __param(0, (0, inversify_1.unmanaged)()),
346
+ __param(1, (0, inversify_1.unmanaged)()),
347
+ __param(2, (0, inversify_1.unmanaged)())
348
+ ], DashboardController);
349
+ exports.default = DashboardController;