@sdflc/backend-helpers 1.0.49 → 1.0.52

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,30 @@ 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) {
33
+ this.measureTool = new utils_1.MeasureTool();
71
34
  this.config = props;
72
35
  this.logger = props.logger;
73
36
  }
74
- BaseCore.prototype.getDb = function () {
37
+ getDb() {
75
38
  return this.config.db;
76
- };
77
- BaseCore.prototype.getRequestId = function () {
39
+ }
40
+ getRequestId() {
78
41
  return this.config.context.requestId;
79
- };
80
- BaseCore.prototype.getContext = function () {
42
+ }
43
+ getContext() {
81
44
  return this.config.context;
82
- };
83
- BaseCore.prototype.getGateways = function () {
45
+ }
46
+ getGateways() {
84
47
  return this.config.context.gateways || {};
85
- };
86
- BaseCore.prototype.getValidators = function () {
48
+ }
49
+ getValidators() {
87
50
  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;
51
+ }
52
+ success(data, code) {
53
+ const code2use = code != null ? code : api_helpers_1.OP_RESULT_CODES.OK;
54
+ let data2use = data;
92
55
  if (!Array.isArray(data2use)) {
93
56
  if (data) {
94
57
  data2use = [data];
@@ -102,25 +65,25 @@ var BaseCore = /** @class */ (function () {
102
65
  data: data2use,
103
66
  errors: [],
104
67
  }).toJS();
105
- };
106
- BaseCore.prototype.failure = function (code, errorMsg, data) {
107
- var result = new api_helpers_1.OpResult({ data: data });
68
+ }
69
+ failure(code, errorMsg, data) {
70
+ const result = new api_helpers_1.OpResult({ data });
108
71
  result.addError('', errorMsg, code);
109
72
  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) {
73
+ }
74
+ validationFailure(ex) {
75
+ const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
76
+ Object.keys(ex.errors).forEach((key) => {
114
77
  result.addError(key, ex.errors[key].message);
115
78
  });
116
79
  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 = '';
80
+ }
81
+ exceptionFailure(ex, doingWhat, data) {
82
+ const [isDbError, dbErrorMsg] = this.getDb().isDbError(ex);
83
+ const isOpResult = typeof ex.getErrorSummary === 'function';
84
+ const message = isOpResult ? ex.getErrorSummary() : ex.message;
85
+ const sysMsg = `An exception has occurred on ${doingWhat} with message ${message}:`;
86
+ let msg = '';
124
87
  if (isDbError) {
125
88
  msg = dbErrorMsg;
126
89
  }
@@ -129,697 +92,482 @@ var BaseCore = /** @class */ (function () {
129
92
  msg = doingWhat['message'];
130
93
  }
131
94
  else {
132
- msg = isOpResult ? ex.getErrorSummary() : "Sorry, something went wrong on ".concat(doingWhat);
95
+ msg = isOpResult ? ex.getErrorSummary() : `Sorry, something went wrong on ${doingWhat}`;
133
96
  }
134
97
  this.logger.error(sysMsg, ex);
135
98
  }
136
- var result = new api_helpers_1.OpResult({ data: data });
99
+ const result = new api_helpers_1.OpResult({ data });
137
100
  result.addError('', msg, api_helpers_1.OP_RESULT_CODES.EXCEPTION);
138
101
  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*/];
102
+ }
103
+ checkResult(args) {
104
+ return __awaiter(this, void 0, void 0, function* () {
105
+ const { hasTransaction, result, sameRequest } = args;
106
+ if (sameRequest) {
107
+ return;
108
+ }
109
+ if (hasTransaction && result) {
110
+ if (result.code >= api_helpers_1.OP_RESULT_CODES.OK) {
111
+ yield this.getDb().commitTransaction();
161
112
  }
162
- });
113
+ else {
114
+ yield this.getDb().rollbackTransaction();
115
+ }
116
+ }
163
117
  });
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];
118
+ }
119
+ runAction(opt) {
120
+ return __awaiter(this, void 0, void 0, function* () {
121
+ const { args, validate, doAuth, action, hasTransaction, doingWhat } = opt || {};
122
+ // If we process action with the same requestId as requestId passed to the constructor it means
123
+ // we are processing the same request from FE and can skip some of the operations like transactions and etc
124
+ const sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
125
+ // TODO: Calculate total thread block time between async calls withing the request
126
+ // this.measureTool.threadBlockTime(doingWhat, ({ name, durationMs }: any) => {
127
+ // this.logger.debug(`The ${name} blocked thread for the time of ${durationMs} ms`);
128
+ // });
129
+ this.logger.log(`The ${doingWhat} started:`, args);
130
+ this.measureTool.start();
131
+ let step = doingWhat || '';
132
+ const setStep = (stepName) => {
133
+ step = stepName || step;
134
+ const time = this.measureTool.addCheckpoint(stepName);
135
+ if (stepName) {
136
+ this.logger.debug(`${time.name}. Since last step: ${time.durationMs} ms`);
137
+ }
138
+ };
139
+ const actionOpt = {
140
+ core: this,
141
+ setStep,
142
+ sameRequest,
143
+ hasTransaction,
144
+ };
145
+ try {
146
+ if (!sameRequest) {
147
+ if (hasTransaction) {
148
+ yield this.getDb().startTransaction();
149
+ }
150
+ if (doAuth === true || (doAuth == null && this.config.doAuth)) {
151
+ this.measureTool.addCheckpoint('Authenticate request: start');
152
+ const [authResult, errMsg] = yield this.authenticate(opt, actionOpt);
153
+ this.measureTool.addCheckpoint('Authenticate request: end');
197
154
  if (!authResult) {
198
- return [2 /*return*/, new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg)];
155
+ return new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
199
156
  }
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];
157
+ }
158
+ else {
159
+ this.measureTool.addCheckpoint('Find space: start');
160
+ const [spaceResult, errMsg] = yield this.findSpace();
161
+ this.measureTool.addCheckpoint('Find space: end');
204
162
  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] || {};
163
+ return new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
218
164
  }
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:
165
+ }
166
+ }
167
+ let validatedItems = {};
168
+ if (typeof validate === 'function') {
169
+ this.measureTool.addCheckpoint('Validate parameters: start');
170
+ // If we have validator provided, use it to validate data before running action
171
+ // validator is expected to return an array:
172
+ // [0] - true or validation error object,
173
+ // [1] - an object with some fetched data to pass ot the action handler
174
+ const valideResult = yield validate(args, actionOpt);
175
+ let result = valideResult;
176
+ if (Array.isArray(valideResult)) {
177
+ result = valideResult[0];
178
+ validatedItems = valideResult[1] || {};
179
+ }
180
+ // If validator returned null then consider the validation passed
181
+ result = result != null ? result : true;
182
+ this.measureTool.addCheckpoint('Validate parameters: end');
183
+ if (result !== true) {
225
184
  // 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*/];
185
+ yield this.checkResult({ hasTransaction, result, sameRequest });
186
+ return result.code != null ? result : new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
187
+ }
253
188
  }
