@sdflc/backend-helpers 1.0.69 → 1.0.71

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.
@@ -29,7 +29,7 @@ class BaseController {
29
29
  /* TODO */
30
30
  };
31
31
  }
32
- this.logger = props.logger instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.LOG });
32
+ this.logger = (props === null || props === void 0 ? void 0 : props.logger) instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
33
33
  }
34
34
  init(props) {
35
35
  // const { app } = props;
package/build/BaseCore.js CHANGED
@@ -40,9 +40,22 @@ const defaultValidators = {
40
40
  };
41
41
  class BaseCore {
42
42
  constructor(props) {
43
+ var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
43
44
  this.measureTool = new utils_1.MeasureTool();
44
- this.config = props;
45
- this.logger = props.logger;
45
+ this.config = props || {};
46
+ this.logger = this.config.logger instanceof utils_1.Logger ? this.config.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
47
+ this.config.doAuthOnAction = {
48
+ list: ((_a = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.list) != null ? (_b = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _b === void 0 ? void 0 : _b.list : props === null || props === void 0 ? void 0 : props.doAuth,
49
+ get: ((_c = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _c === void 0 ? void 0 : _c.get) != null ? (_d = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _d === void 0 ? void 0 : _d.get : props === null || props === void 0 ? void 0 : props.doAuth,
50
+ getMany: ((_e = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _e === void 0 ? void 0 : _e.getMany) != null ? (_f = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _f === void 0 ? void 0 : _f.getMany : props === null || props === void 0 ? void 0 : props.doAuth,
51
+ create: ((_g = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _g === void 0 ? void 0 : _g.create) != null ? (_h = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _h === void 0 ? void 0 : _h.create : props === null || props === void 0 ? void 0 : props.doAuth,
52
+ createMany: ((_j = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _j === void 0 ? void 0 : _j.createMany) != null ? (_k = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _k === void 0 ? void 0 : _k.createMany : props === null || props === void 0 ? void 0 : props.doAuth,
53
+ update: ((_l = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _l === void 0 ? void 0 : _l.update) != null ? (_m = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _m === void 0 ? void 0 : _m.update : props === null || props === void 0 ? void 0 : props.doAuth,
54
+ updateMany: ((_o = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _o === void 0 ? void 0 : _o.updateMany) != null ? (_p = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _p === void 0 ? void 0 : _p.updateMany : props === null || props === void 0 ? void 0 : props.doAuth,
55
+ set: ((_q = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _q === void 0 ? void 0 : _q.set) != null ? (_r = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _r === void 0 ? void 0 : _r.set : props === null || props === void 0 ? void 0 : props.doAuth,
56
+ remove: ((_s = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _s === void 0 ? void 0 : _s.remove) != null ? (_t = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _t === void 0 ? void 0 : _t.remove : props === null || props === void 0 ? void 0 : props.doAuth,
57
+ removeMany: ((_u = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _u === void 0 ? void 0 : _u.removeMany) != null ? (_v = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _v === void 0 ? void 0 : _v.removeMany : props === null || props === void 0 ? void 0 : props.doAuth,
58
+ };
46
59
  }
47
60
  getIdType() {
48
61
  var _a;
@@ -262,39 +275,49 @@ class BaseCore {
262
275
  });
263
276
  }
264
277
  list(args) {
265
- var _a, _b, _c;
278
+ var _a, _b, _c, _d;
266
279
  return __awaiter(this, void 0, void 0, function* () {
267
280
  return this.runAction({
268
281
  args,
282
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.list,
283
+ actionName: constants_1.ACTIONS.LIST,
269
284
  validate: this.getValidators().list,
270
285
  accessRights: [utils_1.ACCESS_RIGHTS.LIST],
271
286
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
272
- var _d;
287
+ var _e;
273
288
  const object = yield this.beforeList(args, opt);
274
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.list(object));
289
+ if (object instanceof api_helpers_1.OpResult) {
290
+ return object;
291
+ }
292
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.list(object));
275
293
  const result = yield this.afterList(items, opt);
276
294
  // ON NO DATA RETURN EMPTY CODE
277
295
  return this.success(result);
278
296
  }),
279
297
  hasTransaction: false,
280
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.list) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'listing',
298
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.list) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'listing',
281
299
  });
282
300
  });
283
301
  }
284
302
  get(args) {
285
- var _a, _b, _c;
303
+ var _a, _b, _c, _d;
286
304
  return __awaiter(this, void 0, void 0, function* () {
287
305
  return this.runAction({
288
306
  args,
307
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.get,
308
+ actionName: constants_1.ACTIONS.GET,
289
309
  validate: this.getValidators().get,
290
310
  accessRights: [utils_1.ACCESS_RIGHTS.GET],
291
311
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
292
- var _d;
312
+ var _e;
293
313
  const args2use = yield this.beforeGet(args, opt);
314
+ if (args2use instanceof api_helpers_1.OpResult) {
315
+ return args2use;
316
+ }
294
317
  if ((args2use === null || args2use === void 0 ? void 0 : args2use.id) == null) {
295
318
  return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, '', []);
296
319
  }
297
- const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.get(args2use === null || args2use === void 0 ? void 0 : args2use.id));
320
+ const item = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.get(args2use === null || args2use === void 0 ? void 0 : args2use.id));
298
321
  const result = yield this.afterGet(item, opt);
299
322
  if (!result) {
300
323
  return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, '', []);
@@ -302,29 +325,34 @@ class BaseCore {
302
325
  return this.success([result]);
303
326
  }),
304
327
  hasTransaction: false,
305
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.get) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'getting an item',
328
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.get) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'getting an item',
306
329
  });
307
330
  });
308
331
  }
309
332
  getMany(args) {
310
- var _a, _b, _c;
333
+ var _a, _b, _c, _d;
311
334
  return __awaiter(this, void 0, void 0, function* () {
312
335
  return this.runAction({
313
336
  args,
337
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.getMany,
338
+ actionName: constants_1.ACTIONS.GET_MANY,
314
339
  validate: this.getValidators().getMany,
315
340
  accessRights: [utils_1.ACCESS_RIGHTS.GET],
316
341
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
317
- var _d;
342
+ var _e;
318
343
  const args2use = yield this.beforeGetMany(args, opt);
344
+ if (args2use instanceof api_helpers_1.OpResult) {
345
+ return args2use;
346
+ }
319
347
  if (!Array.isArray(args2use === null || args2use === void 0 ? void 0 : args2use.ids)) {
320
348
  return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, '', []);
321
349
  }
322
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.getMany(args2use.ids));
350
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.getMany(args2use.ids));
323
351
  const result = yield this.afterGetMany(items, opt);
324
352
  return this.success(result);
325
353
  }),
326
354
  hasTransaction: false,
327
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.getMany) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'geting multiple items',
355
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.getMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'geting multiple items',
328
356
  });
329
357
  });
330
358
  }
@@ -335,14 +363,16 @@ class BaseCore {
335
363
  return {};
336
364
  }
337
365
  create(args) {
338
- var _a, _b, _c;
366
+ var _a, _b, _c, _d;
339
367
  return __awaiter(this, void 0, void 0, function* () {
340
368
  return this.runAction({
341
369
  args,
370
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.create,
371
+ actionName: constants_1.ACTIONS.CREATE,
342
372
  validate: this.getValidators().create,
343
373
  accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
344
374
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
345
- var _d;
375
+ var _e;
346
376
  const { params } = args || {};
347
377
  if (this.config.hasOrderNo === true) {
348
378
  this.setOrderNo({
@@ -352,132 +382,165 @@ class BaseCore {
352
382
  });
353
383
  }
354
384
  const object = yield this.beforeCreate(params, opt);
355
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(object));
385
+ if (object instanceof api_helpers_1.OpResult) {
386
+ return object;
387
+ }
388
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.create(object));
356
389
  const result = yield this.afterCreate(items, opt);
357
390
  return this.success(result);
358
391
  }),
359
392
  hasTransaction: true,
360
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.create) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'creating an item',
393
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.create) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'creating an item',
361
394
  });
362
395
  });
