identity-admin 1.28.10 → 1.28.12
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.
- package/README.md +294 -294
- package/lib/Dashboard.d.ts +14 -14
- package/lib/Dashboard.js +95 -95
- package/lib/container/helpers/HelperInversify.d.ts +6 -6
- package/lib/container/helpers/HelperInversify.js +10 -10
- package/lib/container/helpers/HelperTypes.d.ts +5 -5
- package/lib/container/helpers/HelperTypes.js +7 -7
- package/lib/container/index.d.ts +1 -1
- package/lib/container/index.js +14 -14
- package/lib/container/repositories/RepositoryInversify.d.ts +4 -4
- package/lib/container/repositories/RepositoryInversify.js +13 -13
- package/lib/container/repositories/RepositoryTypes.d.ts +4 -4
- package/lib/container/repositories/RepositoryTypes.js +6 -6
- package/lib/container/types.d.ts +8 -8
- package/lib/container/types.js +9 -9
- package/lib/controllers/ActionController.d.ts +9 -9
- package/lib/controllers/ActionController.js +141 -141
- package/lib/controllers/AdminNotificationController.d.ts +8 -8
- package/lib/controllers/AdminNotificationController.js +69 -69
- package/lib/controllers/DashboardController.d.ts +44 -44
- package/lib/controllers/DashboardController.js +787 -787
- package/lib/controllers/ResourceController.d.ts +15 -15
- package/lib/controllers/ResourceController.js +135 -135
- package/lib/helpers/ActionsGenerator.d.ts +11 -11
- package/lib/helpers/ActionsGenerator.js +179 -179
- package/lib/helpers/AdminNotifications/AdminNotificationCreation.d.ts +54 -53
- package/lib/helpers/AdminNotifications/AdminNotificationCreation.js +66 -66
- package/lib/helpers/AdminNotifications/AdminNotificationFetcher.d.ts +63 -61
- package/lib/helpers/AdminNotifications/AdminNotificationFetcher.js +45 -44
- package/lib/helpers/FilterQueryHelper.d.ts +9 -9
- package/lib/helpers/FilterQueryHelper.js +83 -83
- package/lib/helpers/FiltersHelper.d.ts +9 -9
- package/lib/helpers/FiltersHelper.js +126 -126
- package/lib/helpers/LocalesHelper.d.ts +4 -4
- package/lib/helpers/LocalesHelper.js +73 -73
- package/lib/helpers/LocalizedStringHelper.d.ts +11 -11
- package/lib/helpers/LocalizedStringHelper.js +74 -74
- package/lib/helpers/Permissions/PermissionFetcher.d.ts +8 -8
- package/lib/helpers/Permissions/PermissionFetcher.js +95 -95
- package/lib/helpers/Permissions/PermissionLabel.d.ts +2 -2
- package/lib/helpers/Permissions/PermissionLabel.js +42 -42
- package/lib/helpers/Permissions/PermissionMapper.d.ts +13 -13
- package/lib/helpers/Permissions/PermissionMapper.js +35 -35
- package/lib/helpers/Permissions/PermissionResource.d.ts +6 -6
- package/lib/helpers/Permissions/PermissionResource.js +62 -62
- package/lib/helpers/Permissions/PermissionSavingService.d.ts +13 -13
- package/lib/helpers/Permissions/PermissionSavingService.js +32 -32
- package/lib/helpers/Permissions/SavePermissionGroup.d.ts +6 -6
- package/lib/helpers/Permissions/SavePermissionGroup.js +32 -32
- package/lib/helpers/Permissions/UpdatePermissionGroup.d.ts +6 -6
- package/lib/helpers/Permissions/UpdatePermissionGroup.js +50 -50
- package/lib/helpers/Permissions/types.d.ts +17 -17
- package/lib/helpers/Permissions/types.js +2 -2
- package/lib/helpers/PopulationHelper.d.ts +12 -12
- package/lib/helpers/PopulationHelper.js +50 -50
- package/lib/helpers/RecordsCounter.d.ts +10 -10
- package/lib/helpers/RecordsCounter.js +33 -33
- package/lib/helpers/ReportsGenerator.d.ts +26 -26
- package/lib/helpers/ReportsGenerator.js +162 -162
- package/lib/helpers/ResourceGenerator.d.ts +9 -9
- package/lib/helpers/ResourceGenerator.js +172 -172
- package/lib/helpers/ResourceHelper.d.ts +28 -28
- package/lib/helpers/ResourceHelper.js +395 -395
- package/lib/helpers/RportsGenerator.d.ts +10 -10
- package/lib/helpers/RportsGenerator.js +21 -21
- package/lib/helpers/SchemaGenerator.d.ts +7 -7
- package/lib/helpers/SchemaGenerator.js +132 -132
- package/lib/helpers/SchemaHelper.d.ts +5 -5
- package/lib/helpers/SchemaHelper.js +21 -21
- package/lib/helpers/UserActionsLog/UserActionsLogHelper.d.ts +2 -2
- package/lib/helpers/UserActionsLog/UserActionsLogHelper.js +49 -49
- package/lib/locales/en.json +53 -53
- package/lib/middlewares/cache/decorator.d.ts +3 -3
- package/lib/middlewares/cache/decorator.js +60 -60
- package/lib/middlewares/cache/deletionMethods.d.ts +3 -3
- package/lib/middlewares/cache/deletionMethods.js +23 -23
- package/lib/middlewares/isAuth.d.ts +13 -13
- package/lib/middlewares/isAuth.js +43 -43
- package/lib/models/ModelNames.d.ts +10 -10
- package/lib/models/ModelNames.js +13 -13
- package/lib/models/adminNotification/AdminNotification.d.ts +5 -5
- package/lib/models/adminNotification/AdminNotification.js +70 -65
- package/lib/models/adminNotification/IAdminNotification.d.ts +35 -34
- package/lib/models/adminNotification/IAdminNotification.js +15 -15
- package/lib/models/adminpermission/AdminPermission.d.ts +11 -11
- package/lib/models/adminpermission/AdminPermission.js +23 -23
- package/lib/models/adminpermission/IAdminPermission.d.ts +13 -13
- package/lib/models/adminpermission/IAdminPermission.js +2 -2
- package/lib/models/customPage/CustomPage.d.ts +2 -2
- package/lib/models/customPage/CustomPage.js +38 -38
- package/lib/models/customPage/ICustomPage.d.ts +17 -17
- package/lib/models/customPage/ICustomPage.js +2 -2
- package/lib/models/file/File.d.ts +2 -2
- package/lib/models/file/File.js +23 -23
- package/lib/models/file/IFile.d.ts +15 -15
- package/lib/models/file/IFile.js +2 -2
- package/lib/models/location/ILocation.d.ts +14 -14
- package/lib/models/location/ILocation.js +2 -2
- package/lib/models/location/Location.d.ts +2 -2
- package/lib/models/location/Location.js +21 -21
- package/lib/models/mailLog/IMailLog.d.ts +14 -14
- package/lib/models/mailLog/IMailLog.js +2 -2
- package/lib/models/mailLog/MailLog.d.ts +3 -3
- package/lib/models/mailLog/MailLog.js +23 -23
- package/lib/models/modelConfiguration/IModelConfigurations.d.ts +29 -29
- package/lib/models/modelConfiguration/IModelConfigurations.js +2 -2
- package/lib/models/modelConfiguration/ModelConfiguration.d.ts +13 -13
- package/lib/models/modelConfiguration/ModelConfiguration.js +95 -95
- package/lib/models/permission/IPermission.d.ts +14 -14
- package/lib/models/permission/IPermission.js +2 -2
- package/lib/models/permission/Permission.d.ts +2 -2
- package/lib/models/permission/Permission.js +21 -21
- package/lib/models/request-log/IRequestLog.d.ts +22 -22
- package/lib/models/request-log/IRequestLog.js +2 -2
- package/lib/models/request-log/RequestLog.d.ts +3 -3
- package/lib/models/request-log/RequestLog.js +51 -51
- package/lib/models/userActionsLog/IUserActionsLog.d.ts +26 -26
- package/lib/models/userActionsLog/IUserActionsLog.js +11 -11
- package/lib/models/userActionsLog/UserActionsLog.d.ts +3 -3
- package/lib/models/userActionsLog/UserActionsLog.js +41 -41
- package/lib/pdf/ReportTemplate.d.ts +71 -71
- package/lib/pdf/ReportTemplate.js +94 -94
- package/lib/repositories/AdminNotificationRepository.d.ts +7 -7
- package/lib/repositories/AdminNotificationRepository.js +23 -23
- package/lib/repositories/DashboardRepository.d.ts +5 -5
- package/lib/repositories/DashboardRepository.js +12 -12
- package/lib/repositories/Repository.d.ts +68 -68
- package/lib/repositories/Repository.js +215 -215
- package/lib/repositories/RequestLogRepository.d.ts +10 -10
- package/lib/repositories/RequestLogRepository.js +54 -54
- package/lib/repositories/SaveResult.d.ts +14 -14
- package/lib/repositories/SaveResult.js +18 -18
- package/lib/resources/AdminNotificationsResource.d.ts +3 -3
- package/lib/resources/AdminNotificationsResource.js +23 -23
- package/lib/resources/parents.d.ts +4 -4
- package/lib/resources/parents.js +7 -7
- package/lib/router/index.d.ts +8 -8
- package/lib/router/index.js +104 -104
- package/lib/types/DashbordConfig.d.ts +21 -21
- package/lib/types/DashbordConfig.js +2 -2
- package/lib/types/IConfigurationFile.d.ts +11 -11
- package/lib/types/IConfigurationFile.js +2 -2
- package/lib/types/IResourceFile.d.ts +719 -719
- package/lib/types/IResourceFile.js +2 -2
- package/lib/types/IResourceResponse.d.ts +95 -95
- package/lib/types/IResourceResponse.js +2 -2
- package/lib/types/helpers.d.ts +67 -67
- package/lib/types/helpers.js +82 -82
- package/lib/utils/DateUtils.d.ts +7 -7
- package/lib/utils/DateUtils.js +45 -45
- package/lib/utils/ResourceUtils.d.ts +2 -2
- package/lib/utils/ResourceUtils.js +7 -7
- package/lib/utils/ResponseUtils.d.ts +11 -11
- package/lib/utils/ResponseUtils.js +41 -41
- package/lib/utils/StringUtils.d.ts +9 -9
- package/lib/utils/StringUtils.js +46 -46
- package/lib/view/asset-manifest.json +18 -18
- package/lib/view/assets/icons/delete_icon.svg +3 -3
- package/lib/view/assets/icons/flags/ic_flag_cn.svg +9 -9
- package/lib/view/assets/icons/flags/ic_flag_sa.svg +9 -9
- package/lib/view/assets/icons/flags/ic_flag_vn.svg +9 -9
- package/lib/view/assets/icons/info_icon.svg +3 -3
- package/lib/view/assets/icons/navbar/ic_banking.svg +5 -5
- package/lib/view/assets/icons/navbar/ic_invoice.svg +4 -4
- package/lib/view/assets/icons/navbar/ic_kanban.svg +7 -7
- package/lib/view/assets/icons/navbar/ic_menu_item.svg +8 -8
- package/lib/view/assets/icons/small_info_icon.svg +3 -3
- package/lib/view/assets/illustrations/Group 16.svg +4 -4
- package/lib/view/assets/illustrations/logo.svg +5 -5
- package/lib/view/favicon/browserconfig.xml +9 -9
- package/lib/view/favicon/safari-pinned-tab.svg +182 -182
- package/lib/view/favicon/site.webmanifest +19 -19
- package/lib/view/fonts/index.css +18 -18
- package/lib/view/index.html +52 -52
- package/lib/view/manifest.json +20 -20
- package/lib/view/robots.txt +3 -3
- package/lib/view/static/css/main.54de06ef.css +1 -1
- package/lib/view/static/js/574.778b5963.chunk.js +1 -1
- package/lib/view/static/js/678.521704a3.chunk.js +1 -1
- package/lib/view/static/js/798.54856416.chunk.js +1 -1
- package/lib/view/static/js/main.4687f255.js +2 -2
- package/lib/view/static/js/main.4687f255.js.LICENSE.txt +214 -214
- package/package.json +63 -63
|
@@ -1,787 +1,787 @@
|
|
|
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
|
-
const ReportsGenerator_1 = __importDefault(require("../helpers/ReportsGenerator"));
|
|
41
|
-
const IUserActionsLog_1 = require("../models/userActionsLog/IUserActionsLog");
|
|
42
|
-
const UserActionsLog_1 = __importDefault(require("../models/userActionsLog/UserActionsLog"));
|
|
43
|
-
const PermissionResource_1 = require("../helpers/Permissions/PermissionResource");
|
|
44
|
-
const RecordsCounter_1 = require("../helpers/RecordsCounter");
|
|
45
|
-
const FilterQueryHelper_1 = require("../helpers/FilterQueryHelper");
|
|
46
|
-
let DashboardController = DashboardController_1 = class DashboardController {
|
|
47
|
-
constructor(resource, repository, resources, modelConfigurations) {
|
|
48
|
-
this.resource = resource;
|
|
49
|
-
this.repository = repository;
|
|
50
|
-
this.resources = resources;
|
|
51
|
-
this.modelConfigurations = modelConfigurations;
|
|
52
|
-
if (!repository && resource) {
|
|
53
|
-
this.repository = new Repository_1.default(resource.properties.resource);
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
validateRequest(req, res) {
|
|
57
|
-
const errors = (0, express_validator_1.validationResult)(req);
|
|
58
|
-
if (!errors.isEmpty()) {
|
|
59
|
-
ResponseUtils_1.default.send(res, 422, errors.array()[0].msg, errors);
|
|
60
|
-
return false;
|
|
61
|
-
}
|
|
62
|
-
return true;
|
|
63
|
-
}
|
|
64
|
-
paginateParams(request) {
|
|
65
|
-
const page = request.query.page ? +request.query.page : DashboardController_1.DEFAULT_PAGE;
|
|
66
|
-
const perPage = request.query.perPage ? +request.query.perPage : DashboardController_1.DEFAULT_PER_PAGE;
|
|
67
|
-
return { page, perPage };
|
|
68
|
-
}
|
|
69
|
-
getSearchableSubStringFilter(resource, filter, subString) {
|
|
70
|
-
var _a;
|
|
71
|
-
const schema = resource.properties.resource.schema.paths;
|
|
72
|
-
const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource, (_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(resource.properties.modelName));
|
|
73
|
-
if (searchBy === '_id') {
|
|
74
|
-
if (!mongoose_1.default.isValidObjectId(subString.source)) {
|
|
75
|
-
return filter;
|
|
76
|
-
}
|
|
77
|
-
filter[searchBy] = subString.source;
|
|
78
|
-
return filter;
|
|
79
|
-
}
|
|
80
|
-
if (LocalizedStringHelper_1.default.checkLocalizedStringType(resource, searchBy)) {
|
|
81
|
-
filter.$or = [LocalizedStringHelper_1.default.getFilterObject(searchBy, '0', subString), LocalizedStringHelper_1.default.getFilterObject(searchBy, '1', subString)];
|
|
82
|
-
}
|
|
83
|
-
else {
|
|
84
|
-
filter[searchBy] = subString;
|
|
85
|
-
}
|
|
86
|
-
return filter;
|
|
87
|
-
}
|
|
88
|
-
getScopeFilter(filter, scope) {
|
|
89
|
-
return {};
|
|
90
|
-
}
|
|
91
|
-
getVisibileExtraActions(currentUser, record, resource, repository, modelName) {
|
|
92
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
-
const data = {
|
|
94
|
-
record: record.toObject(),
|
|
95
|
-
currentUser,
|
|
96
|
-
resource: {
|
|
97
|
-
name: modelName,
|
|
98
|
-
path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
|
|
99
|
-
repository,
|
|
100
|
-
},
|
|
101
|
-
};
|
|
102
|
-
if (!resource.properties.actions || !resource.properties.actions.extras) {
|
|
103
|
-
return undefined;
|
|
104
|
-
}
|
|
105
|
-
var extraActionsArray = [];
|
|
106
|
-
const extraActions = resource.properties.actions.extras;
|
|
107
|
-
for (var i = 0; i < extraActions.length; i++) {
|
|
108
|
-
const extraAction = extraActions[i];
|
|
109
|
-
if (!extraAction.isVisible) {
|
|
110
|
-
extraActionsArray.push(extraAction.key);
|
|
111
|
-
continue;
|
|
112
|
-
}
|
|
113
|
-
const isVisible = yield extraAction.isVisible(data);
|
|
114
|
-
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, extraAction.key);
|
|
115
|
-
if (isVisible && havePermission) {
|
|
116
|
-
extraActionsArray.push(extraAction.key);
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
return extraActionsArray;
|
|
120
|
-
});
|
|
121
|
-
}
|
|
122
|
-
getHiddenActions(currentUser, record, resource, repository, modelName) {
|
|
123
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
-
const data = {
|
|
125
|
-
record: record.toObject(),
|
|
126
|
-
currentUser,
|
|
127
|
-
resource: {
|
|
128
|
-
name: modelName,
|
|
129
|
-
path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
|
|
130
|
-
repository,
|
|
131
|
-
},
|
|
132
|
-
};
|
|
133
|
-
if (!resource.properties.actions) {
|
|
134
|
-
return undefined;
|
|
135
|
-
}
|
|
136
|
-
let actionsArray = [];
|
|
137
|
-
const actions = resource.properties.actions;
|
|
138
|
-
for (const [key, action] of Object.entries(actions)) {
|
|
139
|
-
if (!(action === null || action === void 0 ? void 0 : action.isVisible)) {
|
|
140
|
-
continue;
|
|
141
|
-
}
|
|
142
|
-
const isVisible = yield action.isVisible(data);
|
|
143
|
-
if (!isVisible) {
|
|
144
|
-
actionsArray.push(key);
|
|
145
|
-
}
|
|
146
|
-
}
|
|
147
|
-
return actionsArray.length > 0 ? actionsArray : undefined;
|
|
148
|
-
});
|
|
149
|
-
}
|
|
150
|
-
getModelConfiguration(modelName) {
|
|
151
|
-
var _a, _b;
|
|
152
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
-
if ((_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(modelName)) {
|
|
154
|
-
return (_b = this.modelConfigurations) === null || _b === void 0 ? void 0 : _b.get(modelName);
|
|
155
|
-
}
|
|
156
|
-
const collection = mongoose_1.default.connection.db.collection('modelconfigurations');
|
|
157
|
-
return (yield collection.findOne({ modelName: modelName }));
|
|
158
|
-
});
|
|
159
|
-
}
|
|
160
|
-
index(req, res) {
|
|
161
|
-
var _a, _b, _c;
|
|
162
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
-
if (!this.validateRequest(req, res)) {
|
|
164
|
-
return;
|
|
165
|
-
}
|
|
166
|
-
const paginateParams = this.paginateParams(req);
|
|
167
|
-
const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
|
|
168
|
-
const scope = req.query.scope;
|
|
169
|
-
const currentUser = req.user;
|
|
170
|
-
const modelName = req.params.resource;
|
|
171
|
-
const filtersQuery = req.query.filters;
|
|
172
|
-
const filters = filtersQuery ? filtersQuery.split('^^') : undefined;
|
|
173
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
174
|
-
if (!resource) {
|
|
175
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
176
|
-
}
|
|
177
|
-
if (!currentUser) {
|
|
178
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
179
|
-
}
|
|
180
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
181
|
-
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_view`);
|
|
182
|
-
if (!permissionCheck || !havePermission) {
|
|
183
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
184
|
-
}
|
|
185
|
-
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
186
|
-
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
187
|
-
const sort = req.query.order;
|
|
188
|
-
const sortBy = req.query.orderBy;
|
|
189
|
-
const sortQuery = {};
|
|
190
|
-
sortQuery[sortBy] = sort;
|
|
191
|
-
if (sortBy !== '_id') {
|
|
192
|
-
sortQuery._id = 'asc';
|
|
193
|
-
}
|
|
194
|
-
var filter = {};
|
|
195
|
-
if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
|
|
196
|
-
if (resource.properties.filters.scopes.manual) {
|
|
197
|
-
filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
|
|
198
|
-
}
|
|
199
|
-
else if (resource.properties.filters.scopes.auto) {
|
|
200
|
-
const key = resource.properties.filters.scopes.auto.key;
|
|
201
|
-
if (key) {
|
|
202
|
-
filter[key] = scope;
|
|
203
|
-
}
|
|
204
|
-
}
|
|
205
|
-
}
|
|
206
|
-
if (searchableSubString) {
|
|
207
|
-
filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
|
|
208
|
-
}
|
|
209
|
-
filter = yield FiltersHelper_1.default.appendFilters(filter, filters, resource);
|
|
210
|
-
const crudOperations = resource.properties.crudOperations;
|
|
211
|
-
if (crudOperations && crudOperations.index && crudOperations.index.before) {
|
|
212
|
-
filter = yield crudOperations.index.before(req, filter, currentUser);
|
|
213
|
-
}
|
|
214
|
-
var records = [];
|
|
215
|
-
var pageInfo = undefined;
|
|
216
|
-
const populatedString = modifiedResource.properties.populatedString;
|
|
217
|
-
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.LIST, populatedString);
|
|
218
|
-
const modifiedPopulatedObject = yield populationHelper.get();
|
|
219
|
-
if (crudOperations && crudOperations.index && crudOperations.index.index) {
|
|
220
|
-
const page = yield crudOperations.index.index(req, filter, sortQuery, paginateParams, modifiedPopulatedObject, currentUser);
|
|
221
|
-
records = page.records;
|
|
222
|
-
pageInfo = page.pageInfo;
|
|
223
|
-
}
|
|
224
|
-
else {
|
|
225
|
-
if (!req.query.page && !req.query.perPage) {
|
|
226
|
-
records = yield repository.findMany({
|
|
227
|
-
sort: sortQuery,
|
|
228
|
-
filter,
|
|
229
|
-
populate: modifiedPopulatedObject,
|
|
230
|
-
});
|
|
231
|
-
}
|
|
232
|
-
else {
|
|
233
|
-
const page = yield repository.paginate({
|
|
234
|
-
sort: sortQuery,
|
|
235
|
-
filter,
|
|
236
|
-
paginateParams: paginateParams,
|
|
237
|
-
populate: modifiedPopulatedObject,
|
|
238
|
-
});
|
|
239
|
-
records = page.records;
|
|
240
|
-
pageInfo = page.pageInfo;
|
|
241
|
-
}
|
|
242
|
-
}
|
|
243
|
-
var documents = [];
|
|
244
|
-
for (var i = 0; i < records.length; i++) {
|
|
245
|
-
const record = records[i];
|
|
246
|
-
const extraActionKeys = yield this.getVisibileExtraActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
|
|
247
|
-
const hiddenActionKeys = yield this.getHiddenActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
|
|
248
|
-
const recordFlatten = record.toObject();
|
|
249
|
-
if (!extraActionKeys && !hiddenActionKeys) {
|
|
250
|
-
documents.push(record);
|
|
251
|
-
}
|
|
252
|
-
else {
|
|
253
|
-
if (extraActionKeys) {
|
|
254
|
-
recordFlatten.extraActionKeys = extraActionKeys;
|
|
255
|
-
}
|
|
256
|
-
if (hiddenActionKeys) {
|
|
257
|
-
recordFlatten.hiddenActionKeys = hiddenActionKeys;
|
|
258
|
-
}
|
|
259
|
-
documents.push(recordFlatten);
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
if (crudOperations && crudOperations.index && crudOperations.index.after) {
|
|
263
|
-
documents = yield crudOperations.index.after(req, documents, currentUser);
|
|
264
|
-
}
|
|
265
|
-
return ResponseUtils_1.default.send(res, 200, 'OK', {
|
|
266
|
-
records: documents,
|
|
267
|
-
pageInfo,
|
|
268
|
-
//options: modifiedResource
|
|
269
|
-
});
|
|
270
|
-
});
|
|
271
|
-
}
|
|
272
|
-
create(req, res) {
|
|
273
|
-
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
274
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
275
|
-
if (!this.validateRequest(req, res)) {
|
|
276
|
-
return;
|
|
277
|
-
}
|
|
278
|
-
const modelName = req.params.resource;
|
|
279
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
280
|
-
const currentUser = req.user;
|
|
281
|
-
if (!resource) {
|
|
282
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
283
|
-
}
|
|
284
|
-
if (!currentUser) {
|
|
285
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
286
|
-
}
|
|
287
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
288
|
-
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_create`);
|
|
289
|
-
if (!permissionCheck || !havePermission) {
|
|
290
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
291
|
-
}
|
|
292
|
-
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));
|
|
293
|
-
const createPermission = actions.new.isAccessible;
|
|
294
|
-
if (!createPermission) {
|
|
295
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
296
|
-
}
|
|
297
|
-
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
298
|
-
var recordParams = req.body;
|
|
299
|
-
recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
|
|
300
|
-
const crudOperations = resource.properties.crudOperations;
|
|
301
|
-
if (crudOperations && crudOperations.create && crudOperations.create.before) {
|
|
302
|
-
recordParams = yield crudOperations.create.before(req, recordParams, currentUser);
|
|
303
|
-
}
|
|
304
|
-
if (crudOperations && crudOperations.create && crudOperations.create.validators) {
|
|
305
|
-
for (let index = 0; index < crudOperations.create.validators.length; index++) {
|
|
306
|
-
const validation = crudOperations.create.validators[index];
|
|
307
|
-
const result = yield validation.run(req);
|
|
308
|
-
if (!result.isEmpty()) {
|
|
309
|
-
return ResponseUtils_1.default.unprocessable(res, (_f = (result.array() && ((_e = result.array()[0]) === null || _e === void 0 ? void 0 : _e.msg))) !== null && _f !== void 0 ? _f : 'Invalid data', result.array());
|
|
310
|
-
}
|
|
311
|
-
}
|
|
312
|
-
}
|
|
313
|
-
var record;
|
|
314
|
-
if (recordParams.password) {
|
|
315
|
-
//const user = new Mongoose.prototype.model(modifiedResource.properties.modelName)(recordParams);
|
|
316
|
-
const user = new resource.properties.resource(recordParams);
|
|
317
|
-
const set = yield user.setPassword(recordParams.password);
|
|
318
|
-
record = yield repository.saveInstance(user);
|
|
319
|
-
}
|
|
320
|
-
else {
|
|
321
|
-
record = yield repository.save(recordParams);
|
|
322
|
-
}
|
|
323
|
-
if (!record.isValid() || !record.document) {
|
|
324
|
-
const errors = record.getErrors();
|
|
325
|
-
const errorMessage = errors && errors.length > 0 ? errors[0].message : 'Invalid Data';
|
|
326
|
-
return ResponseUtils_1.default.unprocessable(res, errorMessage, record.getErrors());
|
|
327
|
-
}
|
|
328
|
-
if ((_g = resource.properties) === null || _g === void 0 ? void 0 : _g.enableLog) {
|
|
329
|
-
const newRecord = yield repository.findById(record.getDocument()._id);
|
|
330
|
-
const userActionsLog = new UserActionsLog_1.default({
|
|
331
|
-
action: IUserActionsLog_1.UserActions.CREATE,
|
|
332
|
-
modelName: (_h = resource === null || resource === void 0 ? void 0 : resource.properties) === null || _h === void 0 ? void 0 : _h.modelName,
|
|
333
|
-
userId: req.user._id,
|
|
334
|
-
dataAfter: JSON.stringify(newRecord),
|
|
335
|
-
resourceId: newRecord === null || newRecord === void 0 ? void 0 : newRecord._id,
|
|
336
|
-
});
|
|
337
|
-
yield userActionsLog.save();
|
|
338
|
-
}
|
|
339
|
-
if (crudOperations && crudOperations.create && crudOperations.create.after) {
|
|
340
|
-
record = yield crudOperations.create.after(req, record, currentUser, recordParams);
|
|
341
|
-
}
|
|
342
|
-
return ResponseUtils_1.default.created(res, {
|
|
343
|
-
record,
|
|
344
|
-
});
|
|
345
|
-
});
|
|
346
|
-
}
|
|
347
|
-
update(req, res) {
|
|
348
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
349
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
350
|
-
if (!this.validateRequest(req, res)) {
|
|
351
|
-
return;
|
|
352
|
-
}
|
|
353
|
-
const modelName = req.params.resource;
|
|
354
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
355
|
-
if (!resource) {
|
|
356
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
357
|
-
}
|
|
358
|
-
var recordParams = req.body;
|
|
359
|
-
const currentUser = req.user;
|
|
360
|
-
if (!currentUser) {
|
|
361
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
362
|
-
}
|
|
363
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
364
|
-
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_edit`);
|
|
365
|
-
if (!permissionCheck || !havePermission) {
|
|
366
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
367
|
-
}
|
|
368
|
-
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));
|
|
369
|
-
const editPermission = actions.edit.isAccessible;
|
|
370
|
-
if (!editPermission) {
|
|
371
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
372
|
-
}
|
|
373
|
-
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
374
|
-
const recordId = req.params.id;
|
|
375
|
-
var record = yield repository.findById(recordId);
|
|
376
|
-
if (!record) {
|
|
377
|
-
return ResponseUtils_1.default.send(res, 404, 'record Not Found');
|
|
378
|
-
}
|
|
379
|
-
const crudOperations = resource.properties.crudOperations;
|
|
380
|
-
if (crudOperations && crudOperations.update && crudOperations.update.before) {
|
|
381
|
-
recordParams = yield crudOperations.update.before(req, recordParams, currentUser);
|
|
382
|
-
}
|
|
383
|
-
if (crudOperations && crudOperations.update && crudOperations.update.validators) {
|
|
384
|
-
for (let index = 0; index < crudOperations.update.validators.length; index++) {
|
|
385
|
-
const validation = crudOperations.update.validators[index];
|
|
386
|
-
const result = yield validation.run(req);
|
|
387
|
-
if (!result.isEmpty()) {
|
|
388
|
-
return ResponseUtils_1.default.unprocessable(res, (_f = (result.array() && ((_e = result.array()[0]) === null || _e === void 0 ? void 0 : _e.msg))) !== null && _f !== void 0 ? _f : 'Invalid data', result.array());
|
|
389
|
-
}
|
|
390
|
-
}
|
|
391
|
-
}
|
|
392
|
-
var recordSaveResult = {};
|
|
393
|
-
if ((_g = crudOperations === null || crudOperations === void 0 ? void 0 : crudOperations.update) === null || _g === void 0 ? void 0 : _g.update) {
|
|
394
|
-
recordSaveResult = yield crudOperations.update.update(req, record, recordParams, currentUser);
|
|
395
|
-
}
|
|
396
|
-
else {
|
|
397
|
-
recordSaveResult = yield repository.update(record, recordParams);
|
|
398
|
-
}
|
|
399
|
-
if (!recordSaveResult.isValid()) {
|
|
400
|
-
return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', recordSaveResult.getErrors());
|
|
401
|
-
}
|
|
402
|
-
// if (resource.properties.modelName === ModelNames.Settings) {
|
|
403
|
-
// await AppSettings.run()
|
|
404
|
-
// }
|
|
405
|
-
if ((_h = resource.properties) === null || _h === void 0 ? void 0 : _h.enableLog) {
|
|
406
|
-
const newRecord = yield repository.findById(record._id);
|
|
407
|
-
const userActionsLog = new UserActionsLog_1.default({
|
|
408
|
-
action: IUserActionsLog_1.UserActions.EDIT,
|
|
409
|
-
modelName: (_j = resource === null || resource === void 0 ? void 0 : resource.properties) === null || _j === void 0 ? void 0 : _j.modelName,
|
|
410
|
-
userId: req.user._id,
|
|
411
|
-
dataBefore: JSON.stringify(record),
|
|
412
|
-
dataAfter: JSON.stringify(newRecord),
|
|
413
|
-
resourceId: record._id,
|
|
414
|
-
});
|
|
415
|
-
yield userActionsLog.save();
|
|
416
|
-
}
|
|
417
|
-
if (crudOperations && crudOperations.update && crudOperations.update.after) {
|
|
418
|
-
recordSaveResult = yield crudOperations.update.after(req, recordSaveResult, recordParams, currentUser);
|
|
419
|
-
}
|
|
420
|
-
return ResponseUtils_1.default.ok(res, {
|
|
421
|
-
record: recordSaveResult,
|
|
422
|
-
});
|
|
423
|
-
});
|
|
424
|
-
}
|
|
425
|
-
report(req, res) {
|
|
426
|
-
var _a, _b, _c;
|
|
427
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
428
|
-
if (!this.validateRequest(req, res)) {
|
|
429
|
-
return;
|
|
430
|
-
}
|
|
431
|
-
const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
|
|
432
|
-
const scope = req.query.scope;
|
|
433
|
-
const currentUser = req.user;
|
|
434
|
-
const modelName = req.params.resource;
|
|
435
|
-
const language = req.headers.language ? req.headers.language : 'en';
|
|
436
|
-
const filtersQuery = req.query.filters;
|
|
437
|
-
const filters = filtersQuery ? filtersQuery.split('^^') : undefined;
|
|
438
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
439
|
-
if (!resource) {
|
|
440
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
441
|
-
}
|
|
442
|
-
if (!currentUser) {
|
|
443
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
444
|
-
}
|
|
445
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
446
|
-
if (!permissionCheck) {
|
|
447
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
448
|
-
}
|
|
449
|
-
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
450
|
-
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
451
|
-
const sort = req.query.order;
|
|
452
|
-
const sortBy = req.query.orderBy;
|
|
453
|
-
const sortQuery = {};
|
|
454
|
-
sortQuery[sortBy] = sort;
|
|
455
|
-
if (sortBy !== '_id') {
|
|
456
|
-
sortQuery._id = 'asc';
|
|
457
|
-
}
|
|
458
|
-
var filter = {};
|
|
459
|
-
if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
|
|
460
|
-
if (resource.properties.filters.scopes.manual) {
|
|
461
|
-
filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
|
|
462
|
-
}
|
|
463
|
-
else if (resource.properties.filters.scopes.auto) {
|
|
464
|
-
const key = resource.properties.filters.scopes.auto.key;
|
|
465
|
-
if (key) {
|
|
466
|
-
filter[key] = scope;
|
|
467
|
-
}
|
|
468
|
-
}
|
|
469
|
-
}
|
|
470
|
-
if (searchableSubString) {
|
|
471
|
-
filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
|
|
472
|
-
}
|
|
473
|
-
filter = yield FiltersHelper_1.default.appendFilters(filter, filters, resource);
|
|
474
|
-
const crudOperations = resource.properties.crudOperations;
|
|
475
|
-
if (crudOperations && crudOperations.index && crudOperations.index.before) {
|
|
476
|
-
filter = yield crudOperations.index.before(req, filter, currentUser);
|
|
477
|
-
}
|
|
478
|
-
var records = [];
|
|
479
|
-
const populatedString = modifiedResource.properties.populatedString;
|
|
480
|
-
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.LIST, populatedString);
|
|
481
|
-
const modifiedPopulatedObject = yield populationHelper.get();
|
|
482
|
-
records = yield repository.findMany({
|
|
483
|
-
sort: sortQuery,
|
|
484
|
-
filter,
|
|
485
|
-
populate: modifiedPopulatedObject,
|
|
486
|
-
});
|
|
487
|
-
var documents = [];
|
|
488
|
-
for (var i = 0; i < records.length; i++) {
|
|
489
|
-
const record = records[i];
|
|
490
|
-
const recordFlatten = record.toObject();
|
|
491
|
-
documents.push(recordFlatten);
|
|
492
|
-
}
|
|
493
|
-
if (crudOperations && crudOperations.index && crudOperations.index.after) {
|
|
494
|
-
try {
|
|
495
|
-
documents = yield crudOperations.index.after(req, documents, currentUser);
|
|
496
|
-
}
|
|
497
|
-
catch (e) {
|
|
498
|
-
documents = yield crudOperations.index.after(req, records, currentUser);
|
|
499
|
-
}
|
|
500
|
-
}
|
|
501
|
-
const fields = req.query.fields.map((v) => JSON.parse(v));
|
|
502
|
-
const fileType = req.query.fileType;
|
|
503
|
-
if (fileType === 'xlsx') {
|
|
504
|
-
const buffer = ReportsGenerator_1.default.CreateXlsxFile(fields, documents, modelName, language, resource);
|
|
505
|
-
res.writeHead(200, {
|
|
506
|
-
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
507
|
-
'Content-disposition': 'attachment;filename=' + `${modelName}.xlsx`,
|
|
508
|
-
'Content-Length': buffer.length,
|
|
509
|
-
});
|
|
510
|
-
res.end(buffer);
|
|
511
|
-
}
|
|
512
|
-
if (fileType === 'pdf') {
|
|
513
|
-
const buffer = yield ReportsGenerator_1.default.CreatePdfFile(fields, documents, modelName, language, resource);
|
|
514
|
-
res.writeHead(200, {
|
|
515
|
-
'Content-Type': 'application/pdf',
|
|
516
|
-
'Content-disposition': 'attachment;filename=' + `${modelName}.pdf`,
|
|
517
|
-
'Content-Length': buffer.length,
|
|
518
|
-
});
|
|
519
|
-
res.end(buffer);
|
|
520
|
-
}
|
|
521
|
-
});
|
|
522
|
-
}
|
|
523
|
-
show(req, res) {
|
|
524
|
-
var _a, _b, _c, _d, _e;
|
|
525
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
526
|
-
if (!this.validateRequest(req, res)) {
|
|
527
|
-
return;
|
|
528
|
-
}
|
|
529
|
-
const modelName = req.params.resource;
|
|
530
|
-
const recordId = req.params.id;
|
|
531
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
532
|
-
const currentUser = req.user;
|
|
533
|
-
const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
|
|
534
|
-
if (!resource) {
|
|
535
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
536
|
-
}
|
|
537
|
-
if (!currentUser) {
|
|
538
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
539
|
-
}
|
|
540
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
541
|
-
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_view`);
|
|
542
|
-
if (!permissionCheck || !havePermission) {
|
|
543
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
544
|
-
}
|
|
545
|
-
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
546
|
-
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
547
|
-
const populatedString = modifiedResource.properties.populatedString;
|
|
548
|
-
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
|
|
549
|
-
const modifiedPopulatedObject = yield populationHelper.get();
|
|
550
|
-
const crudOperations = resource.properties.crudOperations;
|
|
551
|
-
var filter = {};
|
|
552
|
-
if (crudOperations && crudOperations.show && crudOperations.show.before) {
|
|
553
|
-
filter = yield crudOperations.show.before(req, filter, currentUser);
|
|
554
|
-
}
|
|
555
|
-
filter._id = recordId;
|
|
556
|
-
var record = yield repository.findOne({
|
|
557
|
-
filter,
|
|
558
|
-
populate: modifiedPopulatedObject,
|
|
559
|
-
});
|
|
560
|
-
if (!record) {
|
|
561
|
-
return ResponseUtils_1.default.send(res, 404, 'record not found');
|
|
562
|
-
}
|
|
563
|
-
const recordPageResult = yield RecordsCounter_1.RecordsCounter.count(req, resource, repository, record, this.modelConfigurations);
|
|
564
|
-
const hiddenActionKeys = yield this.getHiddenActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
|
|
565
|
-
record = record.toObject();
|
|
566
|
-
if (hiddenActionKeys && record) {
|
|
567
|
-
record.hiddenActionKeys = hiddenActionKeys;
|
|
568
|
-
}
|
|
569
|
-
record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
|
|
570
|
-
const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
|
|
571
|
-
let extras = {};
|
|
572
|
-
if (afterMethod && record) {
|
|
573
|
-
extras = yield afterMethod(req, record);
|
|
574
|
-
}
|
|
575
|
-
return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null, numberOfRecords: recordPageResult === null || recordPageResult === void 0 ? void 0 : recordPageResult.recordsCount, recordIndex: recordPageResult === null || recordPageResult === void 0 ? void 0 : recordPageResult.recordIndex }));
|
|
576
|
-
});
|
|
577
|
-
}
|
|
578
|
-
deleteAll(req, res) {
|
|
579
|
-
var _a, _b, _c;
|
|
580
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
581
|
-
if (!this.validateRequest(req, res)) {
|
|
582
|
-
return;
|
|
583
|
-
}
|
|
584
|
-
const modelName = req.params.resource;
|
|
585
|
-
const recordIds = req.body;
|
|
586
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
587
|
-
if (!resource) {
|
|
588
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
589
|
-
}
|
|
590
|
-
const currentUser = req.user;
|
|
591
|
-
if (!currentUser) {
|
|
592
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
593
|
-
}
|
|
594
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
595
|
-
if (!permissionCheck) {
|
|
596
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
597
|
-
}
|
|
598
|
-
const configuration = yield this.getModelConfiguration(resource.properties.modelName);
|
|
599
|
-
const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, configuration);
|
|
600
|
-
const bulkDeletePermission = actions.bulkDelete.isAccessible;
|
|
601
|
-
if (!bulkDeletePermission) {
|
|
602
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
603
|
-
}
|
|
604
|
-
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
605
|
-
for (var i = 0; i < recordIds.length; i++) {
|
|
606
|
-
const recordId = recordIds[i];
|
|
607
|
-
const record = yield repository.findById(recordId);
|
|
608
|
-
if (!record) {
|
|
609
|
-
continue;
|
|
610
|
-
}
|
|
611
|
-
yield repository.remove({ _id: record._id });
|
|
612
|
-
}
|
|
613
|
-
return ResponseUtils_1.default.send(res, 200, 'OK');
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
delete(req, res) {
|
|
617
|
-
var _a, _b, _c, _d, _e, _f;
|
|
618
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
619
|
-
if (!this.validateRequest(req, res)) {
|
|
620
|
-
return;
|
|
621
|
-
}
|
|
622
|
-
const modelName = req.params.resource;
|
|
623
|
-
const recordId = req.params.id;
|
|
624
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
625
|
-
if (!resource) {
|
|
626
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
627
|
-
}
|
|
628
|
-
const currentUser = req.user;
|
|
629
|
-
if (!currentUser) {
|
|
630
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
631
|
-
}
|
|
632
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
633
|
-
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_delete`);
|
|
634
|
-
if (!permissionCheck || !havePermission) {
|
|
635
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
636
|
-
}
|
|
637
|
-
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));
|
|
638
|
-
const deletePermission = actions.delete.isAccessible;
|
|
639
|
-
if (!deletePermission) {
|
|
640
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
641
|
-
}
|
|
642
|
-
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
643
|
-
const record = yield repository.findById(recordId);
|
|
644
|
-
if (!record) {
|
|
645
|
-
return ResponseUtils_1.default.send(res, 404, 'record Not Found');
|
|
646
|
-
}
|
|
647
|
-
if ((_e = resource.properties) === null || _e === void 0 ? void 0 : _e.enableLog) {
|
|
648
|
-
const userActionsLog = new UserActionsLog_1.default({
|
|
649
|
-
action: IUserActionsLog_1.UserActions.DELETE,
|
|
650
|
-
modelName: (_f = resource === null || resource === void 0 ? void 0 : resource.properties) === null || _f === void 0 ? void 0 : _f.modelName,
|
|
651
|
-
userId: req.user._id,
|
|
652
|
-
dataBefore: JSON.stringify(record),
|
|
653
|
-
resourceId: record._id,
|
|
654
|
-
});
|
|
655
|
-
yield userActionsLog.save();
|
|
656
|
-
}
|
|
657
|
-
yield repository.remove({ _id: record._id });
|
|
658
|
-
return ResponseUtils_1.default.send(res, 200, 'OK');
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
getNeighbors(req, res) {
|
|
662
|
-
var _a, _b, _c;
|
|
663
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
664
|
-
if (!this.validateRequest(req, res)) {
|
|
665
|
-
return;
|
|
666
|
-
}
|
|
667
|
-
const modelName = req.params.resource;
|
|
668
|
-
const recordId = req.params.id;
|
|
669
|
-
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
670
|
-
const currentUser = req.user;
|
|
671
|
-
const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
|
|
672
|
-
const neighborRecordType = req.query.type;
|
|
673
|
-
if (!resource || !neighborRecordType) {
|
|
674
|
-
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
675
|
-
}
|
|
676
|
-
if (!currentUser) {
|
|
677
|
-
return ResponseUtils_1.default.unauthorized(res);
|
|
678
|
-
}
|
|
679
|
-
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
680
|
-
if (!permissionCheck) {
|
|
681
|
-
return ResponseUtils_1.default.forbidden(res);
|
|
682
|
-
}
|
|
683
|
-
if (neighborRecordType !== helpers_1.NeighborTypes.NEXT && neighborRecordType !== helpers_1.NeighborTypes.PREVIOUS) {
|
|
684
|
-
return ResponseUtils_1.default.notFound(res, 'Type in query should match NEXT or PREVIOUS', []);
|
|
685
|
-
}
|
|
686
|
-
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
687
|
-
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
688
|
-
const populatedString = modifiedResource.properties.populatedString;
|
|
689
|
-
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
|
|
690
|
-
const modifiedPopulatedObject = yield populationHelper.get();
|
|
691
|
-
var record = yield repository.findOne({
|
|
692
|
-
filter: {
|
|
693
|
-
_id: recordId,
|
|
694
|
-
},
|
|
695
|
-
populate: modifiedPopulatedObject,
|
|
696
|
-
});
|
|
697
|
-
if (!record) {
|
|
698
|
-
return ResponseUtils_1.default.send(res, 404, 'record not found');
|
|
699
|
-
}
|
|
700
|
-
record = record.toObject();
|
|
701
|
-
record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
|
|
702
|
-
const sortBy = modifiedResource.properties.defaultOrderBy;
|
|
703
|
-
const sort = modifiedResource.properties.defaultOrder;
|
|
704
|
-
const nextQuery = {};
|
|
705
|
-
const prevQuery = {};
|
|
706
|
-
const nextSortQuery = {};
|
|
707
|
-
const previousSortQuery = {};
|
|
708
|
-
const greaterThanQuery = { $gt: record[sortBy] };
|
|
709
|
-
const lessThanQuery = { $lt: record[sortBy] };
|
|
710
|
-
nextQuery[sortBy] = sort === 'asc' ? greaterThanQuery : lessThanQuery;
|
|
711
|
-
prevQuery[sortBy] = sort === 'asc' ? lessThanQuery : greaterThanQuery;
|
|
712
|
-
nextSortQuery[sortBy] = sort;
|
|
713
|
-
previousSortQuery[sortBy] = sort === 'asc' ? 'desc' : 'asc';
|
|
714
|
-
const handler = resource.properties.crudOperations && resource.properties.crudOperations.show && resource.properties.crudOperations.show.nextPreviousButtonHandler
|
|
715
|
-
? resource.properties.crudOperations.show.nextPreviousButtonHandler
|
|
716
|
-
: undefined;
|
|
717
|
-
var neighbor;
|
|
718
|
-
const filter = yield FilterQueryHelper_1.FilterQueryHelper.getFilter(req, resource, this.modelConfigurations);
|
|
719
|
-
if (neighborRecordType === helpers_1.NeighborTypes.NEXT) {
|
|
720
|
-
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(handler ? yield handler(req, nextQuery, currentUser, neighborRecordType) : Object.assign(Object.assign({}, nextQuery), filter)).sort(nextSortQuery).limit(1));
|
|
721
|
-
}
|
|
722
|
-
else {
|
|
723
|
-
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(handler ? yield handler(req, prevQuery, currentUser, neighborRecordType) : Object.assign(Object.assign({}, prevQuery), filter)).sort(previousSortQuery).limit(1));
|
|
724
|
-
}
|
|
725
|
-
const hasNeighbor = record && neighbor && neighbor._id ? true : false;
|
|
726
|
-
const neighborRecordId = hasNeighbor ? neighbor._id : undefined;
|
|
727
|
-
return ResponseUtils_1.default.ok(res, {
|
|
728
|
-
hasNeighbor,
|
|
729
|
-
neighborRecordId,
|
|
730
|
-
});
|
|
731
|
-
});
|
|
732
|
-
}
|
|
733
|
-
};
|
|
734
|
-
DashboardController.DEFAULT_PAGE = 1;
|
|
735
|
-
DashboardController.DEFAULT_PER_PAGE = 30;
|
|
736
|
-
__decorate([
|
|
737
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
738
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
739
|
-
], DashboardController.prototype, "validateRequest", null);
|
|
740
|
-
__decorate([
|
|
741
|
-
(0, inversify_express_utils_1.httpGet)('/'),
|
|
742
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
743
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
744
|
-
], DashboardController.prototype, "index", null);
|
|
745
|
-
__decorate([
|
|
746
|
-
(0, inversify_express_utils_1.httpPost)('/'),
|
|
747
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
748
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
749
|
-
], DashboardController.prototype, "create", null);
|
|
750
|
-
__decorate([
|
|
751
|
-
(0, inversify_express_utils_1.httpPatch)('/:id'),
|
|
752
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
753
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
754
|
-
], DashboardController.prototype, "update", null);
|
|
755
|
-
__decorate([
|
|
756
|
-
(0, inversify_express_utils_1.httpGet)('/report'),
|
|
757
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
758
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
759
|
-
], DashboardController.prototype, "report", null);
|
|
760
|
-
__decorate([
|
|
761
|
-
(0, inversify_express_utils_1.httpGet)('/:id'),
|
|
762
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
763
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
764
|
-
], DashboardController.prototype, "show", null);
|
|
765
|
-
__decorate([
|
|
766
|
-
(0, inversify_express_utils_1.httpDelete)('/all'),
|
|
767
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
768
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
769
|
-
], DashboardController.prototype, "deleteAll", null);
|
|
770
|
-
__decorate([
|
|
771
|
-
(0, inversify_express_utils_1.httpDelete)('/:id'),
|
|
772
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
773
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
774
|
-
], DashboardController.prototype, "delete", null);
|
|
775
|
-
__decorate([
|
|
776
|
-
(0, inversify_express_utils_1.httpGet)('/neighbors/:id'),
|
|
777
|
-
__param(0, (0, inversify_express_utils_1.request)()),
|
|
778
|
-
__param(1, (0, inversify_express_utils_1.response)())
|
|
779
|
-
], DashboardController.prototype, "getNeighbors", null);
|
|
780
|
-
DashboardController = DashboardController_1 = __decorate([
|
|
781
|
-
(0, inversify_1.injectable)(),
|
|
782
|
-
__param(0, (0, inversify_1.unmanaged)()),
|
|
783
|
-
__param(1, (0, inversify_1.unmanaged)()),
|
|
784
|
-
__param(2, (0, inversify_1.unmanaged)()),
|
|
785
|
-
__param(3, (0, inversify_1.unmanaged)())
|
|
786
|
-
], DashboardController);
|
|
787
|
-
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
|
+
const ReportsGenerator_1 = __importDefault(require("../helpers/ReportsGenerator"));
|
|
41
|
+
const IUserActionsLog_1 = require("../models/userActionsLog/IUserActionsLog");
|
|
42
|
+
const UserActionsLog_1 = __importDefault(require("../models/userActionsLog/UserActionsLog"));
|
|
43
|
+
const PermissionResource_1 = require("../helpers/Permissions/PermissionResource");
|
|
44
|
+
const RecordsCounter_1 = require("../helpers/RecordsCounter");
|
|
45
|
+
const FilterQueryHelper_1 = require("../helpers/FilterQueryHelper");
|
|
46
|
+
let DashboardController = DashboardController_1 = class DashboardController {
|
|
47
|
+
constructor(resource, repository, resources, modelConfigurations) {
|
|
48
|
+
this.resource = resource;
|
|
49
|
+
this.repository = repository;
|
|
50
|
+
this.resources = resources;
|
|
51
|
+
this.modelConfigurations = modelConfigurations;
|
|
52
|
+
if (!repository && resource) {
|
|
53
|
+
this.repository = new Repository_1.default(resource.properties.resource);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
validateRequest(req, res) {
|
|
57
|
+
const errors = (0, express_validator_1.validationResult)(req);
|
|
58
|
+
if (!errors.isEmpty()) {
|
|
59
|
+
ResponseUtils_1.default.send(res, 422, errors.array()[0].msg, errors);
|
|
60
|
+
return false;
|
|
61
|
+
}
|
|
62
|
+
return true;
|
|
63
|
+
}
|
|
64
|
+
paginateParams(request) {
|
|
65
|
+
const page = request.query.page ? +request.query.page : DashboardController_1.DEFAULT_PAGE;
|
|
66
|
+
const perPage = request.query.perPage ? +request.query.perPage : DashboardController_1.DEFAULT_PER_PAGE;
|
|
67
|
+
return { page, perPage };
|
|
68
|
+
}
|
|
69
|
+
getSearchableSubStringFilter(resource, filter, subString) {
|
|
70
|
+
var _a;
|
|
71
|
+
const schema = resource.properties.resource.schema.paths;
|
|
72
|
+
const searchBy = ResourceHelper_1.default.getSchemaTitle(schema, this.resource, (_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(resource.properties.modelName));
|
|
73
|
+
if (searchBy === '_id') {
|
|
74
|
+
if (!mongoose_1.default.isValidObjectId(subString.source)) {
|
|
75
|
+
return filter;
|
|
76
|
+
}
|
|
77
|
+
filter[searchBy] = subString.source;
|
|
78
|
+
return filter;
|
|
79
|
+
}
|
|
80
|
+
if (LocalizedStringHelper_1.default.checkLocalizedStringType(resource, searchBy)) {
|
|
81
|
+
filter.$or = [LocalizedStringHelper_1.default.getFilterObject(searchBy, '0', subString), LocalizedStringHelper_1.default.getFilterObject(searchBy, '1', subString)];
|
|
82
|
+
}
|
|
83
|
+
else {
|
|
84
|
+
filter[searchBy] = subString;
|
|
85
|
+
}
|
|
86
|
+
return filter;
|
|
87
|
+
}
|
|
88
|
+
getScopeFilter(filter, scope) {
|
|
89
|
+
return {};
|
|
90
|
+
}
|
|
91
|
+
getVisibileExtraActions(currentUser, record, resource, repository, modelName) {
|
|
92
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
93
|
+
const data = {
|
|
94
|
+
record: record.toObject(),
|
|
95
|
+
currentUser,
|
|
96
|
+
resource: {
|
|
97
|
+
name: modelName,
|
|
98
|
+
path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
|
|
99
|
+
repository,
|
|
100
|
+
},
|
|
101
|
+
};
|
|
102
|
+
if (!resource.properties.actions || !resource.properties.actions.extras) {
|
|
103
|
+
return undefined;
|
|
104
|
+
}
|
|
105
|
+
var extraActionsArray = [];
|
|
106
|
+
const extraActions = resource.properties.actions.extras;
|
|
107
|
+
for (var i = 0; i < extraActions.length; i++) {
|
|
108
|
+
const extraAction = extraActions[i];
|
|
109
|
+
if (!extraAction.isVisible) {
|
|
110
|
+
extraActionsArray.push(extraAction.key);
|
|
111
|
+
continue;
|
|
112
|
+
}
|
|
113
|
+
const isVisible = yield extraAction.isVisible(data);
|
|
114
|
+
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, extraAction.key);
|
|
115
|
+
if (isVisible && havePermission) {
|
|
116
|
+
extraActionsArray.push(extraAction.key);
|
|
117
|
+
}
|
|
118
|
+
}
|
|
119
|
+
return extraActionsArray;
|
|
120
|
+
});
|
|
121
|
+
}
|
|
122
|
+
getHiddenActions(currentUser, record, resource, repository, modelName) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
const data = {
|
|
125
|
+
record: record.toObject(),
|
|
126
|
+
currentUser,
|
|
127
|
+
resource: {
|
|
128
|
+
name: modelName,
|
|
129
|
+
path: StringUtils_1.default.lowerCaseFirstLetter(modelName),
|
|
130
|
+
repository,
|
|
131
|
+
},
|
|
132
|
+
};
|
|
133
|
+
if (!resource.properties.actions) {
|
|
134
|
+
return undefined;
|
|
135
|
+
}
|
|
136
|
+
let actionsArray = [];
|
|
137
|
+
const actions = resource.properties.actions;
|
|
138
|
+
for (const [key, action] of Object.entries(actions)) {
|
|
139
|
+
if (!(action === null || action === void 0 ? void 0 : action.isVisible)) {
|
|
140
|
+
continue;
|
|
141
|
+
}
|
|
142
|
+
const isVisible = yield action.isVisible(data);
|
|
143
|
+
if (!isVisible) {
|
|
144
|
+
actionsArray.push(key);
|
|
145
|
+
}
|
|
146
|
+
}
|
|
147
|
+
return actionsArray.length > 0 ? actionsArray : undefined;
|
|
148
|
+
});
|
|
149
|
+
}
|
|
150
|
+
getModelConfiguration(modelName) {
|
|
151
|
+
var _a, _b;
|
|
152
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
153
|
+
if ((_a = this.modelConfigurations) === null || _a === void 0 ? void 0 : _a.get(modelName)) {
|
|
154
|
+
return (_b = this.modelConfigurations) === null || _b === void 0 ? void 0 : _b.get(modelName);
|
|
155
|
+
}
|
|
156
|
+
const collection = mongoose_1.default.connection.db.collection('modelconfigurations');
|
|
157
|
+
return (yield collection.findOne({ modelName: modelName }));
|
|
158
|
+
});
|
|
159
|
+
}
|
|
160
|
+
index(req, res) {
|
|
161
|
+
var _a, _b, _c;
|
|
162
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
163
|
+
if (!this.validateRequest(req, res)) {
|
|
164
|
+
return;
|
|
165
|
+
}
|
|
166
|
+
const paginateParams = this.paginateParams(req);
|
|
167
|
+
const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
|
|
168
|
+
const scope = req.query.scope;
|
|
169
|
+
const currentUser = req.user;
|
|
170
|
+
const modelName = req.params.resource;
|
|
171
|
+
const filtersQuery = req.query.filters;
|
|
172
|
+
const filters = filtersQuery ? filtersQuery.split('^^') : undefined;
|
|
173
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
174
|
+
if (!resource) {
|
|
175
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
176
|
+
}
|
|
177
|
+
if (!currentUser) {
|
|
178
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
179
|
+
}
|
|
180
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
181
|
+
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_view`);
|
|
182
|
+
if (!permissionCheck || !havePermission) {
|
|
183
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
184
|
+
}
|
|
185
|
+
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
186
|
+
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
187
|
+
const sort = req.query.order;
|
|
188
|
+
const sortBy = req.query.orderBy;
|
|
189
|
+
const sortQuery = {};
|
|
190
|
+
sortQuery[sortBy] = sort;
|
|
191
|
+
if (sortBy !== '_id') {
|
|
192
|
+
sortQuery._id = 'asc';
|
|
193
|
+
}
|
|
194
|
+
var filter = {};
|
|
195
|
+
if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
|
|
196
|
+
if (resource.properties.filters.scopes.manual) {
|
|
197
|
+
filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
|
|
198
|
+
}
|
|
199
|
+
else if (resource.properties.filters.scopes.auto) {
|
|
200
|
+
const key = resource.properties.filters.scopes.auto.key;
|
|
201
|
+
if (key) {
|
|
202
|
+
filter[key] = scope;
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
}
|
|
206
|
+
if (searchableSubString) {
|
|
207
|
+
filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
|
|
208
|
+
}
|
|
209
|
+
filter = yield FiltersHelper_1.default.appendFilters(filter, filters, resource);
|
|
210
|
+
const crudOperations = resource.properties.crudOperations;
|
|
211
|
+
if (crudOperations && crudOperations.index && crudOperations.index.before) {
|
|
212
|
+
filter = yield crudOperations.index.before(req, filter, currentUser);
|
|
213
|
+
}
|
|
214
|
+
var records = [];
|
|
215
|
+
var pageInfo = undefined;
|
|
216
|
+
const populatedString = modifiedResource.properties.populatedString;
|
|
217
|
+
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.LIST, populatedString);
|
|
218
|
+
const modifiedPopulatedObject = yield populationHelper.get();
|
|
219
|
+
if (crudOperations && crudOperations.index && crudOperations.index.index) {
|
|
220
|
+
const page = yield crudOperations.index.index(req, filter, sortQuery, paginateParams, modifiedPopulatedObject, currentUser);
|
|
221
|
+
records = page.records;
|
|
222
|
+
pageInfo = page.pageInfo;
|
|
223
|
+
}
|
|
224
|
+
else {
|
|
225
|
+
if (!req.query.page && !req.query.perPage) {
|
|
226
|
+
records = yield repository.findMany({
|
|
227
|
+
sort: sortQuery,
|
|
228
|
+
filter,
|
|
229
|
+
populate: modifiedPopulatedObject,
|
|
230
|
+
});
|
|
231
|
+
}
|
|
232
|
+
else {
|
|
233
|
+
const page = yield repository.paginate({
|
|
234
|
+
sort: sortQuery,
|
|
235
|
+
filter,
|
|
236
|
+
paginateParams: paginateParams,
|
|
237
|
+
populate: modifiedPopulatedObject,
|
|
238
|
+
});
|
|
239
|
+
records = page.records;
|
|
240
|
+
pageInfo = page.pageInfo;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
var documents = [];
|
|
244
|
+
for (var i = 0; i < records.length; i++) {
|
|
245
|
+
const record = records[i];
|
|
246
|
+
const extraActionKeys = yield this.getVisibileExtraActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
|
|
247
|
+
const hiddenActionKeys = yield this.getHiddenActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
|
|
248
|
+
const recordFlatten = record.toObject();
|
|
249
|
+
if (!extraActionKeys && !hiddenActionKeys) {
|
|
250
|
+
documents.push(record);
|
|
251
|
+
}
|
|
252
|
+
else {
|
|
253
|
+
if (extraActionKeys) {
|
|
254
|
+
recordFlatten.extraActionKeys = extraActionKeys;
|
|
255
|
+
}
|
|
256
|
+
if (hiddenActionKeys) {
|
|
257
|
+
recordFlatten.hiddenActionKeys = hiddenActionKeys;
|
|
258
|
+
}
|
|
259
|
+
documents.push(recordFlatten);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
if (crudOperations && crudOperations.index && crudOperations.index.after) {
|
|
263
|
+
documents = yield crudOperations.index.after(req, documents, currentUser);
|
|
264
|
+
}
|
|
265
|
+
return ResponseUtils_1.default.send(res, 200, 'OK', {
|
|
266
|
+
records: documents,
|
|
267
|
+
pageInfo,
|
|
268
|
+
//options: modifiedResource
|
|
269
|
+
});
|
|
270
|
+
});
|
|
271
|
+
}
|
|
272
|
+
create(req, res) {
|
|
273
|
+
var _a, _b, _c, _d, _e, _f, _g, _h;
|
|
274
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
275
|
+
if (!this.validateRequest(req, res)) {
|
|
276
|
+
return;
|
|
277
|
+
}
|
|
278
|
+
const modelName = req.params.resource;
|
|
279
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
280
|
+
const currentUser = req.user;
|
|
281
|
+
if (!resource) {
|
|
282
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
283
|
+
}
|
|
284
|
+
if (!currentUser) {
|
|
285
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
286
|
+
}
|
|
287
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
288
|
+
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_create`);
|
|
289
|
+
if (!permissionCheck || !havePermission) {
|
|
290
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
291
|
+
}
|
|
292
|
+
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));
|
|
293
|
+
const createPermission = actions.new.isAccessible;
|
|
294
|
+
if (!createPermission) {
|
|
295
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
296
|
+
}
|
|
297
|
+
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
298
|
+
var recordParams = req.body;
|
|
299
|
+
recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
|
|
300
|
+
const crudOperations = resource.properties.crudOperations;
|
|
301
|
+
if (crudOperations && crudOperations.create && crudOperations.create.before) {
|
|
302
|
+
recordParams = yield crudOperations.create.before(req, recordParams, currentUser);
|
|
303
|
+
}
|
|
304
|
+
if (crudOperations && crudOperations.create && crudOperations.create.validators) {
|
|
305
|
+
for (let index = 0; index < crudOperations.create.validators.length; index++) {
|
|
306
|
+
const validation = crudOperations.create.validators[index];
|
|
307
|
+
const result = yield validation.run(req);
|
|
308
|
+
if (!result.isEmpty()) {
|
|
309
|
+
return ResponseUtils_1.default.unprocessable(res, (_f = (result.array() && ((_e = result.array()[0]) === null || _e === void 0 ? void 0 : _e.msg))) !== null && _f !== void 0 ? _f : 'Invalid data', result.array());
|
|
310
|
+
}
|
|
311
|
+
}
|
|
312
|
+
}
|
|
313
|
+
var record;
|
|
314
|
+
if (recordParams.password) {
|
|
315
|
+
//const user = new Mongoose.prototype.model(modifiedResource.properties.modelName)(recordParams);
|
|
316
|
+
const user = new resource.properties.resource(recordParams);
|
|
317
|
+
const set = yield user.setPassword(recordParams.password);
|
|
318
|
+
record = yield repository.saveInstance(user);
|
|
319
|
+
}
|
|
320
|
+
else {
|
|
321
|
+
record = yield repository.save(recordParams);
|
|
322
|
+
}
|
|
323
|
+
if (!record.isValid() || !record.document) {
|
|
324
|
+
const errors = record.getErrors();
|
|
325
|
+
const errorMessage = errors && errors.length > 0 ? errors[0].message : 'Invalid Data';
|
|
326
|
+
return ResponseUtils_1.default.unprocessable(res, errorMessage, record.getErrors());
|
|
327
|
+
}
|
|
328
|
+
if ((_g = resource.properties) === null || _g === void 0 ? void 0 : _g.enableLog) {
|
|
329
|
+
const newRecord = yield repository.findById(record.getDocument()._id);
|
|
330
|
+
const userActionsLog = new UserActionsLog_1.default({
|
|
331
|
+
action: IUserActionsLog_1.UserActions.CREATE,
|
|
332
|
+
modelName: (_h = resource === null || resource === void 0 ? void 0 : resource.properties) === null || _h === void 0 ? void 0 : _h.modelName,
|
|
333
|
+
userId: req.user._id,
|
|
334
|
+
dataAfter: JSON.stringify(newRecord),
|
|
335
|
+
resourceId: newRecord === null || newRecord === void 0 ? void 0 : newRecord._id,
|
|
336
|
+
});
|
|
337
|
+
yield userActionsLog.save();
|
|
338
|
+
}
|
|
339
|
+
if (crudOperations && crudOperations.create && crudOperations.create.after) {
|
|
340
|
+
record = yield crudOperations.create.after(req, record, currentUser, recordParams);
|
|
341
|
+
}
|
|
342
|
+
return ResponseUtils_1.default.created(res, {
|
|
343
|
+
record,
|
|
344
|
+
});
|
|
345
|
+
});
|
|
346
|
+
}
|
|
347
|
+
update(req, res) {
|
|
348
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
|
|
349
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
350
|
+
if (!this.validateRequest(req, res)) {
|
|
351
|
+
return;
|
|
352
|
+
}
|
|
353
|
+
const modelName = req.params.resource;
|
|
354
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
355
|
+
if (!resource) {
|
|
356
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
357
|
+
}
|
|
358
|
+
var recordParams = req.body;
|
|
359
|
+
const currentUser = req.user;
|
|
360
|
+
if (!currentUser) {
|
|
361
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
362
|
+
}
|
|
363
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
364
|
+
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_edit`);
|
|
365
|
+
if (!permissionCheck || !havePermission) {
|
|
366
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
367
|
+
}
|
|
368
|
+
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));
|
|
369
|
+
const editPermission = actions.edit.isAccessible;
|
|
370
|
+
if (!editPermission) {
|
|
371
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
372
|
+
}
|
|
373
|
+
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
374
|
+
const recordId = req.params.id;
|
|
375
|
+
var record = yield repository.findById(recordId);
|
|
376
|
+
if (!record) {
|
|
377
|
+
return ResponseUtils_1.default.send(res, 404, 'record Not Found');
|
|
378
|
+
}
|
|
379
|
+
const crudOperations = resource.properties.crudOperations;
|
|
380
|
+
if (crudOperations && crudOperations.update && crudOperations.update.before) {
|
|
381
|
+
recordParams = yield crudOperations.update.before(req, recordParams, currentUser);
|
|
382
|
+
}
|
|
383
|
+
if (crudOperations && crudOperations.update && crudOperations.update.validators) {
|
|
384
|
+
for (let index = 0; index < crudOperations.update.validators.length; index++) {
|
|
385
|
+
const validation = crudOperations.update.validators[index];
|
|
386
|
+
const result = yield validation.run(req);
|
|
387
|
+
if (!result.isEmpty()) {
|
|
388
|
+
return ResponseUtils_1.default.unprocessable(res, (_f = (result.array() && ((_e = result.array()[0]) === null || _e === void 0 ? void 0 : _e.msg))) !== null && _f !== void 0 ? _f : 'Invalid data', result.array());
|
|
389
|
+
}
|
|
390
|
+
}
|
|
391
|
+
}
|
|
392
|
+
var recordSaveResult = {};
|
|
393
|
+
if ((_g = crudOperations === null || crudOperations === void 0 ? void 0 : crudOperations.update) === null || _g === void 0 ? void 0 : _g.update) {
|
|
394
|
+
recordSaveResult = yield crudOperations.update.update(req, record, recordParams, currentUser);
|
|
395
|
+
}
|
|
396
|
+
else {
|
|
397
|
+
recordSaveResult = yield repository.update(record, recordParams);
|
|
398
|
+
}
|
|
399
|
+
if (!recordSaveResult.isValid()) {
|
|
400
|
+
return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', recordSaveResult.getErrors());
|
|
401
|
+
}
|
|
402
|
+
// if (resource.properties.modelName === ModelNames.Settings) {
|
|
403
|
+
// await AppSettings.run()
|
|
404
|
+
// }
|
|
405
|
+
if ((_h = resource.properties) === null || _h === void 0 ? void 0 : _h.enableLog) {
|
|
406
|
+
const newRecord = yield repository.findById(record._id);
|
|
407
|
+
const userActionsLog = new UserActionsLog_1.default({
|
|
408
|
+
action: IUserActionsLog_1.UserActions.EDIT,
|
|
409
|
+
modelName: (_j = resource === null || resource === void 0 ? void 0 : resource.properties) === null || _j === void 0 ? void 0 : _j.modelName,
|
|
410
|
+
userId: req.user._id,
|
|
411
|
+
dataBefore: JSON.stringify(record),
|
|
412
|
+
dataAfter: JSON.stringify(newRecord),
|
|
413
|
+
resourceId: record._id,
|
|
414
|
+
});
|
|
415
|
+
yield userActionsLog.save();
|
|
416
|
+
}
|
|
417
|
+
if (crudOperations && crudOperations.update && crudOperations.update.after) {
|
|
418
|
+
recordSaveResult = yield crudOperations.update.after(req, recordSaveResult, recordParams, currentUser);
|
|
419
|
+
}
|
|
420
|
+
return ResponseUtils_1.default.ok(res, {
|
|
421
|
+
record: recordSaveResult,
|
|
422
|
+
});
|
|
423
|
+
});
|
|
424
|
+
}
|
|
425
|
+
report(req, res) {
|
|
426
|
+
var _a, _b, _c;
|
|
427
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
428
|
+
if (!this.validateRequest(req, res)) {
|
|
429
|
+
return;
|
|
430
|
+
}
|
|
431
|
+
const searchableSubString = req.query.filter ? new RegExp(req.query.filter, 'i') : undefined;
|
|
432
|
+
const scope = req.query.scope;
|
|
433
|
+
const currentUser = req.user;
|
|
434
|
+
const modelName = req.params.resource;
|
|
435
|
+
const language = req.headers.language ? req.headers.language : 'en';
|
|
436
|
+
const filtersQuery = req.query.filters;
|
|
437
|
+
const filters = filtersQuery ? filtersQuery.split('^^') : undefined;
|
|
438
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
439
|
+
if (!resource) {
|
|
440
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
441
|
+
}
|
|
442
|
+
if (!currentUser) {
|
|
443
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
444
|
+
}
|
|
445
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
446
|
+
if (!permissionCheck) {
|
|
447
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
448
|
+
}
|
|
449
|
+
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
450
|
+
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
451
|
+
const sort = req.query.order;
|
|
452
|
+
const sortBy = req.query.orderBy;
|
|
453
|
+
const sortQuery = {};
|
|
454
|
+
sortQuery[sortBy] = sort;
|
|
455
|
+
if (sortBy !== '_id') {
|
|
456
|
+
sortQuery._id = 'asc';
|
|
457
|
+
}
|
|
458
|
+
var filter = {};
|
|
459
|
+
if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
|
|
460
|
+
if (resource.properties.filters.scopes.manual) {
|
|
461
|
+
filter = yield resource.properties.filters.scopes.manual.handler(filter, scope, currentUser);
|
|
462
|
+
}
|
|
463
|
+
else if (resource.properties.filters.scopes.auto) {
|
|
464
|
+
const key = resource.properties.filters.scopes.auto.key;
|
|
465
|
+
if (key) {
|
|
466
|
+
filter[key] = scope;
|
|
467
|
+
}
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
if (searchableSubString) {
|
|
471
|
+
filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
|
|
472
|
+
}
|
|
473
|
+
filter = yield FiltersHelper_1.default.appendFilters(filter, filters, resource);
|
|
474
|
+
const crudOperations = resource.properties.crudOperations;
|
|
475
|
+
if (crudOperations && crudOperations.index && crudOperations.index.before) {
|
|
476
|
+
filter = yield crudOperations.index.before(req, filter, currentUser);
|
|
477
|
+
}
|
|
478
|
+
var records = [];
|
|
479
|
+
const populatedString = modifiedResource.properties.populatedString;
|
|
480
|
+
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.LIST, populatedString);
|
|
481
|
+
const modifiedPopulatedObject = yield populationHelper.get();
|
|
482
|
+
records = yield repository.findMany({
|
|
483
|
+
sort: sortQuery,
|
|
484
|
+
filter,
|
|
485
|
+
populate: modifiedPopulatedObject,
|
|
486
|
+
});
|
|
487
|
+
var documents = [];
|
|
488
|
+
for (var i = 0; i < records.length; i++) {
|
|
489
|
+
const record = records[i];
|
|
490
|
+
const recordFlatten = record.toObject();
|
|
491
|
+
documents.push(recordFlatten);
|
|
492
|
+
}
|
|
493
|
+
if (crudOperations && crudOperations.index && crudOperations.index.after) {
|
|
494
|
+
try {
|
|
495
|
+
documents = yield crudOperations.index.after(req, documents, currentUser);
|
|
496
|
+
}
|
|
497
|
+
catch (e) {
|
|
498
|
+
documents = yield crudOperations.index.after(req, records, currentUser);
|
|
499
|
+
}
|
|
500
|
+
}
|
|
501
|
+
const fields = req.query.fields.map((v) => JSON.parse(v));
|
|
502
|
+
const fileType = req.query.fileType;
|
|
503
|
+
if (fileType === 'xlsx') {
|
|
504
|
+
const buffer = ReportsGenerator_1.default.CreateXlsxFile(fields, documents, modelName, language, resource);
|
|
505
|
+
res.writeHead(200, {
|
|
506
|
+
'Content-Type': 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
|
|
507
|
+
'Content-disposition': 'attachment;filename=' + `${modelName}.xlsx`,
|
|
508
|
+
'Content-Length': buffer.length,
|
|
509
|
+
});
|
|
510
|
+
res.end(buffer);
|
|
511
|
+
}
|
|
512
|
+
if (fileType === 'pdf') {
|
|
513
|
+
const buffer = yield ReportsGenerator_1.default.CreatePdfFile(fields, documents, modelName, language, resource);
|
|
514
|
+
res.writeHead(200, {
|
|
515
|
+
'Content-Type': 'application/pdf',
|
|
516
|
+
'Content-disposition': 'attachment;filename=' + `${modelName}.pdf`,
|
|
517
|
+
'Content-Length': buffer.length,
|
|
518
|
+
});
|
|
519
|
+
res.end(buffer);
|
|
520
|
+
}
|
|
521
|
+
});
|
|
522
|
+
}
|
|
523
|
+
show(req, res) {
|
|
524
|
+
var _a, _b, _c, _d, _e;
|
|
525
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
526
|
+
if (!this.validateRequest(req, res)) {
|
|
527
|
+
return;
|
|
528
|
+
}
|
|
529
|
+
const modelName = req.params.resource;
|
|
530
|
+
const recordId = req.params.id;
|
|
531
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
532
|
+
const currentUser = req.user;
|
|
533
|
+
const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
|
|
534
|
+
if (!resource) {
|
|
535
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
536
|
+
}
|
|
537
|
+
if (!currentUser) {
|
|
538
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
539
|
+
}
|
|
540
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
541
|
+
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_view`);
|
|
542
|
+
if (!permissionCheck || !havePermission) {
|
|
543
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
544
|
+
}
|
|
545
|
+
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
546
|
+
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
547
|
+
const populatedString = modifiedResource.properties.populatedString;
|
|
548
|
+
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
|
|
549
|
+
const modifiedPopulatedObject = yield populationHelper.get();
|
|
550
|
+
const crudOperations = resource.properties.crudOperations;
|
|
551
|
+
var filter = {};
|
|
552
|
+
if (crudOperations && crudOperations.show && crudOperations.show.before) {
|
|
553
|
+
filter = yield crudOperations.show.before(req, filter, currentUser);
|
|
554
|
+
}
|
|
555
|
+
filter._id = recordId;
|
|
556
|
+
var record = yield repository.findOne({
|
|
557
|
+
filter,
|
|
558
|
+
populate: modifiedPopulatedObject,
|
|
559
|
+
});
|
|
560
|
+
if (!record) {
|
|
561
|
+
return ResponseUtils_1.default.send(res, 404, 'record not found');
|
|
562
|
+
}
|
|
563
|
+
const recordPageResult = yield RecordsCounter_1.RecordsCounter.count(req, resource, repository, record, this.modelConfigurations);
|
|
564
|
+
const hiddenActionKeys = yield this.getHiddenActions(currentUser, record, resource, repository, modifiedResource.properties.modelName);
|
|
565
|
+
record = record.toObject();
|
|
566
|
+
if (hiddenActionKeys && record) {
|
|
567
|
+
record.hiddenActionKeys = hiddenActionKeys;
|
|
568
|
+
}
|
|
569
|
+
record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
|
|
570
|
+
const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
|
|
571
|
+
let extras = {};
|
|
572
|
+
if (afterMethod && record) {
|
|
573
|
+
extras = yield afterMethod(req, record);
|
|
574
|
+
}
|
|
575
|
+
return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null, numberOfRecords: recordPageResult === null || recordPageResult === void 0 ? void 0 : recordPageResult.recordsCount, recordIndex: recordPageResult === null || recordPageResult === void 0 ? void 0 : recordPageResult.recordIndex }));
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
deleteAll(req, res) {
|
|
579
|
+
var _a, _b, _c;
|
|
580
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
581
|
+
if (!this.validateRequest(req, res)) {
|
|
582
|
+
return;
|
|
583
|
+
}
|
|
584
|
+
const modelName = req.params.resource;
|
|
585
|
+
const recordIds = req.body;
|
|
586
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
587
|
+
if (!resource) {
|
|
588
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
589
|
+
}
|
|
590
|
+
const currentUser = req.user;
|
|
591
|
+
if (!currentUser) {
|
|
592
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
593
|
+
}
|
|
594
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
595
|
+
if (!permissionCheck) {
|
|
596
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
597
|
+
}
|
|
598
|
+
const configuration = yield this.getModelConfiguration(resource.properties.modelName);
|
|
599
|
+
const actions = ActionsGenerator_1.default.generateActions(resource.properties.actions, resource, currentUser, configuration);
|
|
600
|
+
const bulkDeletePermission = actions.bulkDelete.isAccessible;
|
|
601
|
+
if (!bulkDeletePermission) {
|
|
602
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
603
|
+
}
|
|
604
|
+
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
605
|
+
for (var i = 0; i < recordIds.length; i++) {
|
|
606
|
+
const recordId = recordIds[i];
|
|
607
|
+
const record = yield repository.findById(recordId);
|
|
608
|
+
if (!record) {
|
|
609
|
+
continue;
|
|
610
|
+
}
|
|
611
|
+
yield repository.remove({ _id: record._id });
|
|
612
|
+
}
|
|
613
|
+
return ResponseUtils_1.default.send(res, 200, 'OK');
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
delete(req, res) {
|
|
617
|
+
var _a, _b, _c, _d, _e, _f;
|
|
618
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
619
|
+
if (!this.validateRequest(req, res)) {
|
|
620
|
+
return;
|
|
621
|
+
}
|
|
622
|
+
const modelName = req.params.resource;
|
|
623
|
+
const recordId = req.params.id;
|
|
624
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
625
|
+
if (!resource) {
|
|
626
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
627
|
+
}
|
|
628
|
+
const currentUser = req.user;
|
|
629
|
+
if (!currentUser) {
|
|
630
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
631
|
+
}
|
|
632
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
633
|
+
const havePermission = yield PermissionResource_1.PermissionResource.havePermission(currentUser.permissionGroupId, `${StringUtils_1.default.lowerCaseFirstLetter(resource.properties.modelName)}_delete`);
|
|
634
|
+
if (!permissionCheck || !havePermission) {
|
|
635
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
636
|
+
}
|
|
637
|
+
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));
|
|
638
|
+
const deletePermission = actions.delete.isAccessible;
|
|
639
|
+
if (!deletePermission) {
|
|
640
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
641
|
+
}
|
|
642
|
+
const repository = (_d = this.repository) !== null && _d !== void 0 ? _d : new Repository_1.default(resource.properties.resource);
|
|
643
|
+
const record = yield repository.findById(recordId);
|
|
644
|
+
if (!record) {
|
|
645
|
+
return ResponseUtils_1.default.send(res, 404, 'record Not Found');
|
|
646
|
+
}
|
|
647
|
+
if ((_e = resource.properties) === null || _e === void 0 ? void 0 : _e.enableLog) {
|
|
648
|
+
const userActionsLog = new UserActionsLog_1.default({
|
|
649
|
+
action: IUserActionsLog_1.UserActions.DELETE,
|
|
650
|
+
modelName: (_f = resource === null || resource === void 0 ? void 0 : resource.properties) === null || _f === void 0 ? void 0 : _f.modelName,
|
|
651
|
+
userId: req.user._id,
|
|
652
|
+
dataBefore: JSON.stringify(record),
|
|
653
|
+
resourceId: record._id,
|
|
654
|
+
});
|
|
655
|
+
yield userActionsLog.save();
|
|
656
|
+
}
|
|
657
|
+
yield repository.remove({ _id: record._id });
|
|
658
|
+
return ResponseUtils_1.default.send(res, 200, 'OK');
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
getNeighbors(req, res) {
|
|
662
|
+
var _a, _b, _c;
|
|
663
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
664
|
+
if (!this.validateRequest(req, res)) {
|
|
665
|
+
return;
|
|
666
|
+
}
|
|
667
|
+
const modelName = req.params.resource;
|
|
668
|
+
const recordId = req.params.id;
|
|
669
|
+
const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
|
|
670
|
+
const currentUser = req.user;
|
|
671
|
+
const model = resource === null || resource === void 0 ? void 0 : resource.properties.resource;
|
|
672
|
+
const neighborRecordType = req.query.type;
|
|
673
|
+
if (!resource || !neighborRecordType) {
|
|
674
|
+
return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
|
|
675
|
+
}
|
|
676
|
+
if (!currentUser) {
|
|
677
|
+
return ResponseUtils_1.default.unauthorized(res);
|
|
678
|
+
}
|
|
679
|
+
const permissionCheck = resource.properties.isAllowed ? yield resource.properties.isAllowed(currentUser) : true;
|
|
680
|
+
if (!permissionCheck) {
|
|
681
|
+
return ResponseUtils_1.default.forbidden(res);
|
|
682
|
+
}
|
|
683
|
+
if (neighborRecordType !== helpers_1.NeighborTypes.NEXT && neighborRecordType !== helpers_1.NeighborTypes.PREVIOUS) {
|
|
684
|
+
return ResponseUtils_1.default.notFound(res, 'Type in query should match NEXT or PREVIOUS', []);
|
|
685
|
+
}
|
|
686
|
+
const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
|
|
687
|
+
const modifiedResource = ResourceGenerator_1.default.generate(resource, currentUser, undefined, this.modelConfigurations);
|
|
688
|
+
const populatedString = modifiedResource.properties.populatedString;
|
|
689
|
+
const populationHelper = new PopulationHelper_1.PopulationHelper(resource, PopulationHelper_1.PopulationType.SHOW, populatedString);
|
|
690
|
+
const modifiedPopulatedObject = yield populationHelper.get();
|
|
691
|
+
var record = yield repository.findOne({
|
|
692
|
+
filter: {
|
|
693
|
+
_id: recordId,
|
|
694
|
+
},
|
|
695
|
+
populate: modifiedPopulatedObject,
|
|
696
|
+
});
|
|
697
|
+
if (!record) {
|
|
698
|
+
return ResponseUtils_1.default.send(res, 404, 'record not found');
|
|
699
|
+
}
|
|
700
|
+
record = record.toObject();
|
|
701
|
+
record = yield ResourceHelper_1.default.addExtraFields(modifiedResource.showProperties, modifiedResource.properties.model, record, StringUtils_1.default.lowerCaseFirstLetter(modifiedResource.properties.modelName), resource);
|
|
702
|
+
const sortBy = modifiedResource.properties.defaultOrderBy;
|
|
703
|
+
const sort = modifiedResource.properties.defaultOrder;
|
|
704
|
+
const nextQuery = {};
|
|
705
|
+
const prevQuery = {};
|
|
706
|
+
const nextSortQuery = {};
|
|
707
|
+
const previousSortQuery = {};
|
|
708
|
+
const greaterThanQuery = { $gt: record[sortBy] };
|
|
709
|
+
const lessThanQuery = { $lt: record[sortBy] };
|
|
710
|
+
nextQuery[sortBy] = sort === 'asc' ? greaterThanQuery : lessThanQuery;
|
|
711
|
+
prevQuery[sortBy] = sort === 'asc' ? lessThanQuery : greaterThanQuery;
|
|
712
|
+
nextSortQuery[sortBy] = sort;
|
|
713
|
+
previousSortQuery[sortBy] = sort === 'asc' ? 'desc' : 'asc';
|
|
714
|
+
const handler = resource.properties.crudOperations && resource.properties.crudOperations.show && resource.properties.crudOperations.show.nextPreviousButtonHandler
|
|
715
|
+
? resource.properties.crudOperations.show.nextPreviousButtonHandler
|
|
716
|
+
: undefined;
|
|
717
|
+
var neighbor;
|
|
718
|
+
const filter = yield FilterQueryHelper_1.FilterQueryHelper.getFilter(req, resource, this.modelConfigurations);
|
|
719
|
+
if (neighborRecordType === helpers_1.NeighborTypes.NEXT) {
|
|
720
|
+
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(handler ? yield handler(req, nextQuery, currentUser, neighborRecordType) : Object.assign(Object.assign({}, nextQuery), filter)).sort(nextSortQuery).limit(1));
|
|
721
|
+
}
|
|
722
|
+
else {
|
|
723
|
+
neighbor = yield (model === null || model === void 0 ? void 0 : model.findOne(handler ? yield handler(req, prevQuery, currentUser, neighborRecordType) : Object.assign(Object.assign({}, prevQuery), filter)).sort(previousSortQuery).limit(1));
|
|
724
|
+
}
|
|
725
|
+
const hasNeighbor = record && neighbor && neighbor._id ? true : false;
|
|
726
|
+
const neighborRecordId = hasNeighbor ? neighbor._id : undefined;
|
|
727
|
+
return ResponseUtils_1.default.ok(res, {
|
|
728
|
+
hasNeighbor,
|
|
729
|
+
neighborRecordId,
|
|
730
|
+
});
|
|
731
|
+
});
|
|
732
|
+
}
|
|
733
|
+
};
|
|
734
|
+
DashboardController.DEFAULT_PAGE = 1;
|
|
735
|
+
DashboardController.DEFAULT_PER_PAGE = 30;
|
|
736
|
+
__decorate([
|
|
737
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
738
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
739
|
+
], DashboardController.prototype, "validateRequest", null);
|
|
740
|
+
__decorate([
|
|
741
|
+
(0, inversify_express_utils_1.httpGet)('/'),
|
|
742
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
743
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
744
|
+
], DashboardController.prototype, "index", null);
|
|
745
|
+
__decorate([
|
|
746
|
+
(0, inversify_express_utils_1.httpPost)('/'),
|
|
747
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
748
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
749
|
+
], DashboardController.prototype, "create", null);
|
|
750
|
+
__decorate([
|
|
751
|
+
(0, inversify_express_utils_1.httpPatch)('/:id'),
|
|
752
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
753
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
754
|
+
], DashboardController.prototype, "update", null);
|
|
755
|
+
__decorate([
|
|
756
|
+
(0, inversify_express_utils_1.httpGet)('/report'),
|
|
757
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
758
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
759
|
+
], DashboardController.prototype, "report", null);
|
|
760
|
+
__decorate([
|
|
761
|
+
(0, inversify_express_utils_1.httpGet)('/:id'),
|
|
762
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
763
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
764
|
+
], DashboardController.prototype, "show", null);
|
|
765
|
+
__decorate([
|
|
766
|
+
(0, inversify_express_utils_1.httpDelete)('/all'),
|
|
767
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
768
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
769
|
+
], DashboardController.prototype, "deleteAll", null);
|
|
770
|
+
__decorate([
|
|
771
|
+
(0, inversify_express_utils_1.httpDelete)('/:id'),
|
|
772
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
773
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
774
|
+
], DashboardController.prototype, "delete", null);
|
|
775
|
+
__decorate([
|
|
776
|
+
(0, inversify_express_utils_1.httpGet)('/neighbors/:id'),
|
|
777
|
+
__param(0, (0, inversify_express_utils_1.request)()),
|
|
778
|
+
__param(1, (0, inversify_express_utils_1.response)())
|
|
779
|
+
], DashboardController.prototype, "getNeighbors", null);
|
|
780
|
+
DashboardController = DashboardController_1 = __decorate([
|
|
781
|
+
(0, inversify_1.injectable)(),
|
|
782
|
+
__param(0, (0, inversify_1.unmanaged)()),
|
|
783
|
+
__param(1, (0, inversify_1.unmanaged)()),
|
|
784
|
+
__param(2, (0, inversify_1.unmanaged)()),
|
|
785
|
+
__param(3, (0, inversify_1.unmanaged)())
|
|
786
|
+
], DashboardController);
|
|
787
|
+
exports.default = DashboardController;
|