254
- });
189
+ let time = this.measureTool.stop();
190
+ this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
191
+ const result = yield action(args, Object.assign(Object.assign({}, actionOpt), { validatedItems }));
192
+ yield this.checkResult({ hasTransaction, result, sameRequest });
193
+ time = this.measureTool.stop(doingWhat);
194
+ this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
195
+ return result;
196
+ }
197
+ catch (ex) {
198
+ if (!sameRequest && hasTransaction) {
199
+ yield this.getDb().rollbackTransaction();
200
+ }
201
+ yield this.onException(args, Object.assign(Object.assign({}, actionOpt), { doingWhat, exception: ex }));
202
+ let what = doingWhat;
203
+ if (doingWhat !== step) {
204
+ what = {
205
+ message: `${step} failed on ${doingWhat}`,
206
+ };
207
+ }
208
+ const time = this.measureTool.stop(doingWhat);
209
+ this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
210
+ return this.exceptionFailure(ex, what);
211
+ }
255
212
  });
256
- };
257
- BaseCore.prototype.list = function (args) {
213
+ }
214
+ list(args) {
258
215
  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
- })];
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ return this.runAction({
218
+ args,
219
+ validate: this.getValidators().list,
220
+ accessRights: [utils_1.ACCESS_RIGHTS.LIST],
221
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
222
+ var _d;
223
+ const object = yield this.beforeList(args, opt);
224
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.list(object));
225
+ const result = yield this.afterList(items, opt);
226
+ // ON NO DATA RETURN EMPTY CODE
227
+ return this.success(result /*, result.length > 0 OP_RESULT_CODES.OK : OP_RESULT_CODES.*/);
228
+ }),
229
+ hasTransaction: false,
230
+ 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
231
  });
291
232
  });
