identity-admin 1.25.16 → 1.25.17

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 (141) hide show
  1. package/README.md +126 -126
  2. package/lib/Dashboard.d.ts +14 -14
  3. package/lib/Dashboard.js +95 -95
  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 +41 -41
  19. package/lib/controllers/DashboardController.js +602 -595
  20. package/lib/controllers/ResourceController.d.ts +15 -15
  21. package/lib/controllers/ResourceController.js +132 -132
  22. package/lib/helpers/ActionsGenerator.d.ts +11 -11
  23. package/lib/helpers/ActionsGenerator.js +175 -175
  24. package/lib/helpers/FiltersHelper.d.ts +9 -9
  25. package/lib/helpers/FiltersHelper.js +105 -105
  26. package/lib/helpers/LocalesHelper.d.ts +4 -4
  27. package/lib/helpers/LocalesHelper.js +73 -73
  28. package/lib/helpers/LocalizedStringHelper.d.ts +9 -9
  29. package/lib/helpers/LocalizedStringHelper.js +57 -57
  30. package/lib/helpers/Permissions/PermissionFetcher.d.ts +7 -7
  31. package/lib/helpers/Permissions/PermissionFetcher.js +78 -78
  32. package/lib/helpers/Permissions/PermissionLabel.d.ts +2 -2
  33. package/lib/helpers/Permissions/PermissionLabel.js +42 -42
  34. package/lib/helpers/Permissions/PermissionMapper.d.ts +13 -13
  35. package/lib/helpers/Permissions/PermissionMapper.js +35 -35
  36. package/lib/helpers/Permissions/PermissionResource.d.ts +5 -5
  37. package/lib/helpers/Permissions/PermissionResource.js +31 -31
  38. package/lib/helpers/Permissions/PermissionSavingService.d.ts +13 -13
  39. package/lib/helpers/Permissions/PermissionSavingService.js +32 -32
  40. package/lib/helpers/Permissions/SavePermissionGroup.d.ts +6 -6
  41. package/lib/helpers/Permissions/SavePermissionGroup.js +32 -32
  42. package/lib/helpers/Permissions/UpdatePermissionGroup.d.ts +6 -6
  43. package/lib/helpers/Permissions/UpdatePermissionGroup.js +50 -50
  44. package/lib/helpers/Permissions/types.d.ts +17 -17
  45. package/lib/helpers/Permissions/types.js +2 -2
  46. package/lib/helpers/PopulationHelper.d.ts +12 -12
  47. package/lib/helpers/PopulationHelper.js +50 -50
  48. package/lib/helpers/ResourceGenerator.d.ts +9 -9
  49. package/lib/helpers/ResourceGenerator.js +172 -172
  50. package/lib/helpers/ResourceHelper.d.ts +28 -28
  51. package/lib/helpers/ResourceHelper.js +376 -376
  52. package/lib/helpers/SchemaGenerator.d.ts +5 -5
  53. package/lib/helpers/SchemaGenerator.js +97 -97
  54. package/lib/helpers/SchemaHelper.d.ts +5 -5
  55. package/lib/helpers/SchemaHelper.js +21 -21
  56. package/lib/locales/en.json +53 -53
  57. package/lib/middlewares/isAuth.d.ts +13 -13
  58. package/lib/middlewares/isAuth.js +43 -43
  59. package/lib/models/ModelNames.d.ts +7 -7
  60. package/lib/models/ModelNames.js +10 -10
  61. package/lib/models/adminpermission/AdminPermission.d.ts +6 -6
  62. package/lib/models/adminpermission/AdminPermission.js +23 -23
  63. package/lib/models/adminpermission/IAdminPermission.d.ts +13 -13
  64. package/lib/models/adminpermission/IAdminPermission.js +2 -2
  65. package/lib/models/customPage/CustomPage.d.ts +2 -2
  66. package/lib/models/customPage/CustomPage.js +38 -38
  67. package/lib/models/customPage/ICustomPage.d.ts +17 -17
  68. package/lib/models/customPage/ICustomPage.js +2 -2
  69. package/lib/models/file/File.d.ts +2 -2
  70. package/lib/models/file/File.js +23 -23
  71. package/lib/models/file/IFile.d.ts +15 -15
  72. package/lib/models/file/IFile.js +2 -2
  73. package/lib/models/location/ILocation.d.ts +14 -14
  74. package/lib/models/location/ILocation.js +2 -2
  75. package/lib/models/location/Location.d.ts +2 -2
  76. package/lib/models/location/Location.js +21 -21
  77. package/lib/models/modelConfiguration/IModelConfigurations.d.ts +29 -29
  78. package/lib/models/modelConfiguration/IModelConfigurations.js +2 -2
  79. package/lib/models/modelConfiguration/ModelConfiguration.d.ts +13 -13
  80. package/lib/models/modelConfiguration/ModelConfiguration.js +95 -95
  81. package/lib/models/permission/IPermission.d.ts +14 -14
  82. package/lib/models/permission/IPermission.js +2 -2
  83. package/lib/models/permission/Permission.d.ts +2 -2
  84. package/lib/models/permission/Permission.js +21 -21
  85. package/lib/models/request-log/IRequestLog.d.ts +22 -22
  86. package/lib/models/request-log/IRequestLog.js +2 -2
  87. package/lib/models/request-log/RequestLog.d.ts +3 -3
  88. package/lib/models/request-log/RequestLog.js +51 -51
  89. package/lib/repositories/DashboardRepository.d.ts +5 -5
  90. package/lib/repositories/DashboardRepository.js +12 -12
  91. package/lib/repositories/Repository.d.ts +68 -68
  92. package/lib/repositories/Repository.js +212 -212
  93. package/lib/repositories/RequestLogRepository.d.ts +10 -10
  94. package/lib/repositories/RequestLogRepository.js +54 -54
  95. package/lib/repositories/SaveResult.d.ts +14 -14
  96. package/lib/repositories/SaveResult.js +18 -18
  97. package/lib/router/index.d.ts +8 -8
  98. package/lib/router/index.js +104 -104
  99. package/lib/types/DashbordConfig.d.ts +21 -21
  100. package/lib/types/DashbordConfig.js +2 -2
  101. package/lib/types/IConfigurationFile.d.ts +6 -6
  102. package/lib/types/IConfigurationFile.js +2 -2
  103. package/lib/types/IResourceFile.d.ts +596 -584
  104. package/lib/types/IResourceFile.js +2 -2
  105. package/lib/types/IResourceResponse.d.ts +95 -95
  106. package/lib/types/IResourceResponse.js +2 -2
  107. package/lib/types/helpers.d.ts +61 -61
  108. package/lib/types/helpers.js +71 -71
  109. package/lib/utils/ResourceUtils.d.ts +2 -2
  110. package/lib/utils/ResourceUtils.js +7 -7
  111. package/lib/utils/ResponseUtils.d.ts +11 -11
  112. package/lib/utils/ResponseUtils.js +41 -41
  113. package/lib/utils/StringUtils.d.ts +9 -9
  114. package/lib/utils/StringUtils.js +46 -46
  115. package/lib/view/asset-manifest.json +18 -18
  116. package/lib/view/assets/icons/delete_icon.svg +3 -3
  117. package/lib/view/assets/icons/flags/ic_flag_cn.svg +9 -9
  118. package/lib/view/assets/icons/flags/ic_flag_sa.svg +9 -9
  119. package/lib/view/assets/icons/flags/ic_flag_vn.svg +9 -9
  120. package/lib/view/assets/icons/info_icon.svg +3 -3
  121. package/lib/view/assets/icons/navbar/ic_banking.svg +5 -5
  122. package/lib/view/assets/icons/navbar/ic_invoice.svg +4 -4
  123. package/lib/view/assets/icons/navbar/ic_kanban.svg +7 -7
  124. package/lib/view/assets/icons/navbar/ic_menu_item.svg +8 -8
  125. package/lib/view/assets/icons/small_info_icon.svg +3 -3
  126. package/lib/view/assets/illustrations/Group 16.svg +4 -4
  127. package/lib/view/assets/illustrations/logo.svg +5 -5
  128. package/lib/view/favicon/browserconfig.xml +9 -9
  129. package/lib/view/favicon/safari-pinned-tab.svg +182 -182
  130. package/lib/view/favicon/site.webmanifest +19 -19
  131. package/lib/view/fonts/index.css +18 -18
  132. package/lib/view/index.html +52 -52
  133. package/lib/view/manifest.json +20 -20
  134. package/lib/view/robots.txt +3 -3
  135. package/lib/view/static/css/main.54de06ef.css +1 -1
  136. package/lib/view/static/js/574.778b5963.chunk.js +1 -1
  137. package/lib/view/static/js/678.521704a3.chunk.js +1 -1
  138. package/lib/view/static/js/798.54856416.chunk.js +1 -1
  139. package/lib/view/static/js/main.4687f255.js +2 -2
  140. package/lib/view/static/js/main.4687f255.js.LICENSE.txt +214 -214
  141. package/package.json +51 -51