363
396
  }
364
397
  createMany(args) {
365
- var _a, _b, _c;
398
+ var _a, _b, _c, _d;
366
399
  return __awaiter(this, void 0, void 0, function* () {
367
400
  return this.runAction({
368
401
  args,
402
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.createMany,
403
+ actionName: constants_1.ACTIONS.CREATE_MANY,
369
404
  validate: this.getValidators().createMany,
370
405
  accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
371
406
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
372
- var _d;
407
+ var _e;
373
408
  const { params } = args || {};
374
409
  const objects = yield this.beforeCreateMany(params, opt);
375
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(objects));
410
+ if (objects instanceof api_helpers_1.OpResult) {
411
+ return objects;
412
+ }
413
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.create(objects));
376
414
  const result = yield this.afterCreateMany(items, opt);
377
415
  return this.success(result);
378
416
  }),
379
417
  hasTransaction: true,
380
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.createMany) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'creating multiple items',
418
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.createMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'creating multiple items',
381
419
  });
382
420
  });
383
421
  }
384
422
  update(args) {
385
- var _a, _b, _c;
423
+ var _a, _b, _c, _d;
386
424
  return __awaiter(this, void 0, void 0, function* () {
387
425
  return this.runAction({
388
426
  args,
427
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.update,
428
+ actionName: constants_1.ACTIONS.UPDATE,
389
429
  validate: this.getValidators().update,
390
430
  accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
391
431
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
392
- var _d;
432
+ var _e;
393
433
  const { where, params } = args || {};
394
434
  const object = yield this.beforeUpdate(params, opt);
395
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.update(where, object));
435
+ if (object instanceof api_helpers_1.OpResult) {
436
+ return object;
437
+ }
438
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.update(where, object));
396
439
  const result = yield this.afterUpdate(items, opt);
397
440
  return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
398
441
  }),
399
442
  hasTransaction: true,
400
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.update) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'updating an item',
443
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.update) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating an item',
401
444
  });
