identity-admin 1.6.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -32,5 +32,6 @@ export default class DashboardController {
32
32
  create(req: IRequest, res: Response): Promise<void>;
33
33
  update(req: IRequest, res: Response): Promise<void | Response<any, Record<string, any>>>;
34
34
  show(req: IRequest, res: Response): Promise<void | Response<any, Record<string, any>>>;
35
+ deleteAll(req: IRequest, res: Response): Promise<void | Response<any, Record<string, any>>>;
35
36
  delete(req: IRequest, res: Response): Promise<void | Response<any, Record<string, any>>>;
36
37
  }
@@ -34,6 +34,7 @@ const inversify_1 = require("inversify");
34
34
  const Repository_1 = __importDefault(require("../repositories/Repository"));
35
35
  const ResourceUtils_1 = require("../utils/ResourceUtils");
36
36
  const FiltersHelper_1 = __importDefault(require("../helpers/FiltersHelper"));
37
+ const LocalizedStringHelper_1 = __importDefault(require("../helpers/LocalizedStringHelper"));
37
38
  let DashboardController = DashboardController_1 = class DashboardController {
38
39
  constructor(resource, repository, resources) {
39
40
  this.resource = resource;
@@ -66,7 +67,15 @@ let DashboardController = DashboardController_1 = class DashboardController {
66
67
  filter[searchBy] = subString.source;
67
68
  return filter;
68
69
  }
69
- filter[searchBy] = subString;
70
+ if (LocalizedStringHelper_1.default.checkLocalizedStringType(resource, searchBy)) {
71
+ filter.$or = [
72
+ LocalizedStringHelper_1.default.getFilterObject(searchBy, "0", subString),
73
+ LocalizedStringHelper_1.default.getFilterObject(searchBy, "1", subString),
74
+ ];
75
+ }
76
+ else {
77
+ filter[searchBy] = subString;
78
+ }
70
79
  return filter;
71
80
  }
72
81
  getScopeFilter(filter, scope) {
@@ -129,10 +138,6 @@ let DashboardController = DashboardController_1 = class DashboardController {
129
138
  }
130
139
  const populate = req.query.populate;
131
140
  var filter = {};
132
- const crudOperations = resource.properties.crudOperations;
133
- if (crudOperations && crudOperations.index) {
134
- filter = crudOperations.index.before(filter, currentUser);
135
- }
136
141
  if (scope && resource.properties.filters && resource.properties.filters.scopes && resource.properties.filters.scopes.isAccessible) {
137
142
  if (resource.properties.filters.scopes.manual) {
138
143
  filter = resource.properties.filters.scopes.manual.handler(filter, scope);
@@ -148,6 +153,10 @@ let DashboardController = DashboardController_1 = class DashboardController {
148
153
  filter = this.getSearchableSubStringFilter(resource, filter, searchableSubString);
149
154
  }
150
155
  filter = FiltersHelper_1.default.appendFilters(filter, filters);
156
+ const crudOperations = resource.properties.crudOperations;
157
+ if (crudOperations && crudOperations.index) {
158
+ filter = crudOperations.index.before(req, filter, currentUser);
159
+ }
151
160
  var records = [];
152
161
  var pageInfo = undefined;
153
162
  if (!req.query.page && !req.query.perPage) {
@@ -202,9 +211,10 @@ let DashboardController = DashboardController_1 = class DashboardController {
202
211
  const modifiedResource = ResourceGenerator_1.default.generate(resource);
203
212
  var recordParams = req.body;
204
213
  const currentUser = req.user;
214
+ recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
205
215
  const crudOperations = resource.properties.crudOperations;
206
216
  if (crudOperations && crudOperations.create) {
207
- recordParams = crudOperations.create.before(recordParams, currentUser);
217
+ recordParams = crudOperations.create.before(req, recordParams, currentUser);
208
218
  }
209
219
  var record;
210
220
  if (recordParams.password) {
@@ -241,6 +251,7 @@ let DashboardController = DashboardController_1 = class DashboardController {
241
251
  if (!record) {
242
252
  return ResponseUtils_1.default.send(res, 404, 'record Not Found');
243
253
  }
254
+ recordParams = LocalizedStringHelper_1.default.mapLocalizableString(recordParams, resource);
244
255
  const recordSaveResult = yield repository.update(record, recordParams);
245
256
  if (!recordSaveResult.isValid()) {
246
257
  return ResponseUtils_1.default.unprocessable(res, 'Invalid Data', recordSaveResult.getErrors());
@@ -282,11 +293,35 @@ let DashboardController = DashboardController_1 = class DashboardController {
282
293
  const afterMethod = (_e = (_d = resource.properties.crudOperations) === null || _d === void 0 ? void 0 : _d.show) === null || _e === void 0 ? void 0 : _e.after;
283
294
  let extras = {};
284
295
  if (afterMethod && record) {
285
- extras = yield afterMethod(record);
296
+ extras = yield afterMethod(req, record);
286
297
  }
287
298
  return ResponseUtils_1.default.ok(res, Object.assign(Object.assign({}, extras), { record: record ? record : null }));
288
299
  });
289
300
  }
301
+ deleteAll(req, res) {
302
+ var _a, _b, _c;
303
+ return __awaiter(this, void 0, void 0, function* () {
304
+ if (!this.validateRequest(req, res)) {
305
+ return;
306
+ }
307
+ const modelName = req.params.resource;
308
+ const recordIds = req.body;
309
+ const resource = (_a = this.resource) !== null && _a !== void 0 ? _a : (0, ResourceUtils_1.getResource)(modelName, (_b = this.resources) !== null && _b !== void 0 ? _b : []);
310
+ if (!resource) {
311
+ return ResponseUtils_1.default.notFound(res, 'Resource not found', []);
312
+ }
313
+ const repository = (_c = this.repository) !== null && _c !== void 0 ? _c : new Repository_1.default(resource.properties.resource);
314
+ for (var i = 0; i < recordIds.length; i++) {
315
+ const recordId = recordIds[i];
316
+ const record = yield repository.findById(recordId);
317
+ if (!record) {
318
+ continue;
319
+ }
320
+ yield repository.remove({ _id: record._id });
321
+ }
322
+ return ResponseUtils_1.default.send(res, 200, 'OK');
323
+ });
324
+ }
290
325
  delete(req, res) {
291
326
  var _a, _b, _c;
292
327
  return __awaiter(this, void 0, void 0, function* () {
@@ -335,6 +370,11 @@ __decorate([
335
370
  __param(0, (0, inversify_express_utils_1.request)()),
336
371
  __param(1, (0, inversify_express_utils_1.response)())
337
372
  ], DashboardController.prototype, "show", null);
373
+ __decorate([
374
+ (0, inversify_express_utils_1.httpDelete)('/all'),
375
+ __param(0, (0, inversify_express_utils_1.request)()),
376
+ __param(1, (0, inversify_express_utils_1.response)())
377
+ ], DashboardController.prototype, "deleteAll", null);
338
378
  __decorate([
339
379
  (0, inversify_express_utils_1.httpDelete)('/:id'),
340
380
  __param(0, (0, inversify_express_utils_1.request)()),
@@ -5,6 +5,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
6
  const mongoose_1 = __importDefault(require("mongoose"));
7
7
  const helpers_1 = require("../types/helpers");
8
+ const LocalizedStringHelper_1 = __importDefault(require("./LocalizedStringHelper"));
8
9
  var pluralize = require('pluralize');
9
10
  class FilterHelper {
10
11
  static appendFilters(filterQuery, filters) {
@@ -52,6 +53,12 @@ class FilterHelper {
52
53
  case helpers_1.FieldTypes.STRING:
53
54
  filterQuery[key] = new RegExp(value, 'i');
54
55
  break;
56
+ case helpers_1.FieldTypes.LOCALIZEDSTRING:
57
+ filterQuery.$or = [
58
+ LocalizedStringHelper_1.default.getFilterObject(key, "0", new RegExp(value, 'i')),
59
+ LocalizedStringHelper_1.default.getFilterObject(key, "1", new RegExp(value, 'i')),
60
+ ];
61
+ break;
55
62
  default:
56
63
  if (value !== 'null' && value !== 'undefined') {
57
64
  filterQuery[key] = value;
@@ -0,0 +1,9 @@
1
+ import { IResourceFile } from "../types/IResourceFile";
2
+ export default class LocalizedStringHelper {
3
+ static mapLocalizableString(recordParams: any, resource: IResourceFile): any;
4
+ private static getArrayOfLanguages;
5
+ static checkLocalizedStringType(resource: IResourceFile, fieldName: string): boolean;
6
+ static getFilterObject(searchBy: string, index: string, value: any): {
7
+ [key: string]: any;
8
+ };
9
+ }
@@ -0,0 +1,57 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ const helpers_1 = require("../types/helpers");
4
+ class LocalizedStringHelper {
5
+ static mapLocalizableString(recordParams, resource) {
6
+ const modelObject = resource.properties.model;
7
+ if (!modelObject) {
8
+ return recordParams;
9
+ }
10
+ for (const key in modelObject) {
11
+ if (!modelObject.hasOwnProperty(key) || key === 'virtuals') {
12
+ continue;
13
+ }
14
+ const castedModelObject = modelObject;
15
+ if (castedModelObject[key].type && castedModelObject[key].type === helpers_1.FieldTypes.LOCALIZEDSTRING && recordParams[key]) {
16
+ recordParams[key] = this.getArrayOfLanguages(recordParams[key]);
17
+ }
18
+ else if (castedModelObject[key].arrayType && castedModelObject[key].arrayType === helpers_1.FieldTypes.LOCALIZEDSTRING && recordParams[key] && recordParams[key].length !== 0) {
19
+ for (var j = 0; j < recordParams[key].length; j++) {
20
+ recordParams[key][j] = this.getArrayOfLanguages(recordParams[key][j]);
21
+ }
22
+ }
23
+ }
24
+ return recordParams;
25
+ }
26
+ static getArrayOfLanguages(record) {
27
+ var localizedArray = [];
28
+ for (const language in record) {
29
+ if (!record.hasOwnProperty(language)) {
30
+ continue;
31
+ }
32
+ const languageObject = {
33
+ key: language,
34
+ value: record[language] ? record[language] : undefined
35
+ };
36
+ localizedArray.push(languageObject);
37
+ }
38
+ return localizedArray;
39
+ }
40
+ static checkLocalizedStringType(resource, fieldName) {
41
+ const modelObject = resource.properties.model;
42
+ if (!modelObject || !modelObject.hasOwnProperty(fieldName)) {
43
+ return false;
44
+ }
45
+ const castedModelObject = modelObject;
46
+ if (castedModelObject[fieldName].type && castedModelObject[fieldName].type === helpers_1.FieldTypes.LOCALIZEDSTRING) {
47
+ return true;
48
+ }
49
+ return false;
50
+ }
51
+ static getFilterObject(searchBy, index, value) {
52
+ const filterObject = {};
53
+ filterObject[`${searchBy}.${index}.value`] = value;
54
+ return filterObject;
55
+ }
56
+ }
57
+ exports.default = LocalizedStringHelper;
@@ -48,7 +48,7 @@ class SchemaGenerator {
48
48
  model[field].arrayType = helpers_1.FieldTypes.REFERENCE;
49
49
  }
50
50
  else {
51
- model[field].arrayType = schema[field].caster.instance;
51
+ model[field].arrayType = model[field].arrayType ? model[field].arrayType : schema[field].caster.instance;
52
52
  }
53
53
  }
54
54
  model[field].type = model[field].type ? model[field].type : fieldType;
@@ -1,15 +1,28 @@
1
1
  import { Request } from "express";
2
2
  import { Document, Model } from "mongoose";
3
- import { ActionTypes, Virtuals } from "./helpers";
3
+ import { IRequest } from "../middlewares/isAuth";
4
+ import { ActionTypes, FieldTypes, Virtuals } from "./helpers";
4
5
  declare type orderTypes = 'asc' | 'desc';
5
6
  declare type VirtualFieldTypes = 'password' | 'ref' | 'Array';
6
7
  declare type Severity = 'success' | 'info' | 'warning' | 'error';
7
8
  interface IAutoScope {
9
+ /**
10
+ * Key of property as specified in the database
11
+ */
8
12
  key: string;
13
+ /**
14
+ * Array of values that this key can have
15
+ */
9
16
  options: string[];
10
17
  }
11
18
  interface IManualScope {
19
+ /**
20
+ * Array of values that will be appeared in the scope
21
+ */
12
22
  options: string[];
23
+ /**
24
+ * A handler that will be called when scope filter is used. This handler should perform a switch case on the specified options values, each case set the filter on its way.
25
+ */
13
26
  handler: (filter: {
14
27
  [key: string]: any;
15
28
  }, scope: string) => {
@@ -17,25 +30,42 @@ interface IManualScope {
17
30
  };
18
31
  }
19
32
  interface Parent {
33
+ /**
34
+ * Name of the parent
35
+ */
20
36
  name: string;
37
+ /**
38
+ * Icon of the parent. You can get the icons from
39
+ * @link https://mui.com/material-ui/material-icons/
40
+ */
21
41
  icon: string;
22
42
  }
23
43
  interface Action {
44
+ /**
45
+ * Specify if this action is accessible or not.
46
+ * @default True is the default for show, edit, delete, and new while false for bulk delete. You can ovveride any of these values here.
47
+ */
24
48
  isAccessible?: boolean;
25
49
  }
26
50
  interface ICrudOperations {
27
51
  index?: {
28
- before: (filter: {
52
+ /**
53
+ * Before handler that gives you the access to the filter object. You can add to the filter object any key and value that will be used in the filter query before getting the records for list. You should return the filter object
54
+ */
55
+ before: (req: IRequest, filter: {
29
56
  [key: string]: any;
30
57
  }, currentUser: Document) => {
31
58
  [key: string]: any;
32
59
  };
33
60
  };
34
61
  create?: {
35
- before: (params: any, currentUser: Document) => any;
62
+ /**
63
+ * Before handler that is called before creating a new record. This function gives you the access to the record params before saving it, you can perform any update to the params before saving it. This function should return the params
64
+ */
65
+ before: (req: IRequest, params: any, currentUser: Document) => any;
36
66
  };
37
67
  show?: {
38
- after: (record: Document) => Promise<{
68
+ after: (req: IRequest, record: Document) => Promise<{
39
69
  record: Document;
40
70
  [key: string]: any;
41
71
  }>;
@@ -64,6 +94,13 @@ export interface IFieldValue {
64
94
  required?: boolean;
65
95
  isEditable?: boolean;
66
96
  type?: string;
97
+ /**
98
+ * Specify the allowed country codes. Only mandatory if the type is phone number
99
+ * Alert message appear after executing this action.
100
+ * ex: ["sa", "eg", ...]
101
+ */
102
+ countryCodes?: string[];
103
+ arrayType?: FieldTypes;
67
104
  mediaUploader?: boolean;
68
105
  }
69
106
  export interface IVirtualValue {
@@ -24,5 +24,9 @@ export declare enum FieldTypes {
24
24
  OBJECTID = "ObjectID",
25
25
  NESTEDSCHEMA = "nestedSchema",
26
26
  ARRAY = "Array",
27
- IMAGE = "image"
27
+ IMAGE = "image",
28
+ LOCALIZEDSTRING = "localizedString",
29
+ PHONENUMBER = "phoneNumber",
30
+ COLOR = "color",
31
+ TIMEPICKER = "timePicker"
28
32
  }
@@ -32,4 +32,8 @@ var FieldTypes;
32
32
  FieldTypes["NESTEDSCHEMA"] = "nestedSchema";
33
33
  FieldTypes["ARRAY"] = "Array";
34
34
  FieldTypes["IMAGE"] = "image";
35
+ FieldTypes["LOCALIZEDSTRING"] = "localizedString";
36
+ FieldTypes["PHONENUMBER"] = "phoneNumber";
37
+ FieldTypes["COLOR"] = "color";
38
+ FieldTypes["TIMEPICKER"] = "timePicker";
35
39
  })(FieldTypes = exports.FieldTypes || (exports.FieldTypes = {}));
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "identity-admin",
3
- "version": "1.6.0",
3
+ "version": "1.8.0",
4
4
  "description": "",
5
5
  "main": "lib/Dashboard.js",
6
6
  "types": "lib/Dashbord.d.ts",