292
- };
293
- BaseCore.prototype.get = function (args) {
233
+ }
234
+ get(args) {
294
235
  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
- })];
236
+ return __awaiter(this, void 0, void 0, function* () {
237
+ return this.runAction({
238
+ args,
239
+ validate: this.getValidators().get,
240
+ accessRights: [utils_1.ACCESS_RIGHTS.GET],
241
+ action: (args) => __awaiter(this, void 0, void 0, function* () {
242
+ var _d;
243
+ const { id } = args || {};
244
+ const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.get(id));
245
+ return this.success([item], item ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
246
+ }),
247
+ hasTransaction: false,
248
+ 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
249
  });
321
250
  });
322
- };
323
- BaseCore.prototype.getMany = function (args) {
251
+ }
252
+ getMany(args) {
324
253
  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
- })];
254
+ return __awaiter(this, void 0, void 0, function* () {
255
+ return this.runAction({
256
+ args,
257
+ validate: this.getValidators().getMany,
258
+ accessRights: [utils_1.ACCESS_RIGHTS.GET],
259
+ action: (args) => __awaiter(this, void 0, void 0, function* () {
260
+ var _d;
261
+ const { ids } = args || {};
262
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.getMany(ids));
263
+ return this.success(items, items.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
264
+ }),
265
+ hasTransaction: false,
266
+ 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
267
  });
351
268
  });
352
- };
353
- BaseCore.prototype.create = function (args) {
269
+ }
270
+ create(args) {
354
271
  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
- })];
272
+ return __awaiter(this, void 0, void 0, function* () {
273
+ return this.runAction({
274
+ args,
275
+ validate: this.getValidators().create,
276
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
277
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
278
+ var _d;
279
+ const { params } = args || {};
280
+ const object = yield this.beforeCreate(params, opt);
281
+ const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(object));
282
+ const result = yield this.afterCreate(item, opt);
283
+ return this.success(result);
284
+ }),
285
+ hasTransaction: true,
286
+ 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
287
  });
387
288
  });
388
- };
389
- BaseCore.prototype.createMany = function (args) {
289
+ }
290
+ createMany(args) {
390
291
  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
- })];
292
+ return __awaiter(this, void 0, void 0, function* () {
293
+ return this.runAction({
294
+ args,
295
+ validate: this.getValidators().createMany,
296
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
297
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
298
+ var _d;
299
+ const { params } = args || {};
300
+ const objects = yield this.beforeCreateMany(params, opt);
301
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.create(objects));
302
+ const result = yield this.afterCreateMany(items, opt);
303
+ return this.success(result);
304
+ }),
305
+ hasTransaction: true,
306
+ 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
307
  });
423
308
  });
424
- };
425
- BaseCore.prototype.update = function (args) {
309
+ }
310
+ update(args) {
426
311
  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
- })];
312
+ return __awaiter(this, void 0, void 0, function* () {
313
+ return this.runAction({
314
+ args,
315
+ validate: this.getValidators().update,
316
+ accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
317
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
318
+ var _d;
319
+ const { where, params } = args || {};
320
+ const object = yield this.beforeUpdate(params, opt);
321
+ const item = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.update(where, object));
322
+ const result = yield this.afterUpdate(item, opt);
323
+ return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
324
+ }),
325
+ hasTransaction: true,
326
+ 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
327
  });
459
328
  });
460
- };
461
- BaseCore.prototype.updateMany = function (args) {
329
+ }
330
+ updateMany(args) {
462
331
  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
- })];
332
+ return __awaiter(this, void 0, void 0, function* () {
333
+ return this.runAction({
334
+ args,
335
+ validate: this.getValidators().updateMany,
336
+ accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
337
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
338
+ var _d;
339
+ const { params } = args || {};
340
+ const object = yield this.beforeUpdateMany(params, opt);
341
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.updateMany(object));
342
+ const result = yield this.afterUpdateMany(items, opt);
343
+ return this.success(result);
344
+ }),
345
+ hasTransaction: true,
346
+ 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
347
  });
495
348
  });
496
- };
497
- BaseCore.prototype.set = function (args) {
349
+ }
350
+ set(args) {
498
351
  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
- })];
352
+ return __awaiter(this, void 0, void 0, function* () {
353
+ return this.runAction({
354
+ args,
355
+ validate: this.getValidators().set,
356
+ accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
357
+ action: (args, opt /* { setStep, validatedItems, sameRequest } */) => __awaiter(this, void 0, void 0, function* () {
358
+ var _d;
359
+ const { params } = args || {};
360
+ const object = yield this.beforeSet(params, opt);
361
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.set(object));
362
+ const result = yield this.afterSet(items, opt);
363
+ return this.success(result);
364
+ }),
365
+ hasTransaction: true,
366
+ 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
367
  });
531
368
  });