402
445
  });
403
446
  }
404
447
  updateMany(args) {
405
- var _a, _b, _c;
448
+ var _a, _b, _c, _d;
406
449
  return __awaiter(this, void 0, void 0, function* () {
407
450
  return this.runAction({
408
451
  args,
452
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.updateMany,
453
+ actionName: constants_1.ACTIONS.UPDATE_MANY,
409
454
  validate: this.getValidators().updateMany,
410
455
  accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
411
456
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
412
- var _d;
457
+ var _e;
413
458
  const { params } = args || {};
414
459
  const object = yield this.beforeUpdateMany(params, opt);
415
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.updateMany(object));
460
+ if (object instanceof api_helpers_1.OpResult) {
461
+ return object;
462
+ }
463
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.updateMany(object));
416
464
  const result = yield this.afterUpdateMany(items, opt);
417
465
  return this.success(result);
418
466
  }),
419
467
  hasTransaction: true,
420
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.updateMany) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'updating multiple items',
468
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.updateMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating multiple items',
421
469
  });
422
470
  });
423
471
  }
424
472
  set(args) {
425
- var _a, _b, _c;
473
+ var _a, _b, _c, _d;
426
474
  return __awaiter(this, void 0, void 0, function* () {
427
475
  return this.runAction({
428
476
  args,
477
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.set,
478
+ actionName: constants_1.ACTIONS.SET,
429
479
  validate: this.getValidators().set,
430
480
  accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
431
481
  action: (args, opt /* { setStep, validatedItems, sameRequest } */) => __awaiter(this, void 0, void 0, function* () {
432
- var _d;
482
+ var _e;
433
483
  const { params } = args || {};
434
484
  const object = yield this.beforeSet(params, opt);
435
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.set(object));
485
+ if (object instanceof api_helpers_1.OpResult) {
486
+ return object;
487
+ }
488
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.set(object));
436
489
  const result = yield this.afterSet(items, opt);
437
490
  return this.success(result);
438
491
  }),
439
492
  hasTransaction: true,
440
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.updateMany) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'updating multiple items',
493
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.updateMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating multiple items',
441
494
  });
442
495
  });
443
496
  }
444
497
  remove(args) {
445
- var _a, _b, _c;
498
+ var _a, _b, _c, _d;
446
499
  return __awaiter(this, void 0, void 0, function* () {
447
500
  return this.runAction({
448
501
  args,
502
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.remove,
503
+ actionName: constants_1.ACTIONS.REMOVE,
449
504
  validate: this.getValidators().remove,
450
505
  accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
451
506
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
452
- var _d;
507
+ var _e;
453
508
  const { where } = args || {};
454
509
  const object = yield this.beforeRemove(where, opt);
455
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
510
+ if (object instanceof api_helpers_1.OpResult) {
511
+ return object;
512
+ }
513
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.remove(object));
456
514
  const result = yield this.afterRemove(items, opt);
457
515
  return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
458
516
  }),
459
517
  hasTransaction: false,
460
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.remove) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'removing an item',
518
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.remove) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'removing an item',
461
519
  });
