@sdflc/backend-helpers 1.0.48 → 1.0.51

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/build/BaseCore.js CHANGED
@@ -1,15 +1,4 @@
1
1
  "use strict";
2
- var __assign = (this && this.__assign) || function () {
3
- __assign = Object.assign || function(t) {
4
- for (var s, i = 1, n = arguments.length; i < n; i++) {
5
- s = arguments[i];
6
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
- t[p] = s[p];
8
- }
9
- return t;
10
- };
11
- return __assign.apply(this, arguments);
12
- };
13
2
  var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
14
3
  function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
15
4
  return new (P || (P = Promise))(function (resolve, reject) {
@@ -19,42 +8,15 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
19
8
  step((generator = generator.apply(thisArg, _arguments || [])).next());
20
9
  });
21
10
  };
22
- var __generator = (this && this.__generator) || function (thisArg, body) {
23
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
24
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
25
- function verb(n) { return function (v) { return step([n, v]); }; }
26
- function step(op) {
27
- if (f) throw new TypeError("Generator is already executing.");
28
- while (_) try {
29
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
30
- if (y = 0, t) op = [op[0] & 2, t.value];
31
- switch (op[0]) {
32
- case 0: case 1: t = op; break;
33
- case 4: _.label++; return { value: op[1], done: false };
34
- case 5: _.label++; y = op[1]; op = [0]; continue;
35
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
36
- default:
37
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
38
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
39
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
40
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
41
- if (t[2]) _.ops.pop();
42
- _.trys.pop(); continue;
43
- }
44
- op = body.call(thisArg, _);
45
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
46
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
47
- }
48
- };
49
11
  Object.defineProperty(exports, "__esModule", { value: true });
50
12
  exports.BaseCore = void 0;
51
- var api_helpers_1 = require("@sdflc/api-helpers");
52
- var utils_1 = require("@sdflc/utils");
53
- var userToken_1 = require("./userToken");
54
- var defaultValidate = function ( /*params: any, opt?: BaseCoreActionsInterface*/) {
13
+ const api_helpers_1 = require("@sdflc/api-helpers");
14
+ const utils_1 = require("@sdflc/utils");
15
+ const userToken_1 = require("./userToken");
16
+ const defaultValidate = ( /*params: any, opt?: BaseCoreActionsInterface*/) => {
55
17
  return [true, {}];
56
18
  };
57
- var defaultValidators = {
19
+ const defaultValidators = {
58
20
  list: defaultValidate,
59
21
  get: defaultValidate,
60
22
  getMany: defaultValidate,
@@ -66,29 +28,29 @@ var defaultValidators = {
66
28
  remove: defaultValidate,
67
29
  removeMany: defaultValidate,
68
30
  };
69
- var BaseCore = /** @class */ (function () {
70
- function BaseCore(props) {
31
+ class BaseCore {
32
+ constructor(props) {
71
33
  this.config = props;
72
34
  this.logger = props.logger;
73
35
  }
74
- BaseCore.prototype.getDb = function () {
36
+ getDb() {
75
37
  return this.config.db;
76
- };
77
- BaseCore.prototype.getRequestId = function () {
38
+ }
39
+ getRequestId() {
78
40
  return this.config.context.requestId;
79
- };
80
- BaseCore.prototype.getContext = function () {
41
+ }
42
+ getContext() {
81
43
  return this.config.context;
82
- };
83
- BaseCore.prototype.getGateways = function () {
44
+ }
45
+ getGateways() {
84
46
  return this.config.context.gateways || {};
85
- };
86
- BaseCore.prototype.getValidators = function () {
47
+ }
48
+ getValidators() {
87
49
  return defaultValidators;
88
- };
89
- BaseCore.prototype.success = function (data, code) {
90
- var code2use = code != null ? code : api_helpers_1.OP_RESULT_CODES.OK;
91
- var data2use = data;
50
+ }
51
+ success(data, code) {
52
+ const code2use = code != null ? code : api_helpers_1.OP_RESULT_CODES.OK;
53
+ let data2use = data;
92
54
  if (!Array.isArray(data2use)) {
93
55
  if (data) {
94
56
  data2use = [data];
@@ -102,25 +64,25 @@ var BaseCore = /** @class */ (function () {
102
64
  data: data2use,
103
65
  errors: [],
104
66
  }).toJS();
105
- };
106
- BaseCore.prototype.failure = function (code, errorMsg, data) {
107
- var result = new api_helpers_1.OpResult({ data: data });
67
+ }
68
+ failure(code, errorMsg, data) {
69
+ const result = new api_helpers_1.OpResult({ data });
108
70
  result.addError('', errorMsg, code);
109
71
  return result.toJS();
110
- };
111
- BaseCore.prototype.validationFailure = function (ex) {
112
- var result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
113
- Object.keys(ex.errors).forEach(function (key) {
72
+ }
73
+ validationFailure(ex) {
74
+ const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
75
+ Object.keys(ex.errors).forEach((key) => {
114
76
  result.addError(key, ex.errors[key].message);
115
77
  });
116
78
  return result.toJS();
117
- };
118
- BaseCore.prototype.exceptionFailure = function (ex, doingWhat, data) {
119
- var _a = this.getDb().isDbError(ex), isDbError = _a[0], dbErrorMsg = _a[1];
120
- var isOpResult = typeof ex.getErrorSummary === 'function';
121
- var message = isOpResult ? ex.getErrorSummary() : ex.message;
122
- var sysMsg = "An exception has occurred on ".concat(doingWhat, " with message ").concat(message, ":");
123
- var msg = '';
79
+ }
80
+ exceptionFailure(ex, doingWhat, data) {
81
+ const [isDbError, dbErrorMsg] = this.getDb().isDbError(ex);
82
+ const isOpResult = typeof ex.getErrorSummary === 'function';
83
+ const message = isOpResult ? ex.getErrorSummary() : ex.message;
84
+ const sysMsg = `An exception has occurred on ${doingWhat} with message ${message}:`;
85
+ let msg = '';
124
86
  if (isDbError) {
125
87
  msg = dbErrorMsg;
126
88
  }
@@ -129,697 +91,470 @@ var BaseCore = /** @class */ (function () {
129
91
  msg = doingWhat['message'];
130
92
  }
131
93
  else {
132
- msg = isOpResult ? ex.getErrorSummary() : "Sorry, something went wrong on ".concat(doingWhat);
94
+ msg = isOpResult ? ex.getErrorSummary() : `Sorry, something went wrong on ${doingWhat}`;
133
95
  }
134
96
  this.logger.error(sysMsg, ex);
135
97
  }
136
- var result = new api_helpers_1.OpResult({ data: data });
98
+ const result = new api_helpers_1.OpResult({ data });
137
99
  result.addError('', msg, api_helpers_1.OP_RESULT_CODES.EXCEPTION);
138
100
  return result.toJS();
139
- };
140
- BaseCore.prototype.checkResult = function (args) {
141
- return __awaiter(this, void 0, void 0, function () {
142
- var hasTransaction, result, sameRequest;
143
- return __generator(this, function (_a) {
144
- switch (_a.label) {
145
- case 0:
146
- hasTransaction = args.hasTransaction, result = args.result, sameRequest = args.sameRequest;
147
- if (sameRequest) {
148
- return [2 /*return*/];
149
- }
150
- if (!(hasTransaction && result)) return [3 /*break*/, 4];
151
- if (!(result.code >= api_helpers_1.OP_RESULT_CODES.OK)) return [3 /*break*/, 2];
152
- return [4 /*yield*/, this.getDb().commitTransaction()];
153
- case 1:
154
- _a.sent();
155
- return [3 /*break*/, 4];
156
- case 2: return [4 /*yield*/, this.getDb().rollbackTransaction()];
157
- case 3:
158
- _a.sent();
159
- _a.label = 4;
160
- case 4: return [2 /*return*/];
101
+ }
102
+ checkResult(args) {
103
+ return __awaiter(this, void 0, void 0, function* () {
104
+ const { hasTransaction, result, sameRequest } = args;
105
+ if (sameRequest) {
106
+ return;
107
+ }
108
+ if (hasTransaction && result) {
109
+ if (result.code >= api_helpers_1.OP_RESULT_CODES.OK) {
110
+ yield this.getDb().commitTransaction();
161
111
  }
162
- });
112
+ else {
113
+ yield this.getDb().rollbackTransaction();
114
+ }
115
+ }
163
116
  });
164
- };
165
- BaseCore.prototype.runAction = function (opt) {
166
- return __awaiter(this, void 0, void 0, function () {
167
- var _a, args, validate, doAuth, action, hasTransaction, doingWhat, sameRequest, step, setStep, actionOpt, _b, authResult, errMsg, _c, spaceResult, errMsg, validatedItems, valideResult, result_1, result, ex_1, what;
168
- return __generator(this, function (_d) {
169
- switch (_d.label) {
170
- case 0:
171
- _a = opt || {}, args = _a.args, validate = _a.validate, doAuth = _a.doAuth, action = _a.action, hasTransaction = _a.hasTransaction, doingWhat = _a.doingWhat;
172
- sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
173
- step = doingWhat || '';
174
- setStep = function (stepName) {
175
- step = stepName;
176
- };
177
- actionOpt = {
178
- core: this,
179
- setStep: setStep,
180
- sameRequest: sameRequest,
181
- hasTransaction: hasTransaction,
182
- };
183
- _d.label = 1;
184
- case 1:
185
- _d.trys.push([1, 13, , 17]);
186
- if (!!sameRequest) return [3 /*break*/, 7];
187
- if (!hasTransaction) return [3 /*break*/, 3];
188
- return [4 /*yield*/, this.getDb().startTransaction()];
189
- case 2:
190
- _d.sent();
191
- _d.label = 3;
192
- case 3:
193
- if (!(doAuth === true || (doAuth == null && this.config.doAuth))) return [3 /*break*/, 5];
194
- return [4 /*yield*/, this.authenticate(opt, actionOpt)];
195
- case 4:
196
- _b = _d.sent(), authResult = _b[0], errMsg = _b[1];
117
+ }
118
+ runAction(opt) {
119
+ return __awaiter(this, void 0, void 0, function* () {
120
+ const { args, validate, doAuth, action, hasTransaction, doingWhat } = opt || {};
121
+ // If we process action with the same requestId as requestId passed to the constructor it means
122
+ // we are processing the same request from FE and can skip some of the operations like transactions and etc
123
+ const sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
124
+ let step = doingWhat || '';
125
+ const setStep = (stepName) => {
126
+ step = stepName;
127
+ };
128
+ const actionOpt = {
129
+ core: this,
130
+ setStep,
131
+ sameRequest,
132
+ hasTransaction,
133
+ };
134
+ try {
135
+ if (!sameRequest) {
136
+ if (hasTransaction) {
137
+ yield this.getDb().startTransaction();
138
+ }
139
+ if (doAuth === true || (doAuth == null && this.config.doAuth)) {
140
+ const [authResult, errMsg] = yield this.authenticate(opt, actionOpt);
197
141
  if (!authResult) {
198
- return [2 /*return*/, new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg)];
142
+ return new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
199
143
  }
200
- return [3 /*break*/, 7];
201
- case 5: return [4 /*yield*/, this.findSpace()];
202
- case 6:
203
- _c = _d.sent(), spaceResult = _c[0], errMsg = _c[1];
144
+ }
145
+ else {
146
+ const [spaceResult, errMsg] = yield this.findSpace();
204
147
  if (spaceResult !== true) {
205
- return [2 /*return*/, new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg)];
206
- }
207
- _d.label = 7;
208
- case 7:
209
- validatedItems = {};
210
- if (!(typeof validate === 'function')) return [3 /*break*/, 10];
211
- return [4 /*yield*/, validate(args, actionOpt)];
212
- case 8:
213
- valideResult = _d.sent();
214
- result_1 = valideResult;
215
- if (Array.isArray(valideResult)) {
216
- result_1 = valideResult[0];
217
- validatedItems = valideResult[1] || {};
148
+ return new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
218
149
  }
219
- // If validator returned null then consider the validation passed
220
- result_1 = result_1 != null ? result_1 : true;
221
- if (!(result_1 !== true)) return [3 /*break*/, 10];
222
- // if there was an error then `result` is expected to be an object with the same structure as the checkit library does
223
- return [4 /*yield*/, this.checkResult({ hasTransaction: hasTransaction, result: result_1, sameRequest: sameRequest })];
224
- case 9:
150
+ }
151
+ }
152
+ let validatedItems = {};
153
+ if (typeof validate === 'function') {
154
+ // If we have validator provided, use it to validate data before running action
155
+ // validator is expected to return an array:
156
+ // [0] - true or validation error object,
157
+ // [1] - an object with some fetched data to pass ot the action handler
158
+ const valideResult = yield validate(args, actionOpt);
159
+ let result = valideResult;
160
+ if (Array.isArray(valideResult)) {
161
+ result = valideResult[0];
162
+ validatedItems = valideResult[1] || {};
163
+ }
164
+ // If validator returned null then consider the validation passed
165
+ result = result != null ? result : true;
166
+ if (result !== true) {
225
167
  // if there was an error then `result` is expected to be an object with the same structure as the checkit library does
226
- _d.sent();
227
- return [2 /*return*/, result_1.code != null ? result_1 : new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED })];
228
- case 10: return [4 /*yield*/, action(args, __assign(__assign({}, actionOpt), { validatedItems: validatedItems }))];
229
- case 11:
230
- result = _d.sent();
231
- return [4 /*yield*/, this.checkResult({ hasTransaction: hasTransaction, result: result, sameRequest: sameRequest })];
232
- case 12:
233
- _d.sent();
234
- return [2 /*return*/, result];
235
- case 13:
236
- ex_1 = _d.sent();
237
- if (!(!sameRequest && hasTransaction)) return [3 /*break*/, 15];
238
- return [4 /*yield*/, this.getDb().rollbackTransaction()];
239
- case 14:
240
- _d.sent();
241
- _d.label = 15;
242
- case 15: return [4 /*yield*/, this.onException(args, __assign(__assign({}, actionOpt), { doingWhat: doingWhat, exception: ex_1 }))];
243
- case 16:
244
- _d.sent();
245
- what = doingWhat;
246
- if (doingWhat !== step) {
247
- what = {
248
- message: "".concat(step, " failed on ").concat(doingWhat),
249
- };
250
- }
251
- return [2 /*return*/, this.exceptionFailure(ex_1, what)];
252
- case 17: return [2 /*return*/];
168
+ yield this.checkResult({ hasTransaction, result, sameRequest });
169
+ return result.code != null ? result : new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
170
+ }
253
171
  }
254
- });
172
+ const result = yield action(args, Object.assign(Object.assign({}, actionOpt), { validatedItems }));
173
+ yield this.checkResult({ hasTransaction, result, sameRequest });
174
+ return result;
175
+ }
176
+ catch (ex) {
177
+ if (!sameRequest && hasTransaction) {
178
+ yield this.getDb().rollbackTransaction();
179
+ }
180
+ yield this.onException(args, Object.assign(Object.assign({}, actionOpt), { doingWhat, exception: ex }));
181
+ let what = doingWhat;
182
+ if (doingWhat !== step) {
183
+ what = {
184
+ message: `${step} failed on ${doingWhat}`,
185
+ };
186
+ }
187
+ return this.exceptionFailure(ex, what);
188
+ }
255
189
  });
256
- };
257
- BaseCore.prototype.list = function (args) {
190
+ }
191
+ list(args) {
258
192
  var _a, _b, _c;
259
- return __awaiter(this, void 0, void 0, function () {
260
- var _this = this;
261
- return __generator(this, function (_d) {
262
- return [2 /*return*/, this.runAction({
263
- args: args,
264
- validate: this.getValidators().list,
265
- accessRights: [utils_1.ACCESS_RIGHTS.LIST],
266
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
267
- var object, items, result;
268
- var _a;
269
- return __generator(this, function (_b) {
270
- switch (_b.label) {
271
- case 0:
272
- this.logger.log("Requesting a list for ".concat(this.config.name, ":"), args);
273
- return [4 /*yield*/, this.beforeList(args, opt)];
274
- case 1:
275
- object = _b.sent();
276
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.list(object))];
277
- case 2:
278
- items = _b.sent();
279
- return [4 /*yield*/, this.afterList(items, opt)];
280
- case 3:
281
- result = _b.sent();
282
- // ON NO DATA RETURN EMPTY CODE
283
- return [2 /*return*/, this.success(result /*, result.length > 0 OP_RESULT_CODES.OK : OP_RESULT_CODES.*/)];
284
- }
285
- });
286
- }); },
287
- hasTransaction: false,
288
- 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',
289
- })];
193
+ return __awaiter(this, void 0, void 0, function* () {
194
+ return this.runAction({
195
+ args,
196
+ validate: this.getValidators().list,
197
+ accessRights: [utils_1.ACCESS_RIGHTS.LIST],
198
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
199
+ var _d;
200
+ this.logger.log(`Requesting a list for ${this.config.name}:`, args);
201
+ const object = yield this.beforeList(args, opt);
202
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.list(object));
203
+ const result = yield this.afterList(items, opt);
204
+ // ON NO DATA RETURN EMPTY CODE
205
+ return this.success(result /*, result.length > 0 OP_RESULT_CODES.OK : OP_RESULT_CODES.*/);
206
+ }),
207
+ hasTransaction: false,
208
+ 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',
290
209
  });
291
210
  });
292
- };
293
- BaseCore.prototype.get = function (args) {
211
+ }
212
+ get(args) {
294
213
  var _a, _b, _c;
295
- return __awaiter(this, void 0, void 0, function () {
296
- var _this = this;
297
- return __generator(this, function (_d) {
298
- return [2 /*return*/, this.runAction({
299
- args: args,
300
- validate: this.getValidators().get,
301
- accessRights: [utils_1.ACCESS_RIGHTS.GET],
302
- action: function (args) { return __awaiter(_this, void 0, void 0, function () {
303
- var id, item;
304
- var _a;
305
- return __generator(this, function (_b) {
306
- switch (_b.label) {
307
- case 0:
308
- id = (args || {}).id;
309
- this.logger.log("Requesting an item for ".concat(this.config.name, ":"), args);
310
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.get(id))];
311
- case 1:
312
- item = _b.sent();
313
- return [2 /*return*/, this.success([item], item ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
314
- }
315
- });
316
- }); },
317
- hasTransaction: false,
318
- 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',
319
- })];
214
+ return __awaiter(this, void 0, void 0, function* () {
215
+ return this.runAction({
216
+ args,
217
+ validate: this.getValidators().get,
218
+ accessRights: [utils_1.ACCESS_RIGHTS.GET],
219
+ action: (args) => __awaiter(this, void 0, void 0, function* () {
220
+ var _d;
221
+ const { id } = args || {};
222
+ this.logger.log(`Requesting an item for ${this.config.name}:`, args);
223
+ const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.get(id));
224
+ return this.success([item], item ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
225
+ }),
226
+ hasTransaction: false,
227
+ 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',
320
228
  });
321
229
  });
322
- };
323
- BaseCore.prototype.getMany = function (args) {
230
+ }
231
+ getMany(args) {
324
232
  var _a, _b, _c;
325
- return __awaiter(this, void 0, void 0, function () {
326
- var _this = this;
327
- return __generator(this, function (_d) {
328
- return [2 /*return*/, this.runAction({
329
- args: args,
330
- validate: this.getValidators().getMany,
331
- accessRights: [utils_1.ACCESS_RIGHTS.GET],
332
- action: function (args) { return __awaiter(_this, void 0, void 0, function () {
333
- var ids, items;
334
- var _a;
335
- return __generator(this, function (_b) {
336
- switch (_b.label) {
337
- case 0:
338
- ids = (args || {}).ids;
339
- this.logger.log("Requesting multiple items for ".concat(this.config.name, ":"), args);
340
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.getMany(ids))];
341
- case 1:
342
- items = _b.sent();
343
- return [2 /*return*/, this.success(items, items.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
344
- }
345
- });
346
- }); },
347
- hasTransaction: false,
348
- 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',
349
- })];
233
+ return __awaiter(this, void 0, void 0, function* () {
234
+ return this.runAction({
235
+ args,
236
+ validate: this.getValidators().getMany,
237
+ accessRights: [utils_1.ACCESS_RIGHTS.GET],
238
+ action: (args) => __awaiter(this, void 0, void 0, function* () {
239
+ var _d;
240
+ const { ids } = args || {};
241
+ this.logger.log(`Requesting multiple items for ${this.config.name}:`, args);
242
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.getMany(ids));
243
+ return this.success(items, items.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
244
+ }),
245
+ hasTransaction: false,
246
+ 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',
350
247
  });
351
248
  });
352
- };
353
- BaseCore.prototype.create = function (args) {
249
+ }
250
+ create(args) {
354
251
  var _a, _b, _c;
355
- return __awaiter(this, void 0, void 0, function () {
356
- var _this = this;
357
- return __generator(this, function (_d) {
358
- return [2 /*return*/, this.runAction({
359
- args: args,
360
- validate: this.getValidators().create,
361
- accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
362
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
363
- var params, object, item, result;
364
- var _a;
365
- return __generator(this, function (_b) {
366
- switch (_b.label) {
367
- case 0:
368
- params = (args || {}).params;
369
- this.logger.log("Requesting creation of an item for ".concat(this.config.name, ":"), args);
370
- return [4 /*yield*/, this.beforeCreate(params, opt)];
371
- case 1:
372
- object = _b.sent();
373
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.create(object))];
374
- case 2:
375
- item = _b.sent();
376
- return [4 /*yield*/, this.afterCreate(item, opt)];
377
- case 3:
378
- result = _b.sent();
379
- return [2 /*return*/, this.success(result)];
380
- }
381
- });
382
- }); },
383
- hasTransaction: true,
384
- 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',
385
- })];
252
+ return __awaiter(this, void 0, void 0, function* () {
253
+ return this.runAction({
254
+ args,
255
+ validate: this.getValidators().create,
256
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
257
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
258
+ var _d;
259
+ const { params } = args || {};
260
+ this.logger.log(`Requesting creation of an item for ${this.config.name}:`, args);
261
+ const object = yield this.beforeCreate(params, opt);
262
+ const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(object));
263
+ const result = yield this.afterCreate(item, opt);
264
+ return this.success(result);
265
+ }),
266
+ hasTransaction: true,
267
+ 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',
386
268
  });
387
269
  });
388
- };
389
- BaseCore.prototype.createMany = function (args) {
270
+ }
271
+ createMany(args) {
390
272
  var _a, _b, _c;
391
- return __awaiter(this, void 0, void 0, function () {
392
- var _this = this;
393
- return __generator(this, function (_d) {
394
- return [2 /*return*/, this.runAction({
395
- args: args,
396
- validate: this.getValidators().createMany,
397
- accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
398
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
399
- var params, objects, items, result;
400
- var _a;
401
- return __generator(this, function (_b) {
402
- switch (_b.label) {
403
- case 0:
404
- params = (args || {}).params;
405
- this.logger.log("Requesting creation of multiple items for ".concat(this.config.name, ":"), args);
406
- return [4 /*yield*/, this.beforeCreateMany(params, opt)];
407
- case 1:
408
- objects = _b.sent();
409
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.create(objects))];
410
- case 2:
411
- items = _b.sent();
412
- return [4 /*yield*/, this.afterCreateMany(items, opt)];
413
- case 3:
414
- result = _b.sent();
415
- return [2 /*return*/, this.success(result)];
416
- }
417
- });
418
- }); },
419
- hasTransaction: true,
420
- 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',
421
- })];
273
+ return __awaiter(this, void 0, void 0, function* () {
274
+ return this.runAction({
275
+ args,
276
+ validate: this.getValidators().createMany,
277
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
278
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
279
+ var _d;
280
+ const { params } = args || {};
281
+ this.logger.log(`Requesting creation of multiple items for ${this.config.name}:`, args);
282
+ const objects = yield this.beforeCreateMany(params, opt);
283
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(objects));
284
+ const result = yield this.afterCreateMany(items, opt);
285
+ return this.success(result);
286
+ }),
287
+ hasTransaction: true,
288
+ 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',
422
289
  });
423
290
  });
424
- };
425
- BaseCore.prototype.update = function (args) {
291
+ }
292
+ update(args) {
426
293
  var _a, _b, _c;
427
- return __awaiter(this, void 0, void 0, function () {
428
- var _this = this;
429
- return __generator(this, function (_d) {
430
- return [2 /*return*/, this.runAction({
431
- args: args,
432
- validate: this.getValidators().update,
433
- accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
434
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
435
- var _a, where, params, object, item, result;
436
- var _b;
437
- return __generator(this, function (_c) {
438
- switch (_c.label) {
439
- case 0:
440
- _a = args || {}, where = _a.where, params = _a.params;
441
- this.logger.log("Requesting update of an item for ".concat(this.config.name, ":"), args);
442
- return [4 /*yield*/, this.beforeUpdate(params, opt)];
443
- case 1:
444
- object = _c.sent();
445
- return [4 /*yield*/, ((_b = this.getContext().gateways[this.config.gatewayName]) === null || _b === void 0 ? void 0 : _b.update(where, object))];
446
- case 2:
447
- item = _c.sent();
448
- return [4 /*yield*/, this.afterUpdate(item, opt)];
449
- case 3:
450
- result = _c.sent();
451
- return [2 /*return*/, this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
452
- }
453
- });
454
- }); },
455
- hasTransaction: true,
456
- 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',
457
- })];
294
+ return __awaiter(this, void 0, void 0, function* () {
295
+ return this.runAction({
296
+ args,
297
+ validate: this.getValidators().update,
298
+ accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
299
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
300
+ var _d;
301
+ const { where, params } = args || {};
302
+ this.logger.log(`Requesting update of an item for ${this.config.name}:`, args);
303
+ const object = yield this.beforeUpdate(params, opt);
304
+ const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.update(where, object));
305
+ const result = yield this.afterUpdate(item, opt);
306
+ return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
307
+ }),
308
+ hasTransaction: true,
309
+ 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',
458
310
  });
459
311
  });
460
- };
461
- BaseCore.prototype.updateMany = function (args) {
312
+ }
313
+ updateMany(args) {
462
314
  var _a, _b, _c;
463
- return __awaiter(this, void 0, void 0, function () {
464
- var _this = this;
465
- return __generator(this, function (_d) {
466
- return [2 /*return*/, this.runAction({
467
- args: args,
468
- validate: this.getValidators().updateMany,
469
- accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
470
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
471
- var params, object, items, result;
472
- var _a;
473
- return __generator(this, function (_b) {
474
- switch (_b.label) {
475
- case 0:
476
- params = (args || {}).params;
477
- this.logger.log("Requesting update of an item for ".concat(this.config.name, ":"), args);
478
- return [4 /*yield*/, this.beforeUpdateMany(params, opt)];
479
- case 1:
480
- object = _b.sent();
481
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.updateMany(object))];
482
- case 2:
483
- items = _b.sent();
484
- return [4 /*yield*/, this.afterUpdateMany(items, opt)];
485
- case 3:
486
- result = _b.sent();
487
- return [2 /*return*/, this.success(result)];
488
- }
489
- });
490
- }); },
491
- hasTransaction: true,
492
- 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
- })];
315
+ return __awaiter(this, void 0, void 0, function* () {
316
+ return this.runAction({
317
+ args,
318
+ validate: this.getValidators().updateMany,
319
+ accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
320
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
321
+ var _d;
322
+ const { params } = args || {};
323
+ this.logger.log(`Requesting update of an item for ${this.config.name}:`, args);
324
+ const object = yield this.beforeUpdateMany(params, opt);
325
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.updateMany(object));
326
+ const result = yield this.afterUpdateMany(items, opt);
327
+ return this.success(result);
328
+ }),
329
+ hasTransaction: true,
330
+ 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',
494
331
  });
495
332
  });
496
- };
497
- BaseCore.prototype.set = function (args) {
333
+ }
334
+ set(args) {
498
335
  var _a, _b, _c;
499
- return __awaiter(this, void 0, void 0, function () {
500
- var _this = this;
501
- return __generator(this, function (_d) {
502
- return [2 /*return*/, this.runAction({
503
- args: args,
504
- validate: this.getValidators().set,
505
- accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
506
- action: function (args, opt /* { setStep, validatedItems, sameRequest } */) { return __awaiter(_this, void 0, void 0, function () {
507
- var params, object, items, result;
508
- var _a;
509
- return __generator(this, function (_b) {
510
- switch (_b.label) {
511
- case 0:
512
- params = (args || {}).params;
513
- this.logger.log("Requesting setting of item(s) for ".concat(this.config.name, ":"), args);
514
- return [4 /*yield*/, this.beforeSet(params, opt)];
515
- case 1:
516
- object = _b.sent();
517
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.set(object))];
518
- case 2:
519
- items = _b.sent();
520
- return [4 /*yield*/, this.afterSet(items, opt)];
521
- case 3:
522
- result = _b.sent();
523
- return [2 /*return*/, this.success(result)];
524
- }
525
- });
526
- }); },
527
- hasTransaction: true,
528
- 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',
529
- })];
336
+ return __awaiter(this, void 0, void 0, function* () {
337
+ return this.runAction({
338
+ args,
339
+ validate: this.getValidators().set,
340
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
341
+ action: (args, opt /* { setStep, validatedItems, sameRequest } */) => __awaiter(this, void 0, void 0, function* () {
342
+ var _d;
343
+ const { params } = args || {};
344
+ this.logger.log(`Requesting setting of item(s) for ${this.config.name}:`, args);
345
+ const object = yield this.beforeSet(params, opt);
346
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.set(object));
347
+ const result = yield this.afterSet(items, opt);
348
+ return this.success(result);
349
+ }),
350
+ hasTransaction: true,
351
+ 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',
530
352
  });
531
353
  });
532
- };
533
- BaseCore.prototype.remove = function (args) {
354
+ }
355
+ remove(args) {
534
356
  var _a, _b, _c;
535
- return __awaiter(this, void 0, void 0, function () {
536
- var _this = this;
537
- return __generator(this, function (_d) {
538
- return [2 /*return*/, this.runAction({
539
- args: args,
540
- validate: this.getValidators().remove,
541
- accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
542
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
543
- var where, object, items, result;
544
- var _a;
545
- return __generator(this, function (_b) {
546
- switch (_b.label) {
547
- case 0:
548
- where = (args || {}).where;
549
- this.logger.log("Requesting removal of an item for ".concat(this.config.name, ":"), args);
550
- return [4 /*yield*/, this.beforeRemove(where, opt)];
551
- case 1:
552
- object = _b.sent();
553
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.remove(object))];
554
- case 2:
555
- items = _b.sent();
556
- return [4 /*yield*/, this.afterRemove(items, opt)];
557
- case 3:
558
- result = _b.sent();
559
- return [2 /*return*/, this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
560
- }
561
- });
562
- }); },
563
- hasTransaction: false,
564
- 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',
565
- })];
357
+ return __awaiter(this, void 0, void 0, function* () {
358
+ return this.runAction({
359
+ args,
360
+ validate: this.getValidators().remove,
361
+ accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
362
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
363
+ var _d;
364
+ const { where } = args || {};
365
+ this.logger.log(`Requesting removal of an item for ${this.config.name}:`, args);
366
+ const object = yield this.beforeRemove(where, opt);
367
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
368
+ const result = yield this.afterRemove(items, opt);
369
+ return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
370
+ }),
371
+ hasTransaction: false,
372
+ 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',
566
373
  });
567
374
  });
568
- };
569
- BaseCore.prototype.removeMany = function (args) {
375
+ }
376
+ removeMany(args) {
570
377
  var _a, _b, _c;
571
- return __awaiter(this, void 0, void 0, function () {
572
- var _this = this;
573
- return __generator(this, function (_d) {
574
- return [2 /*return*/, this.runAction({
575
- args: args,
576
- validate: this.getValidators().removeMany,
577
- accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
578
- action: function (args, opt) { return __awaiter(_this, void 0, void 0, function () {
579
- var where, object, items, result;
580
- var _a;
581
- return __generator(this, function (_b) {
582
- switch (_b.label) {
583
- case 0:
584
- where = (args || {}).where;
585
- this.logger.log("Requesting removal of multiple items for ".concat(this.config.name, ":"), args);
586
- return [4 /*yield*/, this.beforeRemoveMany(where, opt)];
587
- case 1:
588
- object = _b.sent();
589
- return [4 /*yield*/, ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.remove(object))];
590
- case 2:
591
- items = _b.sent();
592
- return [4 /*yield*/, this.afterRemoveMany(items, opt)];
593
- case 3:
594
- result = _b.sent();
595
- return [2 /*return*/, this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND)];
596
- }
597
- });
598
- }); },
599
- hasTransaction: false,
600
- 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',
601
- })];
378
+ return __awaiter(this, void 0, void 0, function* () {
379
+ return this.runAction({
380
+ args,
381
+ validate: this.getValidators().removeMany,
382
+ accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
383
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
384
+ var _d;
385
+ const { where } = args || {};
386
+ this.logger.log(`Requesting removal of multiple items for ${this.config.name}:`, args);
387
+ const object = yield this.beforeRemoveMany(where, opt);
388
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
389
+ const result = yield this.afterRemoveMany(items, opt);
390
+ return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
391
+ }),
392
+ hasTransaction: false,
393
+ 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',
602
394
  });
603
395
  });
604
- };
396
+ }
605
397
  // eslint-disable-next-line
606
- BaseCore.prototype.defaultAfterAndBefore = function (params, opt) {
607
- return __awaiter(this, void 0, void 0, function () {
608
- return __generator(this, function (_a) {
609
- return [2 /*return*/, params];
610
- });
398
+ defaultAfterAndBefore(params, opt) {
399
+ return __awaiter(this, void 0, void 0, function* () {
400
+ return params;
611
401
  });
612
- };
613
- BaseCore.prototype.beforeList = function (params, opt) {
614
- return __awaiter(this, void 0, void 0, function () {
615
- return __generator(this, function (_a) {
616
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
617
- });
402
+ }
403
+ beforeList(params, opt) {
404
+ return __awaiter(this, void 0, void 0, function* () {
405
+ return this.defaultAfterAndBefore(params, opt);
618
406
  });
619
- };
620
- BaseCore.prototype.afterList = function (params, opt) {
621
- return __awaiter(this, void 0, void 0, function () {
622
- return __generator(this, function (_a) {
623
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
624
- });
407
+ }
408
+ afterList(params, opt) {
409
+ return __awaiter(this, void 0, void 0, function* () {
410
+ return this.defaultAfterAndBefore(params, opt);
625
411
  });
626
- };
627
- BaseCore.prototype.beforeCreate = function (params, opt) {
628
- return __awaiter(this, void 0, void 0, function () {
629
- return __generator(this, function (_a) {
630
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
631
- });
412
+ }
413
+ beforeCreate(params, opt) {
414
+ return __awaiter(this, void 0, void 0, function* () {
415
+ return this.defaultAfterAndBefore(params, opt);
632
416
  });
633
- };
634
- BaseCore.prototype.afterCreate = function (params, opt) {
635
- return __awaiter(this, void 0, void 0, function () {
636
- return __generator(this, function (_a) {
637
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
638
- });
417
+ }
418
+ afterCreate(params, opt) {
419
+ return __awaiter(this, void 0, void 0, function* () {
420
+ return this.defaultAfterAndBefore(params, opt);
639
421
  });
640
- };
641
- BaseCore.prototype.beforeCreateMany = function (params, opt) {
642
- return __awaiter(this, void 0, void 0, function () {
643
- return __generator(this, function (_a) {
644
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
645
- });
422
+ }
423
+ beforeCreateMany(params, opt) {
424
+ return __awaiter(this, void 0, void 0, function* () {
425
+ return this.defaultAfterAndBefore(params, opt);
646
426
  });
647
- };
648
- BaseCore.prototype.afterCreateMany = function (params, opt) {
649
- return __awaiter(this, void 0, void 0, function () {
650
- return __generator(this, function (_a) {
651
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
652
- });
427
+ }
428
+ afterCreateMany(params, opt) {
429
+ return __awaiter(this, void 0, void 0, function* () {
430
+ return this.defaultAfterAndBefore(params, opt);
653
431
  });
654
- };
655
- BaseCore.prototype.beforeUpdate = function (params, opt) {
656
- return __awaiter(this, void 0, void 0, function () {
657
- return __generator(this, function (_a) {
658
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
659
- });
432
+ }
433
+ beforeUpdate(params, opt) {
434
+ return __awaiter(this, void 0, void 0, function* () {
435
+ return this.defaultAfterAndBefore(params, opt);
660
436
  });
661
- };
662
- BaseCore.prototype.afterUpdate = function (params, opt) {
663
- return __awaiter(this, void 0, void 0, function () {
664
- return __generator(this, function (_a) {
665
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
666
- });
437
+ }
438
+ afterUpdate(params, opt) {
439
+ return __awaiter(this, void 0, void 0, function* () {
440
+ return this.defaultAfterAndBefore(params, opt);
667
441
  });
668
- };
669
- BaseCore.prototype.beforeUpdateMany = function (params, opt) {
670
- return __awaiter(this, void 0, void 0, function () {
671
- return __generator(this, function (_a) {
672
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
673
- });
442
+ }
443
+ beforeUpdateMany(params, opt) {
444
+ return __awaiter(this, void 0, void 0, function* () {
445
+ return this.defaultAfterAndBefore(params, opt);
674
446
  });
675
- };
676
- BaseCore.prototype.afterUpdateMany = function (params, opt) {
677
- return __awaiter(this, void 0, void 0, function () {
678
- return __generator(this, function (_a) {
679
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
680
- });
447
+ }
448
+ afterUpdateMany(params, opt) {
449
+ return __awaiter(this, void 0, void 0, function* () {
450
+ return this.defaultAfterAndBefore(params, opt);
681
451
  });
682
- };
683
- BaseCore.prototype.beforeSet = function (params, opt) {
684
- return __awaiter(this, void 0, void 0, function () {
685
- return __generator(this, function (_a) {
686
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
687
- });
452
+ }
453
+ beforeSet(params, opt) {
454
+ return __awaiter(this, void 0, void 0, function* () {
455
+ return this.defaultAfterAndBefore(params, opt);
688
456
  });
689
- };
690
- BaseCore.prototype.afterSet = function (params, opt) {
691
- return __awaiter(this, void 0, void 0, function () {
692
- return __generator(this, function (_a) {
693
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
694
- });
457
+ }
458
+ afterSet(params, opt) {
459
+ return __awaiter(this, void 0, void 0, function* () {
460
+ return this.defaultAfterAndBefore(params, opt);
695
461
  });
696
- };
697
- BaseCore.prototype.beforeRemove = function (params, opt) {
698
- return __awaiter(this, void 0, void 0, function () {
699
- return __generator(this, function (_a) {
700
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
701
- });
462
+ }
463
+ beforeRemove(params, opt) {
464
+ return __awaiter(this, void 0, void 0, function* () {
465
+ return this.defaultAfterAndBefore(params, opt);
702
466
  });
703
- };
704
- BaseCore.prototype.afterRemove = function (params, opt) {
705
- return __awaiter(this, void 0, void 0, function () {
706
- return __generator(this, function (_a) {
707
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
708
- });
467
+ }
468
+ afterRemove(params, opt) {
469
+ return __awaiter(this, void 0, void 0, function* () {
470
+ return this.defaultAfterAndBefore(params, opt);
709
471
  });
710
- };
711
- BaseCore.prototype.beforeRemoveMany = function (params, opt) {
712
- return __awaiter(this, void 0, void 0, function () {
713
- return __generator(this, function (_a) {
714
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
715
- });
472
+ }
473
+ beforeRemoveMany(params, opt) {
474
+ return __awaiter(this, void 0, void 0, function* () {
475
+ return this.defaultAfterAndBefore(params, opt);
716
476
  });
717
- };
718
- BaseCore.prototype.afterRemoveMany = function (params, opt) {
719
- return __awaiter(this, void 0, void 0, function () {
720
- return __generator(this, function (_a) {
721
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
722
- });
477
+ }
478
+ afterRemoveMany(params, opt) {
479
+ return __awaiter(this, void 0, void 0, function* () {
480
+ return this.defaultAfterAndBefore(params, opt);
723
481
  });
724
- };
725
- BaseCore.prototype.onException = function (params, opt) {
726
- return __awaiter(this, void 0, void 0, function () {
727
- return __generator(this, function (_a) {
728
- return [2 /*return*/, this.defaultAfterAndBefore(params, opt)];
729
- });
482
+ }
483
+ onException(params, opt) {
484
+ return __awaiter(this, void 0, void 0, function* () {
485
+ return this.defaultAfterAndBefore(params, opt);
730
486
  });
731
- };
487
+ }
732
488
  /**
733
489
  * Returns JWT secret and its settings to verify received JWT token
734
490
  * @param params Object of parameters passed to the authenticate method
735
491
  * @returns Object with the jwtSecret and verifyTokenArgs
736
492
  */
737
493
  // eslint-disable-next-line
738
- BaseCore.prototype.getJwtTokenSecretAndSettings = function (params) {
739
- return __awaiter(this, void 0, void 0, function () {
740
- var space;
741
- return __generator(this, function (_a) {
742
- space = this.getContext().space;
743
- return [2 /*return*/, {
744
- jwtSecret: (space === null || space === void 0 ? void 0 : space.tokenSecret) || this.config.jwtSecret || '',
745
- verifyTokenArgs: this.config.verifyTokenArgs,
746
- }];
747
- });
494
+ getJwtTokenSecretAndSettings(params) {
495
+ return __awaiter(this, void 0, void 0, function* () {
496
+ const { space } = this.getContext();
497
+ return {
498
+ jwtSecret: (space === null || space === void 0 ? void 0 : space.tokenSecret) || this.config.jwtSecret || '',
499
+ verifyTokenArgs: this.config.verifyTokenArgs,
500
+ };
748
501
  });
749
- };
750
- BaseCore.prototype.findSpace = function () {
751
- return __awaiter(this, void 0, void 0, function () {
752
- var context, space;
753
- return __generator(this, function (_a) {
754
- switch (_a.label) {
755
- case 0:
756
- context = this.config.context;
757
- if (context.space) {
758
- return [2 /*return*/, [true, context.space]];
759
- }
760
- space = null;
761
- if (!context.spaceGw) return [3 /*break*/, 2];
762
- if (!context.spaceId) {
763
- return [2 /*return*/, [false, 'Space not specified']];
764
- }
765
- return [4 /*yield*/, context.spaceGw.get(context.spaceId || '')];
766
- case 1:
767
- // Lookup space in case we have gateway
768
- space = _a.sent();
769
- if (!space) {
770
- return [2 /*return*/, [false, 'Space not found']];
771
- }
772
- context.space = space;
773
- _a.label = 2;
774
- case 2: return [2 /*return*/, [true, space]];
502
+ }
503
+ findSpace() {
504
+ return __awaiter(this, void 0, void 0, function* () {
505
+ const { context } = this.config;
506
+ if (context.space) {
507
+ return [true, context.space];
508
+ }
509
+ let space = null;
510
+ if (context.spaceGw) {
511
+ if (!context.spaceId) {
512
+ return [false, 'Space not specified'];
775
513
  }
776
- });
514
+ // Lookup space in case we have gateway
515
+ space = yield context.spaceGw.get(context.spaceId || '');
516
+ if (!space) {
517
+ return [false, 'Space not found'];
518
+ }
519
+ context.space = space;
520
+ }
521
+ return [true, space];
777
522
  });
778
- };
523
+ }
779
524
  // eslint-disable-next-line
780
- BaseCore.prototype.authenticate = function (params, opt) {
781
- return __awaiter(this, void 0, void 0, function () {
782
- var context, authByToken, _a, spaceResult, space, _b, jwtSecret, verifyTokenArgs, verifyResult, token;
783
- return __generator(this, function (_c) {
784
- switch (_c.label) {
785
- case 0:
786
- context = this.config.context;
787
- authByToken = false;
788
- return [4 /*yield*/, this.findSpace()];
789
- case 1:
790
- _a = _c.sent(), spaceResult = _a[0], space = _a[1];
791
- if (spaceResult !== true) {
792
- return [2 /*return*/, spaceResult];
793
- }
794
- return [4 /*yield*/, this.getJwtTokenSecretAndSettings(params)];
795
- case 2:
796
- _b = _c.sent(), jwtSecret = _b.jwtSecret, verifyTokenArgs = _b.verifyTokenArgs;
797
- if (context.authorization && jwtSecret) {
798
- verifyResult = (0, userToken_1.verifyUserToken)(context.authorization, jwtSecret, verifyTokenArgs);
799
- if (verifyResult.didFail()) {
800
- return [2 /*return*/, [false, verifyResult.getErrorSummary('')]];
801
- }
802
- token = verifyResult.getDataFirst();
803
- context.accountId = token.accountId;
804
- context.roleId = token.roleId;
805
- context.userId = token.userId;
806
- context.spaceId = token.spaceId || context.spaceId;
807
- context.lang = token.lang || context.requestedLang;
808
- authByToken = true;
809
- }
810
- if (!authByToken) {
811
- if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
812
- return [2 /*return*/, [true, '']];
813
- }
814
- else if ((space === null || space === void 0 ? void 0 : space.apiKey) && (space === null || space === void 0 ? void 0 : space.apiKey) !== context.apiKey) {
815
- return [2 /*return*/, [false, 'API Key is invalid']];
816
- }
817
- }
818
- return [2 /*return*/, [true, '']];
525
+ authenticate(params, opt) {
526
+ return __awaiter(this, void 0, void 0, function* () {
527
+ //const { accessRights } = params || {};
528
+ const { context } = this.config;
529
+ let authByToken = false;
530
+ const [spaceResult, space] = yield this.findSpace();
531
+ if (spaceResult !== true) {
532
+ return spaceResult;
533
+ }
534
+ const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
535
+ if (context.authorization && jwtSecret) {
536
+ const verifyResult = (0, userToken_1.verifyUserToken)(context.authorization, jwtSecret, verifyTokenArgs);
537
+ if (verifyResult.didFail()) {
538
+ return [false, verifyResult.getErrorSummary('')];
819
539
  }
820
- });
540
+ const token = verifyResult.getDataFirst();
541
+ context.accountId = token.accountId;
542
+ context.roleId = token.roleId;
543
+ context.userId = token.userId;
544
+ context.spaceId = token.spaceId || context.spaceId;
545
+ context.lang = token.lang || context.requestedLang;
546
+ authByToken = true;
547
+ }
548
+ if (!authByToken) {
549
+ if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
550
+ return [true, ''];
551
+ }
552
+ else if ((space === null || space === void 0 ? void 0 : space.apiKey) && (space === null || space === void 0 ? void 0 : space.apiKey) !== context.apiKey) {
553
+ return [false, 'API Key is invalid'];
554
+ }
555
+ }
556
+ return [true, ''];
821
557
  });
822
- };
823
- return BaseCore;
824
- }());
558
+ }
559
+ }
825
560
  exports.BaseCore = BaseCore;