532
- };
533
- BaseCore.prototype.remove = function (args) {
369
+ }
370
+ remove(args) {
534
371
  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
- })];
372
+ return __awaiter(this, void 0, void 0, function* () {
373
+ return this.runAction({
374
+ args,
375
+ validate: this.getValidators().remove,
376
+ accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
377
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
378
+ var _d;
379
+ const { where } = args || {};
380
+ const object = yield this.beforeRemove(where, opt);
381
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
382
+ const result = yield this.afterRemove(items, opt);
383
+ return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
384
+ }),
385
+ hasTransaction: false,
386
+ 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
387
  });
567
388
  });
568
- };
569
- BaseCore.prototype.removeMany = function (args) {
389
+ }
390
+ removeMany(args) {
570
391
  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
- })];
392
+ return __awaiter(this, void 0, void 0, function* () {
393
+ return this.runAction({
394
+ args,
395
+ validate: this.getValidators().removeMany,
396
+ accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
397
+ action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
398
+ var _d;
399
+ const { where } = args || {};
400
+ const object = yield this.beforeRemoveMany(where, opt);
401
+ const items = yield ((_d = this.getContext().gateways[this.config.gatewayName]) === null || _d === void 0 ? void 0 : _d.remove(object));
402
+ const result = yield this.afterRemoveMany(items, opt);
403
+ return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
404
+ }),
405
+ hasTransaction: false,
406
+ 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
407
  });
603
408
  });
604
- };
409
+ }
605
410
  // 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
- });
411
+ defaultAfterAndBefore(params, opt) {
412
+ return __awaiter(this, void 0, void 0, function* () {
413
+ return params;
611
414
  });
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
- });
415
+ }
416
+ beforeList(params, opt) {
417
+ return __awaiter(this, void 0, void 0, function* () {
418
+ return this.defaultAfterAndBefore(params, opt);
618
419
  });
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
- });
420
+ }
421
+ afterList(params, opt) {
422
+ return __awaiter(this, void 0, void 0, function* () {
423
+ return this.defaultAfterAndBefore(params, opt);
625
424
  });
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
- });
425
+ }
426
+ beforeCreate(params, opt) {
427
+ return __awaiter(this, void 0, void 0, function* () {
428
+ return this.defaultAfterAndBefore(params, opt);
632
429
  });
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
- });
430
+ }
431
+ afterCreate(params, opt) {
432
+ return __awaiter(this, void 0, void 0, function* () {
433
+ return this.defaultAfterAndBefore(params, opt);
639
434
  });
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
- });
435
+ }
436
+ beforeCreateMany(params, opt) {
437
+ return __awaiter(this, void 0, void 0, function* () {
438
+ return this.defaultAfterAndBefore(params, opt);
646
439
  });
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
- });
440
+ }
441
+ afterCreateMany(params, opt) {
442
+ return __awaiter(this, void 0, void 0, function* () {
443
+ return this.defaultAfterAndBefore(params, opt);
653
444
  });
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
- });
445
+ }
446
+ beforeUpdate(params, opt) {
447
+ return __awaiter(this, void 0, void 0, function* () {
448
+ return this.defaultAfterAndBefore(params, opt);
660
449
  });
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
- });
450
+ }
451
+ afterUpdate(params, opt) {
452
+ return __awaiter(this, void 0, void 0, function* () {
453
+ return this.defaultAfterAndBefore(params, opt);
667
454
  });
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
- });
455
+ }
456
+ beforeUpdateMany(params, opt) {
457
+ return __awaiter(this, void 0, void 0, function* () {
458
+ return this.defaultAfterAndBefore(params, opt);
674
459
  });
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
- });
460
+ }
461
+ afterUpdateMany(params, opt) {
462
+ return __awaiter(this, void 0, void 0, function* () {
463
+ return this.defaultAfterAndBefore(params, opt);
681
464
  });
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
- });
465
+ }
466
+ beforeSet(params, opt) {
467
+ return __awaiter(this, void 0, void 0, function* () {
468
+ return this.defaultAfterAndBefore(params, opt);
688
469
  });
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
- });
470
+ }
471
+ afterSet(params, opt) {
472
+ return __awaiter(this, void 0, void 0, function* () {
473
+ return this.defaultAfterAndBefore(params, opt);
695
474
  });
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
- });
475
+ }
476
+ beforeRemove(params, opt) {
477
+ return __awaiter(this, void 0, void 0, function* () {
478
+ return this.defaultAfterAndBefore(params, opt);
702
479
  });
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
- });
480
+ }
481
+ afterRemove(params, opt) {
482
+ return __awaiter(this, void 0, void 0, function* () {
483
+ return this.defaultAfterAndBefore(params, opt);
709
484
  });
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
- });
485
+ }
486
+ beforeRemoveMany(params, opt) {
487
+ return __awaiter(this, void 0, void 0, function* () {
488
+ return this.defaultAfterAndBefore(params, opt);
716
489
  });
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
- });
490
+ }
491
+ afterRemoveMany(params, opt) {
492
+ return __awaiter(this, void 0, void 0, function* () {
493
+ return this.defaultAfterAndBefore(params, opt);
723
494
  });
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
- });
495
+ }
496
+ onException(params, opt) {
497
+ return __awaiter(this, void 0, void 0, function* () {
498
+ return this.defaultAfterAndBefore(params, opt);
730
499
  });
731
- };
500
+ }
732
501
  /**
733
502
  * Returns JWT secret and its settings to verify received JWT token
734
503
  * @param params Object of parameters passed to the authenticate method
735
504
  * @returns Object with the jwtSecret and verifyTokenArgs
736
505
  */