462
520
  });
463
521
  }
464
522
  removeMany(args) {
465
- var _a, _b, _c;
523
+ var _a, _b, _c, _d;
466
524
  return __awaiter(this, void 0, void 0, function* () {
467
525
  return this.runAction({
468
526
  args,
527
+ doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.removeMany,
528
+ actionName: constants_1.ACTIONS.REMOVE_MANY,
469
529
  validate: this.getValidators().removeMany,
470
530
  accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
471
531
  action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
472
- var _d;
532
+ var _e;
473
533
  const { where } = args || {};
474
534
  const object = yield this.beforeRemoveMany(where, opt);
475
- const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
535
+ if (object instanceof api_helpers_1.OpResult) {
536
+ return object;
537
+ }
538
+ const items = yield ((_e = this.getContext().gateways[this.config.gatewayName]) === null || _e === void 0 ? void 0 : _e.remove(object));
476
539
  const result = yield this.afterRemoveMany(items, opt);
477
540
  return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
478
541
  }),
479
542
  hasTransaction: false,
480
- doingWhat: (_c = (_b = (_a = this.config.doingWhat) === null || _a === void 0 ? void 0 : _a.removeMany) !== null && _b !== void 0 ? _b : this.config.name) !== null && _c !== void 0 ? _c : 'removing multiple items',
543
+ doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.removeMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'removing multiple items',
481
544
  });
482
545
  });
483
546
  }
@@ -672,9 +735,9 @@ class BaseCore {
672
735
  let authByToken = false;
673
736
  const [spaceResult, space] = yield this.findSpace();
674
737
  if (spaceResult !== true) {
675
- return spaceResult;
738
+ return [spaceResult, space];
676
739
  }
677
- const { token, fingerprint } = yield this.extractCredentials({ req, authorization, space });
740
+ const { token, fingerprint } = yield this.extractCredentials({ req, authorization, space, params });
678
741
  const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
679
742
  if (token && jwtSecret) {
680
743
  const verifyResult = (0, userToken_1.verifyUserToken)(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { algorithms: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.algorithms) || [(0, userToken_1.getDefaultJwtAlgorithm)()], audience: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.audience) || space.id, issuer: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.issuer) || (0, userToken_1.getDefaultJwtIssuer)(), fingerprint }));
@@ -686,8 +749,8 @@ class BaseCore {
686
749
  context.roleId = tokenData.roleId;
687
750
  context.userId = tokenData.userId;
688
751
  context.isSuperAdmin = context.roleId === 'superadmin';
689
- context.spaceId = tokenData.spaceId || context.spaceId;
690
- context.lang = tokenData.lang || context.requestedLang;
752
+ context.spaceId = context.spaceId || tokenData.spaceId;
753
+ context.lang = context.requestedLang || tokenData.lang;
691
754
  authByToken = true;
692
755
  }
693
756
  if (!authByToken) {
@@ -19,10 +19,11 @@ const lodash_1 = require("lodash");
19
19
  const utils_1 = require("@sdflc/utils");
20
20
  const DataCache_1 = require("./DataCache");
21
21
  const constants_1 = require("./constants");
22
+ const check_types_1 = __importDefault(require("check-types"));
22
23
  const DEFAULT_CHUNCK_SIZE = 2500;
23
24
  class BaseGateway {
24
25
  constructor(props) {
25
- var _a, _b;
26
+ var _a, _b, _c;
26
27
  this.dateTime = new utils_1.DateTimeHelpers();
27
28
  this.dateTimeNow = new Date();
28
29
  this.config = props;
@@ -38,8 +39,8 @@ class BaseGateway {
38
39
  }
39
40
  this.config.context.usedKeyPrefixes[this.config.keyPrefix] = true;
40
41
  }
41
- this.logger = props.logger instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.WARNING });
42
- this.loader = typeof this.config.loader === 'function' ? this.config.loader : new DataCache_1.DataCache(this.config);
42
+ this.logger = (props === null || props === void 0 ? void 0 : props.logger) instanceof utils_1.Logger ? props.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.WARNING });
43
+ this.loader = check_types_1.default.function((_c = this.config) === null || _c === void 0 ? void 0 : _c.loader) ? this.config.loader : new DataCache_1.DataCache(this.config);
43
44
  }