@@ -1,595 +1,602 @@
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 ResponseUtils_1 = __importDefault(require("../utils/ResponseUtils"));
27
- const express_validator_1 = require("express-validator");
28
- const ResourceGenerator_1 = __importDefault(require("../helpers/ResourceGenerator"));
29
- const ResourceHelper_1 = __importDefault(require("../helpers/ResourceHelper"));
30
- const StringUtils_1 = __importDefault(require("../utils/StringUtils"));
31
- const mongoose_1 = __importDefault(require("mongoose"));
32
- const inversify_1 = require("inversify");
33
- const Repository_1 = __importDefault(require("../repositories/Repository"));
34
- const ResourceUtils_1 = require("../utils/ResourceUtils");
35
- const FiltersHelper_1 = __importDefault(require("../helpers/FiltersHelper"));
36
- const LocalizedStringHelper_1 = __importDefault(require("../helpers/LocalizedStringHelper"));
37
- const ActionsGenerator_1 = __importDefault(require("../helpers/ActionsGenerator"));
38
- const helpers_1 = require("../types/helpers");
39
- const PopulationHelper_1 = require("../helpers/PopulationHelper");
40
- let DashboardController = DashboardController_1 = class DashboardController {
41
- constructor(resource, repository, resources, modelConfigurations) {
42
- this.resource = resource;
43
- this.repository = repository;
44
- this.resources = resources;
45
- this.modelConfigurations = modelConfigurations;
46
- if (!repository && resource) {
47
- this.repository = new Repository_1.default(resource.properties.resource);
48
- }
49
- }
50
- validateRequest(req, res) {
51
- const errors = (0, express_validator_1.validationResult)(req);
52
- if (!errors.isEmpty()) {
53
- ResponseUtils_1.default.send(res, 422, errors.array()[0].msg, errors);
54
- return false;
55
- }
56
- return true;
57
- }
58
- paginateParams(request) {
59
- const page = request.query.page
60
- ? +request.query.page
61
- : DashboardController_1.DEFAULT_PAGE;
62
- const perPage = request.query.perPage
63
- ? +request.query.perPage
64
- : DashboardController_1.DEFAULT_PER_PAGE;
65
- return { page, perPage };
66
- }
67
- getSearchableSubStringFilter(resource, filter, subString) {
68
- var _a;
69
- const schema = resource.properties.resource.schema.paths;
70
- const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource, (_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(resource.properties.modelName));
71
- if (searchBy === "_id") {
72
- if (!mongoose_1.default.isValidObjectId(subString.source)) {
73
- return filter;
74
- }
75
- filter[searchBy] = subString.source;
76
- return filter;
77
- }
78
- if (LocalizedStringHelper_1.default.checkLocalizedStringType(resource, searchBy)) {
79
- filter.$or = [
80
- LocalizedStringHelper_1.default.getFilterObject(searchBy, "0", subString),
81
- LocalizedStringHelper_1.default.getFilterObject(searchBy, "1", subString),
82
- ];
83
- }
84
- else {
85
- filter[searchBy] = subString;
86
- }
87
- return filter;
88
- }
89
- getScopeFilter(filter, scope) {
90
- return {};
91
- }
92
- getVisibileExtraActions(currentUser, record, resource, repository, modelName) {
93
- return __awaiter(this, void 0, void 0, function* () {
94
- const data = {
95
- record: record.toObject(),
96
- currentUser,
97
- resource: {
98
- name: modelName,
99
- path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
100
- repository,
101
- },
102
- };
103
- if (!resource.properties.actions || !resource.properties.actions.extras) {
104
- return undefined;
105
- }
106
- var extraActionsArray = [];
107
- const extraActions = resource.properties.actions.extras;
108
- for (var i = 0; i < extraActions.length; i++) {
109
- const extraAction = extraActions[i];
110
- if (!extraAction.isVisible) {
111
- extraActionsArray.push(extraAction.key);
112
- continue;
113
- }
114
- const isVisible = yield extraAction.isVisible(data);
115
- if (isVisible) {
116
- extraActionsArray.push(extraAction.key);
117
- }
118
- }
119
- return extraActionsArray;
120
- });
121
- }
122
- index(req, res) {
123
- var _a, _b, _c;
124
- return __awaiter(this, void 0, void 0, function* () {
125
- if (!this.validateRequest(req, res)) {
126
- return;
127
- }
128
- const paginateParams = this.paginateParams(req);
129
- const searchableSubString = req.query.filter
130
- ? new RegExp(req.query.filter, "i")
131
- : undefined;
132
- const scope = req.query.scope;
133
- const currentUser = req.user;
134
- const modelName = req.params.resource;
135
- const filtersQuery = req.query.filters;
136
- const filters = filtersQuery
137
- ? filtersQuery.split("^^")
138
- : undefined;
139
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
140
- if (!resource) {
141
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
142
- }
143
- if (!currentUser) {
144
- return ResponseUtils_1.default.unauthorized(res);
145
- }
146
- const permissionCheck = resource.properties.isAllowed
147
- ? yield resource.properties.isAllowed(currentUser)
148
- : true;
149
- if (!permissionCheck) {
150
- return ResponseUtils_1.default.forbidden(res);
151
- }
152
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
153
- const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
154
- const sort = req.query.order;
155
- const sortBy = req.query.orderBy;
156
- const sortQuery = {};
157
- sortQuery[sortBy] = sort;
158
- if (sortBy !== "_id") {
159
- sortQuery._id = "asc";
160
- }
161
- var filter = {};
162
- if (scope &&
163
- resource.properties.filters &&
164
- resource.properties.filters.scopes &&
165
- resource.properties.filters.scopes.isAccessible) {
166
- if (resource.properties.filters.scopes.manual) {
167
- filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
168
- }
169
- else if (resource.properties.filters.scopes.auto) {
170
- const key = resource.properties.filters.scopes.auto.key;
171
- if (key) {
172
- filter[key] = scope;
173
- }
174
- }
175
- }
176
- if (searchableSubString) {
177
- filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
178
- }
179
- filter = yield FiltersHelper_1.default.appendFilters(filter, filters, resource);
180
- const crudOperations = resource.properties.crudOperations;
181
- if (crudOperations && crudOperations.index && crudOperations.index.before) {
182
- filter = yield crudOperations.index.before(req, filter, currentUser);
183
- }
184
- var records = [];
185
- var pageInfo = undefined;
186
- const populatedString = modifiedResource.properties.populatedString;
187
- const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.LIST, populatedString);
188
- const modifiedPopulatedObject = yield populationHelper.get();
189
- if (!req.query.page && !req.query.perPage) {
190
- records = yield repository.findMany({
191
- sort: sortQuery,
192
- filter,
193
- populate: modifiedPopulatedObject,
194
- });
195
- }
196
- else {
197
- const page = yield repository.paginate({
198
- sort: sortQuery,
199
- filter,
200
- paginateParams: paginateParams,
201
- populate: modifiedPopulatedObject,
202
- });
203
- records = page.records;
204
- pageInfo = page.pageInfo;
205
- }
206
- var documents = [];
207
- for (var i = 0; i < records.length; i++) {
208
- const record = records[i];
209
- //let record: any = records[i]
210
- //const recordFlatten = await ResourcesHelper.addExtraFields(modifiedResource.listProperties, modifiedResource.properties.model, record.toObject(), StringUtils.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource)
211
- const extraActionKeys = yield this.getVisibileExtraActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
212
- if (extraActionKeys) {
213
- const recordFlatten = record.toObject();
214
- recordFlatten.extraActionKeys = extraActionKeys;
215
- documents.push(recordFlatten);
216
- }
217
- else {
218
- documents.push(record);
219
- }
220
- }
221
- if (crudOperations && crudOperations.index && crudOperations.index.after) {
222
- documents = yield crudOperations.index.after(req, documents, currentUser);
223
- }
224
- return ResponseUtils_1.default.send(res, 200, "OK", {
225
- records: documents,
226
- pageInfo,
227
- //options: modifiedResource
228
- });
229
- });
230
- }
231
- create(req, res) {
232
- var _a, _b, _c, _d;
233
- return __awaiter(this, void 0, void 0, function* () {
234
- if (!this.validateRequest(req, res)) {
235
- return;
236
- }
237
- const modelName = req.params.resource;
238
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
239
- const currentUser = req.user;
240
- if (!resource) {
241
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
242
- }
243
- if (!currentUser) {
244
- return ResponseUtils_1.default.unauthorized(res);
245
- }
246
- const permissionCheck = resource.properties.isAllowed
247
- ? yield resource.properties.isAllowed(currentUser)
248
- : true;
249
- if (!permissionCheck) {
250
- return ResponseUtils_1.default.forbidden(res);
251
- }
252
- const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
253
- const createPermission = actions.new.isAccessible;
254
- if (!createPermission) {
255
- return ResponseUtils_1.default.forbidden(res);
256
- }
257
- const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
258
- var recordParams = req.body;
259
- recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
260
- const crudOperations = resource.properties.crudOperations;
261
- if (crudOperations &&
262
- crudOperations.create &&
263
- crudOperations.create.before) {
264
- recordParams = yield crudOperations.create.before(req, recordParams, currentUser);
265
- }
266
- var record;
267
- if (recordParams.password) {
268
- //const user = new Mongoose.prototype.model(modifiedResource.properties.modelName)(recordParams);
269
- const user = new resource.properties.resource(recordParams);
270
- const set = yield user.setPassword(recordParams.password);
271
- record = yield repository.saveInstance(user);
272
- }
273
- else {
274
- record = yield repository.save(recordParams);
275
- }
276
- if (!record.isValid() || !record.document) {
277
- return ResponseUtils_1.default.unprocessable(res, "Invalid Data", record.getErrors());
278
- }
279
- if (crudOperations &&
280
- crudOperations.create &&
281
- crudOperations.create.after) {
282
- record = yield crudOperations.create.after(req, record, currentUser, recordParams);
283
- }
284
- return ResponseUtils_1.default.created(res, {
285
- record,
286
- });
287
- });
288
- }
289
- update(req, res) {
290
- var _a, _b, _c, _d, _e;
291
- return __awaiter(this, void 0, void 0, function* () {
292
- if (!this.validateRequest(req, res)) {
293
- return;
294
- }
295
- const modelName = req.params.resource;
296
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
297
- if (!resource) {
298
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
299
- }
300
- var recordParams = req.body;
301
- const currentUser = req.user;
302
- if (!currentUser) {
303
- return ResponseUtils_1.default.unauthorized(res);
304
- }
305
- const permissionCheck = resource.properties.isAllowed
306
- ? yield resource.properties.isAllowed(currentUser)
307
- : true;
308
- if (!permissionCheck) {
309
- return ResponseUtils_1.default.forbidden(res);
310
- }
311
- const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
312
- const editPermission = actions.edit.isAccessible;
313
- if (!editPermission) {
314
- return ResponseUtils_1.default.forbidden(res);
315
- }
316
- const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
317
- const recordId = req.params.id;
318
- var record = yield repository.findById(recordId);
319
- if (!record) {
320
- return ResponseUtils_1.default.send(res, 404, "record Not Found");
321
- }
322
- const crudOperations = resource.properties.crudOperations;
323
- if (crudOperations &&
324
- crudOperations.update &&
325
- crudOperations.update.before) {
326
- recordParams = yield crudOperations.update.before(req, recordParams, currentUser);
327
- }
328
- var recordSaveResult = {};
329
- if ((_e = crudOperations === null || crudOperations === void 0 ? void 0 : crudOperations.update) === null || _e === void 0 ? void 0 : _e.update) {
330
- recordSaveResult = yield crudOperations.update.update(req, record, recordParams, currentUser);
331
- }
332
- else {
333
- recordSaveResult = yield repository.update(record, recordParams);
334
- }
335
- if (!recordSaveResult.isValid()) {
336
- return ResponseUtils_1.default.unprocessable(res, "Invalid Data", recordSaveResult.getErrors());
337
- }
338
- // if (resource.properties.modelName === ModelNames.Settings) {
339
- // await AppSettings.run()
340
- // }
341
- if (crudOperations &&
342
- crudOperations.update &&
343
- crudOperations.update.after) {
344
- recordSaveResult = yield crudOperations.update.after(req, recordSaveResult, recordParams, currentUser);
345
- }
346
- return ResponseUtils_1.default.ok(res, {
347
- record: recordSaveResult,
348
- });
349
- });
350
- }
351
- show(req, res) {
352
- var _a, _b, _c, _d, _e;
353
- return __awaiter(this, void 0, void 0, function* () {
354
- if (!this.validateRequest(req, res)) {
355
- return;
356
- }
357
- const modelName = req.params.resource;
358
- const recordId = req.params.id;
359
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
360
- const currentUser = req.user;
361
- const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
362
- if (!resource) {
363
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
364
- }
365
- if (!currentUser) {
366
- return ResponseUtils_1.default.unauthorized(res);
367
- }
368
- const permissionCheck = resource.properties.isAllowed
369
- ? yield resource.properties.isAllowed(currentUser)
370
- : true;
371
- if (!permissionCheck) {
372
- return ResponseUtils_1.default.forbidden(res);
373
- }
374
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
375
- const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
376
- const populatedString = modifiedResource.properties.populatedString;
377
- const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
378
- const modifiedPopulatedObject = yield populationHelper.get();
379
- var record = yield repository.findOne({
380
- filter: {
381
- _id: recordId,
382
- },
383
- populate: modifiedPopulatedObject,
384
- });
385
- if (!record) {
386
- return ResponseUtils_1.default.send(res, 404, "record not found");
387
- }
388
- record = record.toObject();
389
- //record = await this.getExtras(record._id.toString(), record, this.getExtraRepository())
390
- record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
391
- const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
392
- let extras = {};
393
- if (afterMethod && record) {
394
- extras = yield afterMethod(req, record);
395
- }
396
- return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null }));
397
- });
398
- }
399
- deleteAll(req, res) {
400
- var _a, _b, _c, _d;
401
- return __awaiter(this, void 0, void 0, function* () {
402
- if (!this.validateRequest(req, res)) {
403
- return;
404
- }
405
- const modelName = req.params.resource;
406
- const recordIds = req.body;
407
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
408
- if (!resource) {
409
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
410
- }
411
- const currentUser = req.user;
412
- if (!currentUser) {
413
- return ResponseUtils_1.default.unauthorized(res);
414
- }
415
- const permissionCheck = resource.properties.isAllowed
416
- ? yield resource.properties.isAllowed(currentUser)
417
- : true;
418
- if (!permissionCheck) {
419
- return ResponseUtils_1.default.forbidden(res);
420
- }
421
- const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
422
- const bulkDeletePermission = actions.bulkDelete.isAccessible;
423
- if (!bulkDeletePermission) {
424
- return ResponseUtils_1.default.forbidden(res);
425
- }
426
- const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
427
- for (var i = 0; i < recordIds.length; i++) {
428
- const recordId = recordIds[i];
429
- const record = yield repository.findById(recordId);
430
- if (!record) {
431
- continue;
432
- }
433
- yield repository.remove({ _id: record._id });
434
- }
435
- return ResponseUtils_1.default.send(res, 200, "OK");
436
- });
437
- }
438
- delete(req, res) {
439
- var _a, _b, _c, _d;
440
- return __awaiter(this, void 0, void 0, function* () {
441
- if (!this.validateRequest(req, res)) {
442
- return;
443
- }
444
- const modelName = req.params.resource;
445
- const recordId = req.params.id;
446
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
447
- if (!resource) {
448
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
449
- }
450
- const currentUser = req.user;
451
- if (!currentUser) {
452
- return ResponseUtils_1.default.unauthorized(res);
453
- }
454
- const permissionCheck = resource.properties.isAllowed
455
- ? yield resource.properties.isAllowed(currentUser)
456
- : true;
457
- if (!permissionCheck) {
458
- return ResponseUtils_1.default.forbidden(res);
459
- }
460
- const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
461
- const deletePermission = actions.delete.isAccessible;
462
- if (!deletePermission) {
463
- return ResponseUtils_1.default.forbidden(res);
464
- }
465
- const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
466
- const record = yield repository.findById(recordId);
467
- if (!record) {
468
- return ResponseUtils_1.default.send(res, 404, "record Not Found");
469
- }
470
- yield repository.remove({ _id: record._id });
471
- return ResponseUtils_1.default.send(res, 200, "OK");
472
- });
473
- }
474
- getNeighbors(req, res) {
475
- var _a, _b, _c;
476
- return __awaiter(this, void 0, void 0, function* () {
477
- if (!this.validateRequest(req, res)) {
478
- return;
479
- }
480
- const modelName = req.params.resource;
481
- const recordId = req.params.id;
482
- const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
483
- const currentUser = req.user;
484
- const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
485
- const neighborRecordType = req.query.type;
486
- if (!resource || !neighborRecordType) {
487
- return ResponseUtils_1.default.notFound(res, "Resource not found", []);
488
- }
489
- if (!currentUser) {
490
- return ResponseUtils_1.default.unauthorized(res);
491
- }
492
- const permissionCheck = resource.properties.isAllowed
493
- ? yield resource.properties.isAllowed(currentUser)
494
- : true;
495
- if (!permissionCheck) {
496
- return ResponseUtils_1.default.forbidden(res);
497
- }
498
- if (neighborRecordType !== helpers_1.NeighborTypes.NEXT &&
499
- neighborRecordType !== helpers_1.NeighborTypes.PREVIOUS) {
500
- return ResponseUtils_1.default.notFound(res, "Type in query should match NEXT or PREVIOUS", []);
501
- }
502
- const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
503
- const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
504
- const populatedString = modifiedResource.properties.populatedString;
505
- const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
506
- const modifiedPopulatedObject = yield populationHelper.get();
507
- var record = yield repository.findOne({
508
- filter: {
509
- _id: recordId,
510
- },
511
- populate: modifiedPopulatedObject,
512
- });
513
- if (!record) {
514
- return ResponseUtils_1.default.send(res, 404, "record not found");
515
- }
516
- record = record.toObject();
517
- record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
518
- const sortBy = modifiedResource.properties
519
- .defaultOrderBy;
520
- const sort = modifiedResource.properties.defaultOrder;
521
- const nextQuery = {};
522
- const prevQuery = {};
523
- const nextSortQuery = {};
524
- const previousSortQuery = {};
525
- const greaterThanQuery = { $gt: record[sortBy] };
526
- const lessThanQuery = { $lt: record[sortBy] };
527
- nextQuery[sortBy] = sort === 'asc' ? greaterThanQuery : lessThanQuery;
528
- prevQuery[sortBy] = sort === 'asc' ? lessThanQuery : greaterThanQuery;
529
- nextSortQuery[sortBy] = sort;
530
- previousSortQuery[sortBy] = sort === 'asc' ? 'desc' : 'asc';
531
- var neighbor;
532
- if (neighborRecordType === helpers_1.NeighborTypes.NEXT) {
533
- neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(nextQuery).sort(nextSortQuery).limit(1));
534
- }
535
- else {
536
- neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(prevQuery).sort(previousSortQuery).limit(1));
537
- }
538
- const hasNeighbor = record && neighbor && neighbor._id ? true : false;
539
- const neighborRecordId = hasNeighbor ? neighbor._id : undefined;
540
- return ResponseUtils_1.default.ok(res, {
541
- hasNeighbor,
542
- neighborRecordId
543
- });
544
- });
545
- }
546
- };
547
- DashboardController.DEFAULT_PAGE = 1;
548
- DashboardController.DEFAULT_PER_PAGE = 30;
549
- __decorate([
550
- __param(0, (0, inversify_express_utils_1.request)()),
551
- __param(1, (0, inversify_express_utils_1.response)())
552
- ], DashboardController.prototype, "validateRequest", null);
553
- __decorate([
554
- (0, inversify_express_utils_1.httpGet)("/"),
555
- __param(0, (0, inversify_express_utils_1.request)()),
556
- __param(1, (0, inversify_express_utils_1.response)())
557
- ], DashboardController.prototype, "index", null);
558
- __decorate([
559
- (0, inversify_express_utils_1.httpPost)("/"),
560
- __param(0, (0, inversify_express_utils_1.request)()),
561
- __param(1, (0, inversify_express_utils_1.response)())
562
- ], DashboardController.prototype, "create", null);
563
- __decorate([
564
- (0, inversify_express_utils_1.httpPatch)("/:id"),
565
- __param(0, (0, inversify_express_utils_1.request)()),
566
- __param(1, (0, inversify_express_utils_1.response)())
567
- ], DashboardController.prototype, "update", null);
568
- __decorate([
569
- (0, inversify_express_utils_1.httpGet)("/:id"),
570
- __param(0, (0, inversify_express_utils_1.request)()),
571
- __param(1, (0, inversify_express_utils_1.response)())
572
- ], DashboardController.prototype, "show", null);
573
- __decorate([
574
- (0, inversify_express_utils_1.httpDelete)("/all"),
575
- __param(0, (0, inversify_express_utils_1.request)()),
576
- __param(1, (0, inversify_express_utils_1.response)())
577
- ], DashboardController.prototype, "deleteAll", null);
578
- __decorate([
579
- (0, inversify_express_utils_1.httpDelete)("/:id"),
580
- __param(0, (0, inversify_express_utils_1.request)()),
581
- __param(1, (0, inversify_express_utils_1.response)())
582
- ], DashboardController.prototype, "delete", null);
583
- __decorate([
584
- (0, inversify_express_utils_1.httpGet)("/neighbors/:id"),
585
- __param(0, (0, inversify_express_utils_1.request)()),
586
- __param(1, (0, inversify_express_utils_1.response)())
587
- ], DashboardController.prototype, "getNeighbors", null);
588
- DashboardController = DashboardController_1 = __decorate([
589
- (0, inversify_1.injectable)(),
590
- __param(0, (0, inversify_1.unmanaged)()),
591
- __param(1, (0, inversify_1.unmanaged)()),
592
- __param(2, (0, inversify_1.unmanaged)()),
593
- __param(3, (0, inversify_1.unmanaged)())
594
- ], DashboardController);
595
- 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 ResponseUtils_1 = __importDefault(require("../utils/ResponseUtils"));
27
+ const express_validator_1 = require("express-validator");
28
+ const ResourceGenerator_1 = __importDefault(require("../helpers/ResourceGenerator"));
29
+ const ResourceHelper_1 = __importDefault(require("../helpers/ResourceHelper"));
30
+ const StringUtils_1 = __importDefault(require("../utils/StringUtils"));
31
+ const mongoose_1 = __importDefault(require("mongoose"));
32
+ const inversify_1 = require("inversify");
33
+ const Repository_1 = __importDefault(require("../repositories/Repository"));
34
+ const ResourceUtils_1 = require("../utils/ResourceUtils");
35
+ const FiltersHelper_1 = __importDefault(require("../helpers/FiltersHelper"));
36
+ const LocalizedStringHelper_1 = __importDefault(require("../helpers/LocalizedStringHelper"));
37
+ const ActionsGenerator_1 = __importDefault(require("../helpers/ActionsGenerator"));
38
+ const helpers_1 = require("../types/helpers");
39
+ const PopulationHelper_1 = require("../helpers/PopulationHelper");
40
+ let DashboardController = DashboardController_1 = class DashboardController {
41
+ constructor(resource, repository, resources, modelConfigurations) {
42
+ this.resource = resource;
43
+ this.repository = repository;
44
+ this.resources = resources;
45
+ this.modelConfigurations = modelConfigurations;
46
+ if (!repository && resource) {
47
+ this.repository = new Repository_1.default(resource.properties.resource);
48
+ }
49
+ }
50
+ validateRequest(req, res) {
51
+ const errors = (0, express_validator_1.validationResult)(req);
52
+ if (!errors.isEmpty()) {
53
+ ResponseUtils_1.default.send(res, 422, errors.array()[0].msg, errors);
54
+ return false;
55
+ }
56
+ return true;
57
+ }
58
+ paginateParams(request) {
59
+ const page = request.query.page
60
+ ? +request.query.page
61
+ : DashboardController_1.DEFAULT_PAGE;
62
+ const perPage = request.query.perPage
63
+ ? +request.query.perPage
64
+ : DashboardController_1.DEFAULT_PER_PAGE;
65
+ return { page, perPage };
66
+ }
67
+ getSearchableSubStringFilter(resource, filter, subString) {
68
+ var _a;
69
+ const schema = resource.properties.resource.schema.paths;
70
+ const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource, (_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(resource.properties.modelName));
71
+ if (searchBy === "_id") {
72
+ if (!mongoose_1.default.isValidObjectId(subString.source)) {
73
+ return filter;
74
+ }
75
+ filter[searchBy] = subString.source;
76
+ return filter;
77
+ }
78
+ if (LocalizedStringHelper_1.default.checkLocalizedStringType(resource, searchBy)) {
79
+ filter.$or = [
80
+ LocalizedStringHelper_1.default.getFilterObject(searchBy, "0", subString),
81
+ LocalizedStringHelper_1.default.getFilterObject(searchBy, "1", subString),
82
+ ];
83
+ }
84
+ else {
85
+ filter[searchBy] = subString;
86
+ }
87
+ return filter;
88
+ }
89
+ getScopeFilter(filter, scope) {
90
+ return {};
91
+ }
92
+ getVisibileExtraActions(currentUser, record, resource, repository, modelName) {
93
+ return __awaiter(this, void 0, void 0, function* () {
94
+ const data = {
95
+ record: record.toObject(),
96
+ currentUser,
97
+ resource: {
98
+ name: modelName,
99
+ path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
100
+ repository,
101
+ },
102
+ };
103
+ if (!resource.properties.actions || !resource.properties.actions.extras) {
104
+ return undefined;
105
+ }
106
+ var extraActionsArray = [];
107
+ const extraActions = resource.properties.actions.extras;
108
+ for (var i = 0; i < extraActions.length; i++) {
109
+ const extraAction = extraActions[i];
110
+ if (!extraAction.isVisible) {
111
+ extraActionsArray.push(extraAction.key);
112
+ continue;
113
+ }
114
+ const isVisible = yield extraAction.isVisible(data);
115
+ if (isVisible) {
116
+ extraActionsArray.push(extraAction.key);
117
+ }
118
+ }
119
+ return extraActionsArray;
120
+ });
121
+ }
122
+ index(req, res) {
123
+ var _a, _b, _c;
124
+ return __awaiter(this, void 0, void 0, function* () {
125
+ if (!this.validateRequest(req, res)) {
126
+ return;
127
+ }
128
+ const paginateParams = this.paginateParams(req);
129
+ const searchableSubString = req.query.filter
130
+ ? new RegExp(req.query.filter, "i")
131
+ : undefined;
132
+ const scope = req.query.scope;
133
+ const currentUser = req.user;
134
+ const modelName = req.params.resource;
135
+ const filtersQuery = req.query.filters;
136
+ const filters = filtersQuery
137
+ ? filtersQuery.split("^^")
138
+ : undefined;
139
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
140
+ if (!resource) {
141
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
142
+ }
143
+ if (!currentUser) {
144
+ return ResponseUtils_1.default.unauthorized(res);
145
+ }
146
+ const permissionCheck = resource.properties.isAllowed
147
+ ? yield resource.properties.isAllowed(currentUser)
148
+ : true;
149
+ if (!permissionCheck) {
150
+ return ResponseUtils_1.default.forbidden(res);
151
+ }
152
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
153
+ const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
154
+ const sort = req.query.order;
155
+ const sortBy = req.query.orderBy;
156
+ const sortQuery = {};
157
+ sortQuery[sortBy] = sort;
158
+ if (sortBy !== "_id") {
159
+ sortQuery._id = "asc";
160
+ }
161
+ var filter = {};
162
+ if (scope &&
163
+ resource.properties.filters &&
164
+ resource.properties.filters.scopes &&
165
+ resource.properties.filters.scopes.isAccessible) {
166
+ if (resource.properties.filters.scopes.manual) {
167
+ filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
168
+ }
169
+ else if (resource.properties.filters.scopes.auto) {
170
+ const key = resource.properties.filters.scopes.auto.key;
171
+ if (key) {
172
+ filter[key] = scope;
173
+ }
174
+ }
175
+ }
176
+ if (searchableSubString) {
177
+ filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
178
+ }
179
+ filter = yield FiltersHelper_1.default.appendFilters(filter, filters, resource);
180
+ const crudOperations = resource.properties.crudOperations;
181
+ if (crudOperations && crudOperations.index && crudOperations.index.before) {
182
+ filter = yield crudOperations.index.before(req, filter, currentUser);
183
+ }
184
+ var records = [];
185
+ var pageInfo = undefined;
186
+ const populatedString = modifiedResource.properties.populatedString;
187
+ const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.LIST, populatedString);
188
+ const modifiedPopulatedObject = yield populationHelper.get();
189
+ if (crudOperations && crudOperations.index && crudOperations.index.index) {
190
+ const page = yield crudOperations.index.index(req, filter, sortQuery, paginateParams, modifiedPopulatedObject, currentUser);
191
+ records = page.records;
192
+ pageInfo = page.pageInfo;
193
+ }
194
+ else {
195
+ if (!req.query.page && !req.query.perPage) {
196
+ records = yield repository.findMany({
197
+ sort: sortQuery,
198
+ filter,
199
+ populate: modifiedPopulatedObject,
200
+ });
201
+ }
202
+ else {
203
+ const page = yield repository.paginate({
204
+ sort: sortQuery,
205
+ filter,
206
+ paginateParams: paginateParams,
207
+ populate: modifiedPopulatedObject,
208
+ });
209
+ records = page.records;
210
+ pageInfo = page.pageInfo;
211
+ }
212
+ }
213
+ var documents = [];
214
+ for (var i = 0; i < records.length; i++) {
215
+ const record = records[i];
216
+ //let record: any = records[i]
217
+ //const recordFlatten = await ResourcesHelper.addExtraFields(modifiedResource.listProperties, modifiedResource.properties.model, record.toObject(), StringUtils.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource)
218
+ const extraActionKeys = yield this.getVisibileExtraActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
219
+ if (extraActionKeys) {
220
+ const recordFlatten = record.toObject();
221
+ recordFlatten.extraActionKeys = extraActionKeys;
222
+ documents.push(recordFlatten);
223
+ }
224
+ else {
225
+ documents.push(record);
226
+ }
227
+ }
228
+ if (crudOperations && crudOperations.index && crudOperations.index.after) {
229
+ documents = yield crudOperations.index.after(req, documents, currentUser);
230
+ }
231
+ return ResponseUtils_1.default.send(res, 200, "OK", {
232
+ records: documents,
233
+ pageInfo,
234
+ //options: modifiedResource
235
+ });
236
+ });
237
+ }
238
+ create(req, res) {
239
+ var _a, _b, _c, _d;
240
+ return __awaiter(this, void 0, void 0, function* () {
241
+ if (!this.validateRequest(req, res)) {
242
+ return;
243
+ }
244
+ const modelName = req.params.resource;
245
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
246
+ const currentUser = req.user;
247
+ if (!resource) {
248
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
249
+ }
250
+ if (!currentUser) {
251
+ return ResponseUtils_1.default.unauthorized(res);
252
+ }
253
+ const permissionCheck = resource.properties.isAllowed
254
+ ? yield resource.properties.isAllowed(currentUser)
255
+ : true;
256
+ if (!permissionCheck) {
257
+ return ResponseUtils_1.default.forbidden(res);
258
+ }
259
+ const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
260
+ const createPermission = actions.new.isAccessible;
261
+ if (!createPermission) {
262
+ return ResponseUtils_1.default.forbidden(res);
263
+ }
264
+ const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
265
+ var recordParams = req.body;
266
+ recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
267
+ const crudOperations = resource.properties.crudOperations;
268
+ if (crudOperations &&
269
+ crudOperations.create &&
270
+ crudOperations.create.before) {
271
+ recordParams = yield crudOperations.create.before(req, recordParams, currentUser);
272
+ }
273
+ var record;
274
+ if (recordParams.password) {
275
+ //const user = new Mongoose.prototype.model(modifiedResource.properties.modelName)(recordParams);
276
+ const user = new resource.properties.resource(recordParams);
277
+ const set = yield user.setPassword(recordParams.password);
278
+ record = yield repository.saveInstance(user);
279
+ }
280
+ else {
281
+ record = yield repository.save(recordParams);
282
+ }
283
+ if (!record.isValid() || !record.document) {
284
+ return ResponseUtils_1.default.unprocessable(res, "Invalid Data", record.getErrors());
285
+ }
286
+ if (crudOperations &&
287
+ crudOperations.create &&
288
+ crudOperations.create.after) {
289
+ record = yield crudOperations.create.after(req, record, currentUser, recordParams);
290
+ }
291
+ return ResponseUtils_1.default.created(res, {
292
+ record,
293
+ });
294
+ });
295
+ }
296
+ update(req, res) {
297
+ var _a, _b, _c, _d, _e;
298
+ return __awaiter(this, void 0, void 0, function* () {
299
+ if (!this.validateRequest(req, res)) {
300
+ return;
301
+ }
302
+ const modelName = req.params.resource;
303
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
304
+ if (!resource) {
305
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
306
+ }
307
+ var recordParams = req.body;
308
+ const currentUser = req.user;
309
+ if (!currentUser) {
310
+ return ResponseUtils_1.default.unauthorized(res);
311
+ }
312
+ const permissionCheck = resource.properties.isAllowed
313
+ ? yield resource.properties.isAllowed(currentUser)
314
+ : true;
315
+ if (!permissionCheck) {
316
+ return ResponseUtils_1.default.forbidden(res);
317
+ }
318
+ const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
319
+ const editPermission = actions.edit.isAccessible;
320
+ if (!editPermission) {
321
+ return ResponseUtils_1.default.forbidden(res);
322
+ }
323
+ const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
324
+ const recordId = req.params.id;
325
+ var record = yield repository.findById(recordId);
326
+ if (!record) {
327
+ return ResponseUtils_1.default.send(res, 404, "record Not Found");
328
+ }
329
+ const crudOperations = resource.properties.crudOperations;
330
+ if (crudOperations &&
331
+ crudOperations.update &&
332
+ crudOperations.update.before) {
333
+ recordParams = yield crudOperations.update.before(req, recordParams, currentUser);
334
+ }
335
+ var recordSaveResult = {};
336
+ if ((_e = crudOperations === null || crudOperations === void 0 ? void 0 : crudOperations.update) === null || _e === void 0 ? void 0 : _e.update) {
337
+ recordSaveResult = yield crudOperations.update.update(req, record, recordParams, currentUser);
338
+ }
339
+ else {
340
+ recordSaveResult = yield repository.update(record, recordParams);
341
+ }
342
+ if (!recordSaveResult.isValid()) {
343
+ return ResponseUtils_1.default.unprocessable(res, "Invalid Data", recordSaveResult.getErrors());
344
+ }
345
+ // if (resource.properties.modelName === ModelNames.Settings) {
346
+ // await AppSettings.run()
347
+ // }
348
+ if (crudOperations &&
349
+ crudOperations.update &&
350
+ crudOperations.update.after) {
351
+ recordSaveResult = yield crudOperations.update.after(req, recordSaveResult, recordParams, currentUser);
352
+ }
353
+ return ResponseUtils_1.default.ok(res, {
354
+ record: recordSaveResult,
355
+ });
356
+ });
357
+ }
358
+ show(req, res) {
359
+ var _a, _b, _c, _d, _e;
360
+ return __awaiter(this, void 0, void 0, function* () {
361
+ if (!this.validateRequest(req, res)) {
362
+ return;
363
+ }
364
+ const modelName = req.params.resource;
365
+ const recordId = req.params.id;
366
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
367
+ const currentUser = req.user;
368
+ const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
369
+ if (!resource) {
370
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
371
+ }
372
+ if (!currentUser) {
373
+ return ResponseUtils_1.default.unauthorized(res);
374
+ }
375
+ const permissionCheck = resource.properties.isAllowed
376
+ ? yield resource.properties.isAllowed(currentUser)
377
+ : true;
378
+ if (!permissionCheck) {
379
+ return ResponseUtils_1.default.forbidden(res);
380
+ }
381
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
382
+ const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
383
+ const populatedString = modifiedResource.properties.populatedString;
384
+ const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
385
+ const modifiedPopulatedObject = yield populationHelper.get();
386
+ var record = yield repository.findOne({
387
+ filter: {
388
+ _id: recordId,
389
+ },
390
+ populate: modifiedPopulatedObject,
391
+ });
392
+ if (!record) {
393
+ return ResponseUtils_1.default.send(res, 404, "record not found");
394
+ }
395
+ record = record.toObject();
396
+ //record = await this.getExtras(record._id.toString(), record, this.getExtraRepository())
397
+ record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
398
+ const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
399
+ let extras = {};
400
+ if (afterMethod && record) {
401
+ extras = yield afterMethod(req, record);
402
+ }
403
+ return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null }));
404
+ });
405
+ }
406
+ deleteAll(req, res) {
407
+ var _a, _b, _c, _d;
408
+ return __awaiter(this, void 0, void 0, function* () {
409
+ if (!this.validateRequest(req, res)) {
410
+ return;
411
+ }
412
+ const modelName = req.params.resource;
413
+ const recordIds = req.body;
414
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
415
+ if (!resource) {
416
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
417
+ }
418
+ const currentUser = req.user;
419
+ if (!currentUser) {
420
+ return ResponseUtils_1.default.unauthorized(res);
421
+ }
422
+ const permissionCheck = resource.properties.isAllowed
423
+ ? yield resource.properties.isAllowed(currentUser)
424
+ : true;
425
+ if (!permissionCheck) {
426
+ return ResponseUtils_1.default.forbidden(res);
427
+ }
428
+ const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
429
+ const bulkDeletePermission = actions.bulkDelete.isAccessible;
430
+ if (!bulkDeletePermission) {
431
+ return ResponseUtils_1.default.forbidden(res);
432
+ }
433
+ const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
434
+ for (var i = 0; i < recordIds.length; i++) {
435
+ const recordId = recordIds[i];
436
+ const record = yield repository.findById(recordId);
437
+ if (!record) {
438
+ continue;
439
+ }
440
+ yield repository.remove({ _id: record._id });
441
+ }
442
+ return ResponseUtils_1.default.send(res, 200, "OK");
443
+ });
444
+ }
445
+ delete(req, res) {
446
+ var _a, _b, _c, _d;
447
+ return __awaiter(this, void 0, void 0, function* () {
448
+ if (!this.validateRequest(req, res)) {
449
+ return;
450
+ }
451
+ const modelName = req.params.resource;
452
+ const recordId = req.params.id;
453
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
454
+ if (!resource) {
455
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
456
+ }
457
+ const currentUser = req.user;
458
+ if (!currentUser) {
459
+ return ResponseUtils_1.default.unauthorized(res);
460
+ }
461
+ const permissionCheck = resource.properties.isAllowed
462
+ ? yield resource.properties.isAllowed(currentUser)
463
+ : true;
464
+ if (!permissionCheck) {
465
+ return ResponseUtils_1.default.forbidden(res);
466
+ }
467
+ const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, (_c = this.modelConfigurations) === null || _c === void 0 ? void 0 : _c.get(resource.properties.modelName));
468
+ const deletePermission = actions.delete.isAccessible;
469
+ if (!deletePermission) {
470
+ return ResponseUtils_1.default.forbidden(res);
471
+ }
472
+ const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
473
+ const record = yield repository.findById(recordId);
474
+ if (!record) {
475
+ return ResponseUtils_1.default.send(res, 404, "record Not Found");
476
+ }
477
+ yield repository.remove({ _id: record._id });
478
+ return ResponseUtils_1.default.send(res, 200, "OK");
479
+ });
480
+ }
481
+ getNeighbors(req, res) {
482
+ var _a, _b, _c;
483
+ return __awaiter(this, void 0, void 0, function* () {
484
+ if (!this.validateRequest(req, res)) {
485
+ return;
486
+ }
487
+ const modelName = req.params.resource;
488
+ const recordId = req.params.id;
489
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
490
+ const currentUser = req.user;
491
+ const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
492
+ const neighborRecordType = req.query.type;
493
+ if (!resource || !neighborRecordType) {
494
+ return ResponseUtils_1.default.notFound(res, "Resource not found", []);
495
+ }
496
+ if (!currentUser) {
497
+ return ResponseUtils_1.default.unauthorized(res);
498
+ }
499
+ const permissionCheck = resource.properties.isAllowed
500
+ ? yield resource.properties.isAllowed(currentUser)
501
+ : true;
502
+ if (!permissionCheck) {
503
+ return ResponseUtils_1.default.forbidden(res);
504
+ }
505
+ if (neighborRecordType !== helpers_1.NeighborTypes.NEXT &&
506
+ neighborRecordType !== helpers_1.NeighborTypes.PREVIOUS) {
507
+ return ResponseUtils_1.default.notFound(res, "Type in query should match NEXT or PREVIOUS", []);
508
+ }
509
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
510
+ const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
511
+ const populatedString = modifiedResource.properties.populatedString;
512
+ const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
513
+ const modifiedPopulatedObject = yield populationHelper.get();
514
+ var record = yield repository.findOne({
515
+ filter: {
516
+ _id: recordId,
517
+ },
518
+ populate: modifiedPopulatedObject,
519
+ });
520
+ if (!record) {
521
+ return ResponseUtils_1.default.send(res, 404, "record not found");
522
+ }
523
+ record = record.toObject();
524
+ record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
525
+ const sortBy = modifiedResource.properties
526
+ .defaultOrderBy;
527
+ const sort = modifiedResource.properties.defaultOrder;
528
+ const nextQuery = {};
529
+ const prevQuery = {};
530
+ const nextSortQuery = {};
531
+ const previousSortQuery = {};
532
+ const greaterThanQuery = { $gt: record[sortBy] };
533
+ const lessThanQuery = { $lt: record[sortBy] };
534
+ nextQuery[sortBy] = sort === 'asc' ? greaterThanQuery : lessThanQuery;
535
+ prevQuery[sortBy] = sort === 'asc' ? lessThanQuery : greaterThanQuery;
536
+ nextSortQuery[sortBy] = sort;
537
+ previousSortQuery[sortBy] = sort === 'asc' ? 'desc' : 'asc';
538
+ var neighbor;
539
+ if (neighborRecordType === helpers_1.NeighborTypes.NEXT) {
540
+ neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(nextQuery).sort(nextSortQuery).limit(1));
541
+ }
542
+ else {
543
+ neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(prevQuery).sort(previousSortQuery).limit(1));
544
+ }
545
+ const hasNeighbor = record && neighbor && neighbor._id ? true : false;
546
+ const neighborRecordId = hasNeighbor ? neighbor._id : undefined;
547
+ return ResponseUtils_1.default.ok(res, {
548
+ hasNeighbor,
549
+ neighborRecordId
550
+ });
551
+ });
552
+ }
553
+ };
554
+ DashboardController.DEFAULT_PAGE = 1;
555
+ DashboardController.DEFAULT_PER_PAGE = 30;
556
+ __decorate([
557
+ __param(0, (0, inversify_express_utils_1.request)()),
558
+ __param(1, (0, inversify_express_utils_1.response)())
559
+ ], DashboardController.prototype, "validateRequest", null);
560
+ __decorate([
561
+ (0, inversify_express_utils_1.httpGet)("/"),
562
+ __param(0, (0, inversify_express_utils_1.request)()),
563
+ __param(1, (0, inversify_express_utils_1.response)())
564
+ ], DashboardController.prototype, "index", null);
565
+ __decorate([
566
+ (0, inversify_express_utils_1.httpPost)("/"),
567
+ __param(0, (0, inversify_express_utils_1.request)()),
568
+ __param(1, (0, inversify_express_utils_1.response)())
569
+ ], DashboardController.prototype, "create", null);
570
+ __decorate([
571
+ (0, inversify_express_utils_1.httpPatch)("/:id"),
572
+ __param(0, (0, inversify_express_utils_1.request)()),
573
+ __param(1, (0, inversify_express_utils_1.response)())
574
+ ], DashboardController.prototype, "update", null);
575
+ __decorate([
576
+ (0, inversify_express_utils_1.httpGet)("/:id"),
577
+ __param(0, (0, inversify_express_utils_1.request)()),
578
+ __param(1, (0, inversify_express_utils_1.response)())
579
+ ], DashboardController.prototype, "show", null);
580
+ __decorate([
581
+ (0, inversify_express_utils_1.httpDelete)("/all"),
582
+ __param(0, (0, inversify_express_utils_1.request)()),
583
+ __param(1, (0, inversify_express_utils_1.response)())
584
+ ], DashboardController.prototype, "deleteAll", null);
585
+ __decorate([
586
+ (0, inversify_express_utils_1.httpDelete)("/:id"),
587
+ __param(0, (0, inversify_express_utils_1.request)()),
588
+ __param(1, (0, inversify_express_utils_1.response)())
589
+ ], DashboardController.prototype, "delete", null);
590
+ __decorate([
591
+ (0, inversify_express_utils_1.httpGet)("/neighbors/:id"),
592
+ __param(0, (0, inversify_express_utils_1.request)()),
593
+ __param(1, (0, inversify_express_utils_1.response)())
594
+ ], DashboardController.prototype, "getNeighbors", null);
595
+ DashboardController = DashboardController_1 = __decorate([
596
+ (0, inversify_1.injectable)(),
597
+ __param(0, (0, inversify_1.unmanaged)()),
598
+ __param(1, (0, inversify_1.unmanaged)()),
599
+ __param(2, (0, inversify_1.unmanaged)()),
600
+ __param(3, (0, inversify_1.unmanaged)())
601
+ ], DashboardController);
602
+ exports.default = DashboardController;