737
506
  // 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
- });
507
+ getJwtTokenSecretAndSettings(params) {
508
+ return __awaiter(this, void 0, void 0, function* () {
509
+ const { space } = this.getContext();
510
+ return {
511
+ jwtSecret: (space === null || space === void 0 ? void 0 : space.tokenSecret) || this.config.jwtSecret || '',
512
+ verifyTokenArgs: this.config.verifyTokenArgs,
513
+ };
748
514
  });
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]];
515
+ }
516
+ findSpace() {
517
+ return __awaiter(this, void 0, void 0, function* () {
518
+ const { context } = this.config;
519
+ if (context.space) {
520
+ return [true, context.space];
521
+ }
522
+ let space = null;
523
+ if (context.spaceGw) {
524
+ if (!context.spaceId) {
525
+ return [false, 'Space not specified'];
775
526
  }
776
- });
527
+ // Lookup space in case we have gateway
528
+ space = yield context.spaceGw.get(context.spaceId || '');
529
+ if (!space) {
530
+ return [false, 'Space not found'];
531
+ }
532
+ context.space = space;
533
+ }
534
+ return [true, space];
777
535
  });
778
- };
536
+ }
779
537
  // 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, '']];
538
+ authenticate(params, opt) {
539
+ return __awaiter(this, void 0, void 0, function* () {
540
+ //const { accessRights } = params || {};
541
+ const { context } = this.config;
542
+ let authByToken = false;
543
+ const [spaceResult, space] = yield this.findSpace();
544
+ if (spaceResult !== true) {
545
+ return spaceResult;
546
+ }
547
+ const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
548
+ if (context.authorization && jwtSecret) {
549
+ const verifyResult = (0, userToken_1.verifyUserToken)(context.authorization, jwtSecret, verifyTokenArgs);
550
+ if (verifyResult.didFail()) {
551
+ return [false, verifyResult.getErrorSummary('')];
819
552
  }
820
- });
553
+ const token = verifyResult.getDataFirst();
554
+ context.accountId = token.accountId;
555
+ context.roleId = token.roleId;
556
+ context.userId = token.userId;
557
+ context.spaceId = token.spaceId || context.spaceId;
558
+ context.lang = token.lang || context.requestedLang;
559
+ authByToken = true;
560
+ }
561
+ if (!authByToken) {
562
+ if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
563
+ return [true, ''];
564
+ }
565
+ else if ((space === null || space === void 0 ? void 0 : space.apiKey) && (space === null || space === void 0 ? void 0 : space.apiKey) !== context.apiKey) {
566
+ return [false, 'API Key is invalid'];
567
+ }
568
+ }
569
+ return [true, ''];
821
570
  });
822
- };
823
- return BaseCore;
824
- }());
571
+ }
572
+ }
825
573
  exports.BaseCore = BaseCore;