44
45
  setConfigPart(props) {
45
46
  Object.assign(this.config, props);
@@ -53,24 +54,24 @@ class BaseGateway {
53
54
  }
54
55
  onListFilter(query, filterParams) {
55
56
  return __awaiter(this, void 0, void 0, function* () {
56
- const { hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword, filterByUserField } = this.config || {};
57
+ const { table, hasStatus, hasRemovedAt, hasSpace, activeStatuses, hasLang, fieldForKeyword, filterByUserField } = this.config || {};
57
58
  // This function should be overriden
58
59
  if (hasStatus) {
59
- query.whereIn(constants_1.FIELDS.STATUS, Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]) ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS] : (0, lodash_1.castArray)(activeStatuses));
60
+ query.whereIn(`${table}.${constants_1.FIELDS.STATUS}`, Array.isArray(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS]) ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.STATUS] : (0, lodash_1.castArray)(activeStatuses));
60
61
  }
61
62
  if (hasRemovedAt) {
62
- query.whereNull(constants_1.FIELDS.REMOVED_AT);
63
+ query.whereNull(`${table}.${constants_1.FIELDS.REMOVED_AT}`);
63
64
  }
64
65
  if (hasSpace && ((filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID]) != undefined || this.config.context.spaceId)) {
65
66
  query.where({
66
- [constants_1.FIELDS.SPACE_ID]: (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID]) != undefined ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID] : this.config.context.spaceId,
67
+ [`${table}.${constants_1.FIELDS.SPACE_ID}`]: (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID]) != undefined ? filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.SPACE_ID] : this.config.context.spaceId,
67
68
  });
68
69
  }
69
70
  if (hasLang && (filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG]) != undefined) {
70
- query.whereIn(constants_1.FIELDS.LANG, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG]));
71
+ query.whereIn(`${table}.${constants_1.FIELDS.LANG}`, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[constants_1.FIELDS.LANG]));
71
72
  }
72
73
  if (filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]) {
73
- query.whereIn(constants_1.FIELDS.ID, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]));
74
+ query.whereIn(`${table}.${constants_1.FIELDS.ID}`, (0, lodash_1.castArray)(filterParams === null || filterParams === void 0 ? void 0 : filterParams[this.getIdField()]));
74
75
  }
75
76
  if (fieldForKeyword && filterParams.keyword) {
76
77
  query.whereRaw(`${fieldForKeyword} ILIKE ?`, `%${filterParams.keyword}%`);
@@ -294,8 +295,8 @@ class BaseGateway {
294
295
  if (!this.config.table) {
295
296
  return [];
296
297
  }
297
- const shouldUseCache = useCache != null ? useCache : this.config.noCache;
298
- const fieldsToUse = shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
298
+ const shouldUseCache = useCache != null ? useCache : !this.config.noCache;
299
+ const fieldsToUse = !shouldUseCache && Array.isArray(this.config.selectFields) ? this.config.selectFields : this.getIdField();
299
300
  const query = this.buildListQuery({ filter, params, fieldsToUse });
300
301
  yield this.onListFilter(query, filter);
301
302
  if (limit && !isNaN(limit) && limit > 0) {
@@ -305,7 +306,8 @@ class BaseGateway {
305
306
  query.orderBy(sortBy);
306
307
  }
307
308
  const items = yield query;
308
- return shouldUseCache ? items : yield this.getMany(items.map((row) => row.id));
309
+ const result = shouldUseCache ? yield this.getMany(items.map((row) => row.id)) : items;
310
+ return result;
309
311
  });
310
312
  }
311
313
  create(args) {
@@ -24,6 +24,9 @@ class DataCache {
24
24
  if (!(props === null || props === void 0 ? void 0 : props.idField)) {
25
25
  props.idField = _1.FIELDS.ID;
26
26
  }
27
+ if (!(props === null || props === void 0 ? void 0 : props.idFieldAs)) {
28
+ props.idFieldAs = _1.FIELDS.ID;
29
+ }
27
30
  this.config = props;
28
31
  this.redisClient = redisClient;
29
32
  this.buildKey = typeof buildKey === 'function' ? buildKey : utils_1.buildKey;
@@ -33,7 +36,7 @@ class DataCache {
33
36
  this.init(fn, prefixToUse, context);
34
37
  }
35
38
  batchLoaderDefault(props) {
36
- const { table, idField, readAll, db } = props;
39
+ const { table, idField, idFieldAs, readAll, db } = props;
37
40
  if (!table) {
38
41
  throw Error(`DataCache requires 'table' property to be setup`);
39
42
  }
@@ -48,7 +51,7 @@ class DataCache {
48
51
  const readCfg = Object.assign(Object.assign({}, props), { buildKey: this.buildKey });
49
52
  const idsToUse = (0, lodash_1.castArray)(ids);
50
53
  if (!readAll) {
51
- readCfg.mapBy = idField;
54
+ readCfg.mapBy = idFieldAs || idField;
52
55
  readCfg.whereIn = {
53
56
  field: idField,
54
57
  values: idsToUse.filter((id) => id),
@@ -15,4 +15,16 @@ declare const DEFAULT_ORDER_NO = "10000000";
15
15
  declare const DEFAULT_ORDER_NO_STEP = "1000";
16
16
  declare const DEFAULT_JWT_ALGORITHM = "HS256";
17
17
  declare const DEFAULT_JWT_ISSUER = "microservices-auth";
18
- export { ID_FIELD, FIELDS, ORDER_BYS, HEADERS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, ID_TYPES, DEFAULT_JWT_ALGORITHM, DEFAULT_JWT_ISSUER, };
18
+ declare const ACTIONS: {
19
+ LIST: string;
20
+ GET: string;
21
+ GET_MANY: string;
22
+ CREATE: string;
23
+ CREATE_MANY: string;
24
+ UPDATE: string;
25
+ UPDATE_MANY: string;
26
+ SET: string;
27
+ REMOVE: string;
28
+ REMOVE_MANY: string;
29
+ };
30
+ export { ID_FIELD, FIELDS, ORDER_BYS, HEADERS, DEFAULT_ORDER_NO, DEFAULT_ORDER_NO_STEP, ID_TYPES, DEFAULT_JWT_ALGORITHM, DEFAULT_JWT_ISSUER, ACTIONS, };
@@ -1,6 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DEFAULT_JWT_ISSUER = exports.DEFAULT_JWT_ALGORITHM = exports.ID_TYPES = exports.DEFAULT_ORDER_NO_STEP = exports.DEFAULT_ORDER_NO = exports.HEADERS = exports.ORDER_BYS = exports.FIELDS = exports.ID_FIELD = void 0;
3
+ exports.ACTIONS = exports.DEFAULT_JWT_ISSUER = exports.DEFAULT_JWT_ALGORITHM = exports.ID_TYPES = exports.DEFAULT_ORDER_NO_STEP = exports.DEFAULT_ORDER_NO = exports.HEADERS = exports.ORDER_BYS = exports.FIELDS = exports.ID_FIELD = void 0;
4
4
  const FIELDS = {
5
5
  ID: 'id',
6
6
  STATUS: 'status',
@@ -66,3 +66,16 @@ const DEFAULT_JWT_ALGORITHM = 'HS256';
66
66
  exports.DEFAULT_JWT_ALGORITHM = DEFAULT_JWT_ALGORITHM;
67
67
  const DEFAULT_JWT_ISSUER = 'microservices-auth';
68
68
  exports.DEFAULT_JWT_ISSUER = DEFAULT_JWT_ISSUER;
69
+ const ACTIONS = {
70
+ LIST: 'LIST',
71
+ GET: 'GET',
72
+ GET_MANY: 'GET_MANY',
73
+ CREATE: 'CREATE',
74
+ CREATE_MANY: 'CREATE_MANY',
75
+ UPDATE: 'UPDATE',
76
+ UPDATE_MANY: 'UPDATE_MANY',
77
+ SET: 'SET',
78
+ REMOVE: 'REMOVE',
79
+ REMOVE_MANY: 'REMOVE_MANY',
80
+ };
81
+ exports.ACTIONS = ACTIONS;
@@ -0,0 +1,12 @@
1
+ export interface BaseCoreDoAuthOnActionInterface {
2
+ list?: boolean;
3
+ get?: boolean;
4
+ getMany?: boolean;
5
+ create?: boolean;
6
+ createMany?: boolean;
7
+ update?: boolean;
8
+ updateMany?: boolean;
9
+ set?: boolean;
10
+ remove?: boolean;
11
+ removeMany?: boolean;
12
+ }
@@ -0,0 +1,2 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
@@ -6,6 +6,7 @@ import { BaseCoreDoingWhatInterface } from './BaseCoreDoingWhatInterface';
6
6
  import { VerifyUserTokenArgs } from './VerifyUserTokenArgs';
7
7
  import { ApiContext } from './ApiContext';
8
8
  import { ID_TYPES } from '../constants';
9
+ import { BaseCoreDoAuthOnActionInterface } from './BaseCoreDoAuthOnActionInterface';
9
10
  export interface BaseCorePropsInterface {
10
11
  /**
11
12
  * Application API context with all necessary information about request
@@ -23,6 +24,7 @@ export interface BaseCorePropsInterface {
23
24
  logger: Logger;
24
25
  name?: string;
25
26
  doAuth: boolean;
27
+ doAuthOnAction?: BaseCoreDoAuthOnActionInterface;
26
28
  doingWhat?: BaseCoreDoingWhatInterface;
27
29
  validators?: BaseCoreValidatorsInterface;
28
30
  spaceGw?: BaseGateway;
@@ -5,6 +5,9 @@ import { LoaderInterface } from './LoaderInterface';
5
5
  import { SortItemInterface } from './SortItemInterface';
6
6
  export interface BaseGatewayPropsInterface extends CacheInterface {
7
7
  context: ApiContext;
8
+ table?: string;
9
+ idField?: string;
10
+ idFieldAs?: string;
8
11
  setFields?: string[] | string;
9
12
  mergeFields?: string[];
10
13
  hasStatus?: boolean;
@@ -5,6 +5,7 @@ export interface CacheInterface {
5
5
  context?: any;
6
6
  table?: string;
7
7
  idField?: string;
8
+ idFieldAs?: string;
8
9
  selectFields?: any[];
9
10
  readAll?: boolean;
10
11
  redisClient?: any;
@@ -1,6 +1,7 @@
1
1
  import { BaseCoreActionsInterface } from './BaseCoreActionsInterface';
2
2
  export interface CoreActionInterface {
3
3
  args: any;
4
+ actionName?: string;
4
5
  doAuth?: boolean;
5
6
  accessRights?: any[];
6
7
  validate?(args: any, opt: BaseCoreActionsInterface): Promise<any[]> | any[];
@@ -11,4 +11,8 @@ export interface ExtractCredentialsArgs {
11
11
  * Authorization is a token from the Authorization header
12
12
  */
13
13
  authorization?: string;
14
+ /**
15
+ * An object with parameters passed to the authenticate methods
16
+ */
17
+ params?: any;
14
18
  }
@@ -25,3 +25,4 @@ export * from './CreateUserTokenResult';
25
25
  export * from './ExtractCredentialsArgs';
26
26
  export * from './DownloadFileArgs';
27
27
  export * from './BuildListQueryArgsInterface';
28
+ export * from './BaseCoreDoAuthOnActionInterface';
@@ -40,3 +40,4 @@ __exportStar(require("./CreateUserTokenResult"), exports);
40
40
  __exportStar(require("./ExtractCredentialsArgs"), exports);
41
41
  __exportStar(require("./DownloadFileArgs"), exports);
42
42
  __exportStar(require("./BuildListQueryArgsInterface"), exports);
43
+ __exportStar(require("./BaseCoreDoAuthOnActionInterface"), exports);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sdflc/backend-helpers",
3
- "version": "1.0.69",
3
+ "version": "1.0.71",
4
4
  "description": "Set of helper functions and classes to simplify building backend apps within @sdflc ecosystem",
5
5
  "main": "build/index.js",
6
6
  "types": "build/index.d.ts",