@sdflc/backend-helpers 1.0.79 → 1.0.81
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 +496 -600
- package/build/BaseGateway.js +331 -377
- package/build/DataCache.js +16 -19
- package/build/Database.js +55 -74
- package/build/DbMigratorAndSeeder.js +88 -114
- package/build/RedisDataLoader.js +153 -184
- package/build/apiContext.js +35 -33
- package/build/baseController.js +9 -16
- package/build/commonValidators.js +22 -31
- package/build/configBulder.js +5 -1
- package/build/dbHelpers.js +19 -29
- package/build/dbSeeder.js +11 -20
- package/build/dbTableHelpers.js +10 -10
- package/build/downloadHelpers.js +15 -25
- package/build/esm/BaseCore.js +496 -600
- package/build/esm/BaseGateway.js +331 -377
- package/build/esm/DataCache.js +16 -19
- package/build/esm/Database.js +55 -74
- package/build/esm/DbMigratorAndSeeder.js +88 -114
- package/build/esm/RedisDataLoader.js +153 -184
- package/build/esm/apiContext.js +35 -33
- package/build/esm/baseController.js +9 -16
- package/build/esm/commonValidators.js +22 -31
- package/build/esm/configBulder.js +5 -1
- package/build/esm/dbHelpers.js +19 -29
- package/build/esm/dbSeeder.js +11 -20
- package/build/esm/dbTableHelpers.js +10 -10
- package/build/esm/downloadHelpers.js +15 -25
- package/build/esm/knexConfigBuilder.js +17 -7
- package/build/esm/passwordHelpers.js +7 -16
- package/build/esm/resolversHelpers.js +11 -4
- package/build/esm/userToken.js +2 -2
- package/build/knexConfigBuilder.js +17 -7
- package/build/passwordHelpers.js +7 -16
- package/build/resolversHelpers.js +11 -4
- package/build/userToken.js +2 -2
- package/package.json +4 -5
package/build/esm/BaseCore.js
CHANGED
|
@@ -1,12 +1,3 @@
|
|
|
1
|
-
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
2
|
-
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
3
|
-
return new (P || (P = Promise))(function (resolve, reject) {
|
|
4
|
-
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
5
|
-
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
6
|
-
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
7
|
-
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
8
|
-
});
|
|
9
|
-
};
|
|
10
1
|
import { OpResult, OP_RESULT_CODES } from '@sdflc/api-helpers';
|
|
11
2
|
import { Logger, ACCESS_RIGHTS, MeasureTool, StrOrderHelpers, LoggerLevels } from '@sdflc/utils';
|
|
12
3
|
import check from 'check-types';
|
|
@@ -33,27 +24,27 @@ const defaultValidators = {
|
|
|
33
24
|
removeMany: validateRemoveMany,
|
|
34
25
|
};
|
|
35
26
|
class BaseCore {
|
|
27
|
+
measureTool = new MeasureTool();
|
|
28
|
+
config;
|
|
29
|
+
logger;
|
|
36
30
|
constructor(props) {
|
|
37
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
38
|
-
this.measureTool = new MeasureTool();
|
|
39
31
|
this.config = props || {};
|
|
40
32
|
this.logger = this.config.logger instanceof Logger ? this.config.logger : new Logger({ level: LoggerLevels.ERROR });
|
|
41
33
|
this.config.doAuthOnAction = {
|
|
42
|
-
list:
|
|
43
|
-
get:
|
|
44
|
-
getMany:
|
|
45
|
-
create:
|
|
46
|
-
createMany:
|
|
47
|
-
update:
|
|
48
|
-
updateMany:
|
|
49
|
-
set:
|
|
50
|
-
remove:
|
|
51
|
-
removeMany:
|
|
34
|
+
list: props?.doAuthOnAction?.list != null ? props?.doAuthOnAction?.list : props?.doAuth,
|
|
35
|
+
get: props?.doAuthOnAction?.get != null ? props?.doAuthOnAction?.get : props?.doAuth,
|
|
36
|
+
getMany: props?.doAuthOnAction?.getMany != null ? props?.doAuthOnAction?.getMany : props?.doAuth,
|
|
37
|
+
create: props?.doAuthOnAction?.create != null ? props?.doAuthOnAction?.create : props?.doAuth,
|
|
38
|
+
createMany: props?.doAuthOnAction?.createMany != null ? props?.doAuthOnAction?.createMany : props?.doAuth,
|
|
39
|
+
update: props?.doAuthOnAction?.update != null ? props?.doAuthOnAction?.update : props?.doAuth,
|
|
40
|
+
updateMany: props?.doAuthOnAction?.updateMany != null ? props?.doAuthOnAction?.updateMany : props?.doAuth,
|
|
41
|
+
set: props?.doAuthOnAction?.set != null ? props?.doAuthOnAction?.set : props?.doAuth,
|
|
42
|
+
remove: props?.doAuthOnAction?.remove != null ? props?.doAuthOnAction?.remove : props?.doAuth,
|
|
43
|
+
removeMany: props?.doAuthOnAction?.removeMany != null ? props?.doAuthOnAction?.removeMany : props?.doAuth,
|
|
52
44
|
};
|
|
53
45
|
}
|
|
54
46
|
getIdType() {
|
|
55
|
-
|
|
56
|
-
return (_a = this.config.idType) !== null && _a !== void 0 ? _a : ID_TYPES.UNKNOWN;
|
|
47
|
+
return this.config.idType ?? ID_TYPES.UNKNOWN;
|
|
57
48
|
}
|
|
58
49
|
getDb() {
|
|
59
50
|
return this.config.db;
|
|
@@ -68,8 +59,7 @@ class BaseCore {
|
|
|
68
59
|
return this.config.context.gateways || {};
|
|
69
60
|
}
|
|
70
61
|
getGateway(name) {
|
|
71
|
-
|
|
72
|
-
return (_a = this.getGateways()) === null || _a === void 0 ? void 0 : _a[name || this.config.gatewayName];
|
|
62
|
+
return this.getGateways()?.[name || this.config.gatewayName];
|
|
73
63
|
}
|
|
74
64
|
getValidators() {
|
|
75
65
|
return defaultValidators;
|
|
@@ -101,7 +91,7 @@ class BaseCore {
|
|
|
101
91
|
if (ex instanceof OpResult) {
|
|
102
92
|
return result.toJS();
|
|
103
93
|
}
|
|
104
|
-
Object.keys(
|
|
94
|
+
Object.keys(ex?.errors || {}).forEach((key) => {
|
|
105
95
|
const obj = ex.errors[key];
|
|
106
96
|
if (Array.isArray(obj.errors)) {
|
|
107
97
|
// if we have multiple rules failures the add them all
|
|
@@ -132,222 +122,206 @@ class BaseCore {
|
|
|
132
122
|
result.addError('', msg, OP_RESULT_CODES.EXCEPTION);
|
|
133
123
|
return result.toJS();
|
|
134
124
|
}
|
|
135
|
-
checkResult(args) {
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
125
|
+
async checkResult(args) {
|
|
126
|
+
const { hasTransaction, result, sameRequest, doingWhat } = args;
|
|
127
|
+
if (sameRequest) {
|
|
128
|
+
return;
|
|
129
|
+
}
|
|
130
|
+
if (hasTransaction && result) {
|
|
131
|
+
if (result.code >= OP_RESULT_CODES.OK) {
|
|
132
|
+
this.logger.debug(`Commit DB transaction on ${doingWhat}`);
|
|
133
|
+
await this.getDb().commitTransaction();
|
|
140
134
|
}
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
yield this.getDb().commitTransaction();
|
|
145
|
-
}
|
|
146
|
-
else {
|
|
147
|
-
this.logger.debug(`Rollback DB transaction on ${doingWhat} as action result code is '${result.code}'`);
|
|
148
|
-
yield this.getDb().rollbackTransaction();
|
|
149
|
-
}
|
|
135
|
+
else {
|
|
136
|
+
this.logger.debug(`Rollback DB transaction on ${doingWhat} as action result code is '${result.code}'`);
|
|
137
|
+
await this.getDb().rollbackTransaction();
|
|
150
138
|
}
|
|
151
|
-
}
|
|
139
|
+
}
|
|
152
140
|
}
|
|
153
|
-
runAction(opt) {
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
160
|
-
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
141
|
+
async runAction(opt) {
|
|
142
|
+
const { args, validate, doAuth, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace } = opt || {};
|
|
143
|
+
// If we process action with the same requestId as requestId passed to the constructor it means
|
|
144
|
+
// we are processing the same request from FE and can skip some of the operations like transactions and etc
|
|
145
|
+
const sameRequest = !!this.config.context.requestId && args?.requestId === this.config.context.requestId;
|
|
146
|
+
// TODO: Calculate total thread block time between async calls withing the request
|
|
147
|
+
// this.measureTool.threadBlockTime(doingWhat, ({ name, durationMs }: any) => {
|
|
148
|
+
// this.logger.debug(`The ${name} blocked thread for the time of ${durationMs} ms`);
|
|
149
|
+
// });
|
|
150
|
+
if (shouldLogArgs == null || shouldLogArgs === true) {
|
|
151
|
+
this.logger.debug(`The ${doingWhat} started:`, args);
|
|
152
|
+
}
|
|
153
|
+
else {
|
|
154
|
+
this.logger.debug(`The ${doingWhat} started`);
|
|
155
|
+
}
|
|
156
|
+
this.measureTool.start();
|
|
157
|
+
let step = doingWhat || '';
|
|
158
|
+
const setStep = (stepName) => {
|
|
159
|
+
step = stepName || step;
|
|
160
|
+
const time = this.measureTool.addCheckpoint(stepName);
|
|
161
|
+
if (stepName) {
|
|
162
|
+
this.logger.debug(`${time.name}. Since last step: ${time.durationMs} ms`);
|
|
168
163
|
}
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
164
|
+
};
|
|
165
|
+
const actionOpt = {
|
|
166
|
+
core: this,
|
|
167
|
+
setStep,
|
|
168
|
+
sameRequest,
|
|
169
|
+
hasTransaction,
|
|
170
|
+
};
|
|
171
|
+
try {
|
|
172
|
+
if (!sameRequest) {
|
|
173
|
+
if (hasTransaction) {
|
|
174
|
+
this.logger.debug(`Start DB transaction on ${step}`);
|
|
175
|
+
await this.getDb().startTransaction();
|
|
176
176
|
}
|
|
177
|
-
|
|
178
|
-
|
|
179
|
-
|
|
180
|
-
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
if (hasTransaction) {
|
|
187
|
-
this.logger.debug(`Start DB transaction on ${step}`);
|
|
188
|
-
yield this.getDb().startTransaction();
|
|
189
|
-
}
|
|
190
|
-
if (doAuth === true || (doAuth == null && this.config.doAuth)) {
|
|
191
|
-
this.measureTool.addCheckpoint('Authenticate request: start');
|
|
192
|
-
const [authResult, errMsg] = yield this.authenticate(opt, actionOpt);
|
|
193
|
-
this.measureTool.addCheckpoint('Authenticate request: end');
|
|
194
|
-
if (!authResult) {
|
|
195
|
-
const result = new OpResult({ code: OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
|
|
196
|
-
this.logger.error(`The ${step} requires authentication and it has failed:`, errMsg);
|
|
197
|
-
yield this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
198
|
-
return result;
|
|
199
|
-
}
|
|
200
|
-
}
|
|
201
|
-
else if (requireSpace == null || requireSpace === true) {
|
|
202
|
-
this.measureTool.addCheckpoint('Find space: start');
|
|
203
|
-
const [spaceResult, errMsg] = yield this.findSpace();
|
|
204
|
-
this.measureTool.addCheckpoint('Find space: end');
|
|
205
|
-
if (spaceResult !== true) {
|
|
206
|
-
const result = new OpResult({ code: OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
|
|
207
|
-
this.logger.error(`The ${step} requires a space and it has failed:`, errMsg);
|
|
208
|
-
yield this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
209
|
-
return result;
|
|
210
|
-
}
|
|
177
|
+
if (doAuth === true || (doAuth == null && this.config.doAuth)) {
|
|
178
|
+
this.measureTool.addCheckpoint('Authenticate request: start');
|
|
179
|
+
const [authResult, errMsg] = await this.authenticate(opt, actionOpt);
|
|
180
|
+
this.measureTool.addCheckpoint('Authenticate request: end');
|
|
181
|
+
if (!authResult) {
|
|
182
|
+
const result = new OpResult({ code: OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
|
|
183
|
+
this.logger.error(`The ${step} requires authentication and it has failed:`, errMsg);
|
|
184
|
+
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
185
|
+
return result;
|
|
211
186
|
}
|
|
212
187
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
this.
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
if (Array.isArray(valideResult)) {
|
|
223
|
-
result = valideResult[0];
|
|
224
|
-
validatedItems = valideResult[1] || {};
|
|
225
|
-
}
|
|
226
|
-
// If validator returned null then consider the validation passed
|
|
227
|
-
result = result != null ? result : true;
|
|
228
|
-
this.measureTool.addCheckpoint('Validate parameters: end');
|
|
229
|
-
if (result !== true) {
|
|
230
|
-
// if there was an error then `result` is expected to be an object with the same structure as the checkit library does
|
|
231
|
-
yield this.checkResult({ hasTransaction, result, sameRequest });
|
|
232
|
-
return result.code != null ? result : new OpResult({ code: OP_RESULT_CODES.VALIDATION_FAILED });
|
|
188
|
+
else if (requireSpace == null || requireSpace === true) {
|
|
189
|
+
this.measureTool.addCheckpoint('Find space: start');
|
|
190
|
+
const [spaceResult, errMsg] = await this.findSpace();
|
|
191
|
+
this.measureTool.addCheckpoint('Find space: end');
|
|
192
|
+
if (spaceResult !== true) {
|
|
193
|
+
const result = new OpResult({ code: OP_RESULT_CODES.UNAUTHORIZED }).addError('', errMsg);
|
|
194
|
+
this.logger.error(`The ${step} requires a space and it has failed:`, errMsg);
|
|
195
|
+
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
196
|
+
return result;
|
|
233
197
|
}
|
|
234
198
|
}
|
|
235
|
-
let time = this.measureTool.stop();
|
|
236
|
-
this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
|
|
237
|
-
const result = yield action(args, Object.assign(Object.assign({}, actionOpt), { validatedItems, args }));
|
|
238
|
-
yield this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
239
|
-
time = this.measureTool.stop(doingWhat);
|
|
240
|
-
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
241
|
-
return result;
|
|
242
199
|
}
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
251
|
-
|
|
252
|
-
|
|
253
|
-
|
|
254
|
-
}
|
|
200
|
+
let validatedItems = {};
|
|
201
|
+
if (typeof validate === 'function') {
|
|
202
|
+
this.measureTool.addCheckpoint('Validate parameters: start');
|
|
203
|
+
// If we have validator provided, use it to validate data before running action
|
|
204
|
+
// validator is expected to return an array:
|
|
205
|
+
// [0] - true or validation error object,
|
|
206
|
+
// [1] - an object with some fetched data to pass ot the action handler
|
|
207
|
+
const valideResult = await validate(args, actionOpt);
|
|
208
|
+
let result = valideResult;
|
|
209
|
+
if (Array.isArray(valideResult)) {
|
|
210
|
+
result = valideResult[0];
|
|
211
|
+
validatedItems = valideResult[1] || {};
|
|
255
212
|
}
|
|
256
|
-
|
|
257
|
-
|
|
213
|
+
// If validator returned null then consider the validation passed
|
|
214
|
+
result = result != null ? result : true;
|
|
215
|
+
this.measureTool.addCheckpoint('Validate parameters: end');
|
|
216
|
+
if (result !== true) {
|
|
217
|
+
// if there was an error then `result` is expected to be an object with the same structure as the checkit library does
|
|
218
|
+
await this.checkResult({ hasTransaction, result, sameRequest });
|
|
219
|
+
return result.code != null ? result : new OpResult({ code: OP_RESULT_CODES.VALIDATION_FAILED });
|
|
258
220
|
}
|
|
259
|
-
|
|
260
|
-
|
|
261
|
-
|
|
262
|
-
|
|
263
|
-
|
|
221
|
+
}
|
|
222
|
+
let time = this.measureTool.stop();
|
|
223
|
+
this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
|
|
224
|
+
const result = await action(args, { ...actionOpt, validatedItems, args });
|
|
225
|
+
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
226
|
+
time = this.measureTool.stop(doingWhat);
|
|
227
|
+
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
228
|
+
return result;
|
|
229
|
+
}
|
|
230
|
+
catch (ex) {
|
|
231
|
+
this.logger.error(`An exception has occurred on ${step}:`, ex);
|
|
232
|
+
if (!sameRequest && hasTransaction) {
|
|
233
|
+
this.logger.debug(`Rolling back DB transaction on ${step} due to an excaption`);
|
|
234
|
+
await this.getDb().rollbackTransaction();
|
|
235
|
+
}
|
|
236
|
+
let errMessage = '';
|
|
237
|
+
try {
|
|
238
|
+
errMessage = await this.onException(args, { ...actionOpt, doingWhat, exception: ex });
|
|
239
|
+
if (check.function(onException)) {
|
|
240
|
+
errMessage = await onException(args, { ...actionOpt, doingWhat, exception: ex });
|
|
264
241
|
}
|
|
265
|
-
const time = this.measureTool.stop(doingWhat);
|
|
266
|
-
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
267
|
-
return this.exceptionFailure(ex, errMessage);
|
|
268
242
|
}
|
|
269
|
-
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
const object = yield this.beforeList(args, opt);
|
|
283
|
-
if (object instanceof OpResult) {
|
|
284
|
-
return object;
|
|
285
|
-
}
|
|
286
|
-
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.list(object));
|
|
287
|
-
const result = yield this.afterList(items, opt);
|
|
288
|
-
// ON NO DATA RETURN EMPTY CODE
|
|
289
|
-
return this.success(result);
|
|
290
|
-
}),
|
|
291
|
-
hasTransaction: false,
|
|
292
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.list) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'listing',
|
|
293
|
-
});
|
|
294
|
-
});
|
|
295
|
-
}
|
|
296
|
-
get(args) {
|
|
297
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
298
|
-
var _a, _b, _c, _d;
|
|
299
|
-
return this.runAction({
|
|
300
|
-
args,
|
|
301
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.get,
|
|
302
|
-
actionName: ACTIONS.GET,
|
|
303
|
-
validate: this.getValidators().get,
|
|
304
|
-
accessRights: [ACCESS_RIGHTS.GET],
|
|
305
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
306
|
-
var _a;
|
|
307
|
-
const args2use = yield this.beforeGet(args, opt);
|
|
308
|
-
if (args2use instanceof OpResult) {
|
|
309
|
-
return args2use;
|
|
310
|
-
}
|
|
311
|
-
if ((args2use === null || args2use === void 0 ? void 0 : args2use.id) == null) {
|
|
312
|
-
return this.failure(OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
313
|
-
}
|
|
314
|
-
const item = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.get(args2use === null || args2use === void 0 ? void 0 : args2use.id));
|
|
315
|
-
const result = yield this.afterGet(item, opt);
|
|
316
|
-
if (!result) {
|
|
317
|
-
return this.failure(OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
318
|
-
}
|
|
319
|
-
return this.success([result]);
|
|
320
|
-
}),
|
|
321
|
-
hasTransaction: false,
|
|
322
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.get) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'getting an item',
|
|
323
|
-
});
|
|
324
|
-
});
|
|
243
|
+
catch (ex2) {
|
|
244
|
+
this.logger.error(`An exception has occured when handling exception:`, ex2);
|
|
245
|
+
}
|
|
246
|
+
if (!errMessage) {
|
|
247
|
+
errMessage = doingWhat;
|
|
248
|
+
}
|
|
249
|
+
if (doingWhat !== step) {
|
|
250
|
+
errMessage = `${step} failed on ${doingWhat}`;
|
|
251
|
+
}
|
|
252
|
+
const time = this.measureTool.stop(doingWhat);
|
|
253
|
+
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
254
|
+
return this.exceptionFailure(ex, errMessage);
|
|
255
|
+
}
|
|
325
256
|
}
|
|
326
|
-
|
|
327
|
-
return
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
|
|
336
|
-
|
|
337
|
-
|
|
338
|
-
|
|
339
|
-
|
|
340
|
-
|
|
341
|
-
|
|
342
|
-
|
|
343
|
-
|
|
344
|
-
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
|
|
350
|
-
|
|
257
|
+
async list(args) {
|
|
258
|
+
return this.runAction({
|
|
259
|
+
args,
|
|
260
|
+
doAuth: this.config.doAuthOnAction?.list,
|
|
261
|
+
actionName: ACTIONS.LIST,
|
|
262
|
+
validate: this.getValidators().list,
|
|
263
|
+
accessRights: [ACCESS_RIGHTS.LIST],
|
|
264
|
+
action: async (args, opt) => {
|
|
265
|
+
const object = await this.beforeList(args, opt);
|
|
266
|
+
if (object instanceof OpResult) {
|
|
267
|
+
return object;
|
|
268
|
+
}
|
|
269
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.list(object);
|
|
270
|
+
const result = await this.afterList(items, opt);
|
|
271
|
+
// ON NO DATA RETURN EMPTY CODE
|
|
272
|
+
return this.success(result);
|
|
273
|
+
},
|
|
274
|
+
hasTransaction: false,
|
|
275
|
+
doingWhat: this.config.doingWhat?.list ?? this.config.name ?? 'listing',
|
|
276
|
+
});
|
|
277
|
+
}
|
|
278
|
+
async get(args) {
|
|
279
|
+
return this.runAction({
|
|
280
|
+
args,
|
|
281
|
+
doAuth: this.config.doAuthOnAction?.get,
|
|
282
|
+
actionName: ACTIONS.GET,
|
|
283
|
+
validate: this.getValidators().get,
|
|
284
|
+
accessRights: [ACCESS_RIGHTS.GET],
|
|
285
|
+
action: async (args, opt) => {
|
|
286
|
+
const args2use = await this.beforeGet(args, opt);
|
|
287
|
+
if (args2use instanceof OpResult) {
|
|
288
|
+
return args2use;
|
|
289
|
+
}
|
|
290
|
+
if (args2use?.id == null) {
|
|
291
|
+
return this.failure(OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
292
|
+
}
|
|
293
|
+
const item = await this.getContext().gateways[this.config.gatewayName]?.get(args2use?.id);
|
|
294
|
+
const result = await this.afterGet(item, opt);
|
|
295
|
+
if (!result) {
|
|
296
|
+
return this.failure(OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
297
|
+
}
|
|
298
|
+
return this.success([result]);
|
|
299
|
+
},
|
|
300
|
+
hasTransaction: false,
|
|
301
|
+
doingWhat: this.config.doingWhat?.get ?? this.config.name ?? 'getting an item',
|
|
302
|
+
});
|
|
303
|
+
}
|
|
304
|
+
async getMany(args) {
|
|
305
|
+
return this.runAction({
|
|
306
|
+
args,
|
|
307
|
+
doAuth: this.config.doAuthOnAction?.getMany,
|
|
308
|
+
actionName: ACTIONS.GET_MANY,
|
|
309
|
+
validate: this.getValidators().getMany,
|
|
310
|
+
accessRights: [ACCESS_RIGHTS.GET],
|
|
311
|
+
action: async (args, opt) => {
|
|
312
|
+
const args2use = await this.beforeGetMany(args, opt);
|
|
313
|
+
if (args2use instanceof OpResult) {
|
|
314
|
+
return args2use;
|
|
315
|
+
}
|
|
316
|
+
if (!Array.isArray(args2use?.ids)) {
|
|
317
|
+
return this.failure(OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
318
|
+
}
|
|
319
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.getMany(args2use.ids);
|
|
320
|
+
const result = await this.afterGetMany(items, opt);
|
|
321
|
+
return this.success(result);
|
|
322
|
+
},
|
|
323
|
+
hasTransaction: false,
|
|
324
|
+
doingWhat: this.config.doingWhat?.getMany ?? this.config.name ?? 'geting multiple items',
|
|
351
325
|
});
|
|
352
326
|
}
|
|
353
327
|
getGatewayToSetOrerNo() {
|
|
@@ -356,192 +330,162 @@ class BaseCore {
|
|
|
356
330
|
getFilterToSetOrderNo(args) {
|
|
357
331
|
return {};
|
|
358
332
|
}
|
|
359
|
-
create(args) {
|
|
360
|
-
return
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
this.
|
|
373
|
-
|
|
374
|
-
|
|
375
|
-
|
|
376
|
-
|
|
377
|
-
|
|
378
|
-
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
args
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
401
|
-
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
409
|
-
|
|
410
|
-
|
|
411
|
-
|
|
412
|
-
|
|
413
|
-
|
|
414
|
-
|
|
415
|
-
|
|
416
|
-
|
|
417
|
-
|
|
418
|
-
|
|
419
|
-
|
|
420
|
-
|
|
421
|
-
|
|
422
|
-
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
430
|
-
|
|
431
|
-
|
|
432
|
-
|
|
433
|
-
|
|
434
|
-
|
|
435
|
-
|
|
436
|
-
|
|
437
|
-
|
|
438
|
-
|
|
439
|
-
|
|
440
|
-
|
|
441
|
-
|
|
442
|
-
|
|
443
|
-
|
|
444
|
-
|
|
445
|
-
|
|
446
|
-
|
|
447
|
-
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
|
|
454
|
-
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
|
|
461
|
-
|
|
462
|
-
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
|
|
466
|
-
|
|
467
|
-
|
|
468
|
-
|
|
469
|
-
|
|
470
|
-
|
|
471
|
-
|
|
472
|
-
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
|
|
476
|
-
|
|
477
|
-
|
|
478
|
-
|
|
479
|
-
|
|
480
|
-
|
|
481
|
-
|
|
482
|
-
|
|
483
|
-
|
|
484
|
-
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
489
|
-
|
|
490
|
-
|
|
491
|
-
|
|
492
|
-
|
|
493
|
-
|
|
494
|
-
|
|
495
|
-
|
|
496
|
-
|
|
497
|
-
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
|
|
501
|
-
|
|
502
|
-
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
510
|
-
|
|
511
|
-
hasTransaction: false,
|
|
512
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.remove) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'removing an item',
|
|
513
|
-
});
|
|
514
|
-
});
|
|
515
|
-
}
|
|
516
|
-
removeMany(args) {
|
|
517
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
518
|
-
var _a, _b, _c, _d;
|
|
519
|
-
return this.runAction({
|
|
520
|
-
args,
|
|
521
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.removeMany,
|
|
522
|
-
actionName: ACTIONS.REMOVE_MANY,
|
|
523
|
-
validate: this.getValidators().removeMany,
|
|
524
|
-
accessRights: [ACCESS_RIGHTS.REMOVE],
|
|
525
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
526
|
-
var _a;
|
|
527
|
-
const { where } = args || {};
|
|
528
|
-
const object = yield this.beforeRemoveMany(where, opt);
|
|
529
|
-
if (object instanceof OpResult) {
|
|
530
|
-
return object;
|
|
531
|
-
}
|
|
532
|
-
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.remove(object));
|
|
533
|
-
const result = yield this.afterRemoveMany(items, opt);
|
|
534
|
-
return this.success(result, result.length > 0 ? OP_RESULT_CODES.OK : OP_RESULT_CODES.NOT_FOUND);
|
|
535
|
-
}),
|
|
536
|
-
hasTransaction: false,
|
|
537
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.removeMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'removing multiple items',
|
|
538
|
-
});
|
|
333
|
+
async create(args) {
|
|
334
|
+
return this.runAction({
|
|
335
|
+
args,
|
|
336
|
+
doAuth: this.config.doAuthOnAction?.create,
|
|
337
|
+
actionName: ACTIONS.CREATE,
|
|
338
|
+
validate: this.getValidators().create,
|
|
339
|
+
accessRights: [ACCESS_RIGHTS.CREATE],
|
|
340
|
+
action: async (args, opt) => {
|
|
341
|
+
const { params } = args || {};
|
|
342
|
+
if (this.config.hasOrderNo === true) {
|
|
343
|
+
this.setOrderNo({
|
|
344
|
+
params,
|
|
345
|
+
filter: this.getFilterToSetOrderNo({ params, opt }),
|
|
346
|
+
gateway: this.getGatewayToSetOrerNo(),
|
|
347
|
+
});
|
|
348
|
+
}
|
|
349
|
+
const object = await this.beforeCreate(params, opt);
|
|
350
|
+
if (object instanceof OpResult) {
|
|
351
|
+
return object;
|
|
352
|
+
}
|
|
353
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.create(object);
|
|
354
|
+
const result = await this.afterCreate(items, opt);
|
|
355
|
+
return this.success(result);
|
|
356
|
+
},
|
|
357
|
+
hasTransaction: true,
|
|
358
|
+
doingWhat: this.config.doingWhat?.create ?? this.config.name ?? 'creating an item',
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
async createMany(args) {
|
|
362
|
+
return this.runAction({
|
|
363
|
+
args,
|
|
364
|
+
doAuth: this.config.doAuthOnAction?.createMany,
|
|
365
|
+
actionName: ACTIONS.CREATE_MANY,
|
|
366
|
+
validate: this.getValidators().createMany,
|
|
367
|
+
accessRights: [ACCESS_RIGHTS.CREATE],
|
|
368
|
+
action: async (args, opt) => {
|
|
369
|
+
const { params } = args || {};
|
|
370
|
+
const objects = await this.beforeCreateMany(params, opt);
|
|
371
|
+
if (objects instanceof OpResult) {
|
|
372
|
+
return objects;
|
|
373
|
+
}
|
|
374
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.create(objects);
|
|
375
|
+
const result = await this.afterCreateMany(items, opt);
|
|
376
|
+
return this.success(result);
|
|
377
|
+
},
|
|
378
|
+
hasTransaction: true,
|
|
379
|
+
doingWhat: this.config.doingWhat?.createMany ?? this.config.name ?? 'creating multiple items',
|
|
380
|
+
});
|
|
381
|
+
}
|
|
382
|
+
async update(args) {
|
|
383
|
+
return this.runAction({
|
|
384
|
+
args,
|
|
385
|
+
doAuth: this.config.doAuthOnAction?.update,
|
|
386
|
+
actionName: ACTIONS.UPDATE,
|
|
387
|
+
validate: this.getValidators().update,
|
|
388
|
+
accessRights: [ACCESS_RIGHTS.UPDATE],
|
|
389
|
+
action: async (args, opt) => {
|
|
390
|
+
const { where, params } = args || {};
|
|
391
|
+
const object = await this.beforeUpdate(params, opt);
|
|
392
|
+
if (object instanceof OpResult) {
|
|
393
|
+
return object;
|
|
394
|
+
}
|
|
395
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.update(where, object);
|
|
396
|
+
const result = await this.afterUpdate(items, opt);
|
|
397
|
+
return this.success(result, result.length > 0 ? OP_RESULT_CODES.OK : OP_RESULT_CODES.NOT_FOUND);
|
|
398
|
+
},
|
|
399
|
+
hasTransaction: true,
|
|
400
|
+
doingWhat: this.config.doingWhat?.update ?? this.config.name ?? 'updating an item',
|
|
401
|
+
});
|
|
402
|
+
}
|
|
403
|
+
async updateMany(args) {
|
|
404
|
+
return this.runAction({
|
|
405
|
+
args,
|
|
406
|
+
doAuth: this.config.doAuthOnAction?.updateMany,
|
|
407
|
+
actionName: ACTIONS.UPDATE_MANY,
|
|
408
|
+
validate: this.getValidators().updateMany,
|
|
409
|
+
accessRights: [ACCESS_RIGHTS.UPDATE],
|
|
410
|
+
action: async (args, opt) => {
|
|
411
|
+
const { params } = args || {};
|
|
412
|
+
const object = await this.beforeUpdateMany(params, opt);
|
|
413
|
+
if (object instanceof OpResult) {
|
|
414
|
+
return object;
|
|
415
|
+
}
|
|
416
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.updateMany(object);
|
|
417
|
+
const result = await this.afterUpdateMany(items, opt);
|
|
418
|
+
return this.success(result);
|
|
419
|
+
},
|
|
420
|
+
hasTransaction: true,
|
|
421
|
+
doingWhat: this.config.doingWhat?.updateMany ?? this.config.name ?? 'updating multiple items',
|
|
422
|
+
});
|
|
423
|
+
}
|
|
424
|
+
async set(args) {
|
|
425
|
+
return this.runAction({
|
|
426
|
+
args,
|
|
427
|
+
doAuth: this.config.doAuthOnAction?.set,
|
|
428
|
+
actionName: ACTIONS.SET,
|
|
429
|
+
validate: this.getValidators().set,
|
|
430
|
+
accessRights: [ACCESS_RIGHTS.CREATE, ACCESS_RIGHTS.UPDATE],
|
|
431
|
+
action: async (args, opt /* { setStep, validatedItems, sameRequest } */) => {
|
|
432
|
+
const { params } = args || {};
|
|
433
|
+
const object = await this.beforeSet(params, opt);
|
|
434
|
+
if (object instanceof OpResult) {
|
|
435
|
+
return object;
|
|
436
|
+
}
|
|
437
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.set(object);
|
|
438
|
+
const result = await this.afterSet(items, opt);
|
|
439
|
+
return this.success(result);
|
|
440
|
+
},
|
|
441
|
+
hasTransaction: true,
|
|
442
|
+
doingWhat: this.config.doingWhat?.updateMany ?? this.config.name ?? 'updating multiple items',
|
|
443
|
+
});
|
|
444
|
+
}
|
|
445
|
+
async remove(args) {
|
|
446
|
+
return this.runAction({
|
|
447
|
+
args,
|
|
448
|
+
doAuth: this.config.doAuthOnAction?.remove,
|
|
449
|
+
actionName: ACTIONS.REMOVE,
|
|
450
|
+
validate: this.getValidators().remove,
|
|
451
|
+
accessRights: [ACCESS_RIGHTS.REMOVE],
|
|
452
|
+
action: async (args, opt) => {
|
|
453
|
+
const { where } = args || {};
|
|
454
|
+
const object = await this.beforeRemove(where, opt);
|
|
455
|
+
if (object instanceof OpResult) {
|
|
456
|
+
return object;
|
|
457
|
+
}
|
|
458
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
|
|
459
|
+
const result = await this.afterRemove(items, opt);
|
|
460
|
+
return this.success(result, result.length > 0 ? OP_RESULT_CODES.OK : OP_RESULT_CODES.NOT_FOUND);
|
|
461
|
+
},
|
|
462
|
+
hasTransaction: false,
|
|
463
|
+
doingWhat: this.config.doingWhat?.remove ?? this.config.name ?? 'removing an item',
|
|
464
|
+
});
|
|
465
|
+
}
|
|
466
|
+
async removeMany(args) {
|
|
467
|
+
return this.runAction({
|
|
468
|
+
args,
|
|
469
|
+
doAuth: this.config.doAuthOnAction?.removeMany,
|
|
470
|
+
actionName: ACTIONS.REMOVE_MANY,
|
|
471
|
+
validate: this.getValidators().removeMany,
|
|
472
|
+
accessRights: [ACCESS_RIGHTS.REMOVE],
|
|
473
|
+
action: async (args, opt) => {
|
|
474
|
+
const { where } = args || {};
|
|
475
|
+
const object = await this.beforeRemoveMany(where, opt);
|
|
476
|
+
if (object instanceof OpResult) {
|
|
477
|
+
return object;
|
|
478
|
+
}
|
|
479
|
+
const items = await this.getContext().gateways[this.config.gatewayName]?.remove(object);
|
|
480
|
+
const result = await this.afterRemoveMany(items, opt);
|
|
481
|
+
return this.success(result, result.length > 0 ? OP_RESULT_CODES.OK : OP_RESULT_CODES.NOT_FOUND);
|
|
482
|
+
},
|
|
483
|
+
hasTransaction: false,
|
|
484
|
+
doingWhat: this.config.doingWhat?.removeMany ?? this.config.name ?? 'removing multiple items',
|
|
539
485
|
});
|
|
540
486
|
}
|
|
541
|
-
defaultAfterAndBefore(params, opt) {
|
|
542
|
-
return
|
|
543
|
-
return params;
|
|
544
|
-
});
|
|
487
|
+
async defaultAfterAndBefore(params, opt) {
|
|
488
|
+
return params;
|
|
545
489
|
}
|
|
546
490
|
/**
|
|
547
491
|
* Used to transform an object before sending it in the response
|
|
@@ -561,205 +505,160 @@ class BaseCore {
|
|
|
561
505
|
processItemOnIn(item, opt) {
|
|
562
506
|
return this.defaultAfterAndBefore(item, opt);
|
|
563
507
|
}
|
|
564
|
-
beforeGet(item, opt) {
|
|
565
|
-
return
|
|
566
|
-
return this.defaultAfterAndBefore(item, opt);
|
|
567
|
-
});
|
|
508
|
+
async beforeGet(item, opt) {
|
|
509
|
+
return this.defaultAfterAndBefore(item, opt);
|
|
568
510
|
}
|
|
569
|
-
beforeGetMany(items, opt) {
|
|
570
|
-
return
|
|
571
|
-
return this.defaultAfterAndBefore(items, opt);
|
|
572
|
-
});
|
|
511
|
+
async beforeGetMany(items, opt) {
|
|
512
|
+
return this.defaultAfterAndBefore(items, opt);
|
|
573
513
|
}
|
|
574
|
-
beforeList(filter, opt) {
|
|
575
|
-
return
|
|
576
|
-
return this.defaultAfterAndBefore(filter, opt);
|
|
577
|
-
});
|
|
514
|
+
async beforeList(filter, opt) {
|
|
515
|
+
return this.defaultAfterAndBefore(filter, opt);
|
|
578
516
|
}
|
|
579
|
-
afterGet(item, opt) {
|
|
580
|
-
return
|
|
581
|
-
return this.processItemOnOut(item, opt);
|
|
582
|
-
});
|
|
517
|
+
async afterGet(item, opt) {
|
|
518
|
+
return this.processItemOnOut(item, opt);
|
|
583
519
|
}
|
|
584
|
-
afterGetMany(items, opt) {
|
|
585
|
-
return
|
|
586
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
587
|
-
});
|
|
520
|
+
async afterGetMany(items, opt) {
|
|
521
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
588
522
|
}
|
|
589
|
-
afterList(items, opt) {
|
|
590
|
-
return
|
|
591
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
592
|
-
});
|
|
523
|
+
async afterList(items, opt) {
|
|
524
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
593
525
|
}
|
|
594
|
-
beforeCreate(params, opt) {
|
|
595
|
-
return
|
|
596
|
-
return this.processItemOnIn(params, opt);
|
|
597
|
-
});
|
|
526
|
+
async beforeCreate(params, opt) {
|
|
527
|
+
return this.processItemOnIn(params, opt);
|
|
598
528
|
}
|
|
599
|
-
afterCreate(items, opt) {
|
|
600
|
-
return
|
|
601
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
602
|
-
});
|
|
529
|
+
async afterCreate(items, opt) {
|
|
530
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
603
531
|
}
|
|
604
|
-
beforeCreateMany(items, opt) {
|
|
605
|
-
return
|
|
606
|
-
return items.map((item) => this.processItemOnIn(item, opt));
|
|
607
|
-
});
|
|
532
|
+
async beforeCreateMany(items, opt) {
|
|
533
|
+
return items.map((item) => this.processItemOnIn(item, opt));
|
|
608
534
|
}
|
|
609
|
-
afterCreateMany(items, opt) {
|
|
610
|
-
return
|
|
611
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
612
|
-
});
|
|
535
|
+
async afterCreateMany(items, opt) {
|
|
536
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
613
537
|
}
|
|
614
|
-
beforeUpdate(params, opt) {
|
|
615
|
-
return
|
|
616
|
-
return this.processItemOnIn(params, opt);
|
|
617
|
-
});
|
|
538
|
+
async beforeUpdate(params, opt) {
|
|
539
|
+
return this.processItemOnIn(params, opt);
|
|
618
540
|
}
|
|
619
|
-
afterUpdate(items, opt) {
|
|
620
|
-
return
|
|
621
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
622
|
-
});
|
|
541
|
+
async afterUpdate(items, opt) {
|
|
542
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
623
543
|
}
|
|
624
|
-
beforeUpdateMany(items, opt) {
|
|
625
|
-
return
|
|
626
|
-
return
|
|
627
|
-
|
|
628
|
-
|
|
629
|
-
|
|
630
|
-
};
|
|
631
|
-
});
|
|
544
|
+
async beforeUpdateMany(items, opt) {
|
|
545
|
+
return items.map((item) => {
|
|
546
|
+
return {
|
|
547
|
+
params: this.processItemOnIn(item.params, opt),
|
|
548
|
+
where: item.where,
|
|
549
|
+
};
|
|
632
550
|
});
|
|
633
551
|
}
|
|
634
|
-
afterUpdateMany(items, opt) {
|
|
635
|
-
return
|
|
636
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
637
|
-
});
|
|
552
|
+
async afterUpdateMany(items, opt) {
|
|
553
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
638
554
|
}
|
|
639
|
-
beforeSet(params, opt) {
|
|
640
|
-
return
|
|
641
|
-
return castArray(params).map((item) => this.processItemOnIn(item, opt));
|
|
642
|
-
});
|
|
555
|
+
async beforeSet(params, opt) {
|
|
556
|
+
return castArray(params).map((item) => this.processItemOnIn(item, opt));
|
|
643
557
|
}
|
|
644
|
-
afterSet(items, opt) {
|
|
645
|
-
return
|
|
646
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
647
|
-
});
|
|
558
|
+
async afterSet(items, opt) {
|
|
559
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
648
560
|
}
|
|
649
|
-
beforeRemove(params, opt) {
|
|
650
|
-
return
|
|
651
|
-
return this.defaultAfterAndBefore(params, opt);
|
|
652
|
-
});
|
|
561
|
+
async beforeRemove(params, opt) {
|
|
562
|
+
return this.defaultAfterAndBefore(params, opt);
|
|
653
563
|
}
|
|
654
|
-
afterRemove(items, opt) {
|
|
655
|
-
return
|
|
656
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
657
|
-
});
|
|
564
|
+
async afterRemove(items, opt) {
|
|
565
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
658
566
|
}
|
|
659
|
-
beforeRemoveMany(params, opt) {
|
|
660
|
-
return
|
|
661
|
-
return this.defaultAfterAndBefore(params, opt);
|
|
662
|
-
});
|
|
567
|
+
async beforeRemoveMany(params, opt) {
|
|
568
|
+
return this.defaultAfterAndBefore(params, opt);
|
|
663
569
|
}
|
|
664
|
-
afterRemoveMany(items, opt) {
|
|
665
|
-
return
|
|
666
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
667
|
-
});
|
|
570
|
+
async afterRemoveMany(items, opt) {
|
|
571
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
668
572
|
}
|
|
669
|
-
onException(params, opt) {
|
|
670
|
-
return
|
|
671
|
-
return '';
|
|
672
|
-
});
|
|
573
|
+
async onException(params, opt) {
|
|
574
|
+
return '';
|
|
673
575
|
}
|
|
674
576
|
/**
|
|
675
577
|
* Returns JWT secret and its settings to verify received JWT token
|
|
676
578
|
* @param params Object of parameters passed to the authenticate method
|
|
677
579
|
* @returns Object with the jwtSecret and verifyTokenArgs
|
|
678
580
|
*/
|
|
679
|
-
getJwtTokenSecretAndSettings(params) {
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
};
|
|
686
|
-
});
|
|
581
|
+
async getJwtTokenSecretAndSettings(params) {
|
|
582
|
+
const { space } = this.getContext();
|
|
583
|
+
return {
|
|
584
|
+
jwtSecret: space?.tokenSecret || this.config.jwtSecret || '',
|
|
585
|
+
verifyTokenArgs: this.config.verifyTokenArgs,
|
|
586
|
+
};
|
|
687
587
|
}
|
|
688
|
-
findSpace() {
|
|
689
|
-
|
|
690
|
-
|
|
691
|
-
|
|
692
|
-
|
|
588
|
+
async findSpace() {
|
|
589
|
+
const { context } = this.config;
|
|
590
|
+
if (context.space) {
|
|
591
|
+
return [true, context.space];
|
|
592
|
+
}
|
|
593
|
+
let space = null;
|
|
594
|
+
if (context.spaceGw) {
|
|
595
|
+
if (!context.spaceId) {
|
|
596
|
+
return [false, 'Space not specified'];
|
|
693
597
|
}
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
}
|
|
699
|
-
// Lookup space in case we have gateway
|
|
700
|
-
space = yield context.spaceGw.get(context.spaceId || '');
|
|
701
|
-
if (!space) {
|
|
702
|
-
return [false, 'Space not found'];
|
|
703
|
-
}
|
|
704
|
-
context.space = space;
|
|
598
|
+
// Lookup space in case we have gateway
|
|
599
|
+
space = await context.spaceGw.get(context.spaceId || '');
|
|
600
|
+
if (!space) {
|
|
601
|
+
return [false, 'Space not found'];
|
|
705
602
|
}
|
|
706
|
-
|
|
707
|
-
}
|
|
603
|
+
context.space = space;
|
|
604
|
+
}
|
|
605
|
+
return [true, space];
|
|
606
|
+
}
|
|
607
|
+
async extractCredentials(args) {
|
|
608
|
+
const { req, space, authorization } = args;
|
|
609
|
+
const token = req.cookies?.[space.cookieName] || authorization;
|
|
610
|
+
const fingerprint = req.cookies?.[space.fingerprintCookieName] || args.fingerprint || '';
|
|
611
|
+
return {
|
|
612
|
+
token,
|
|
613
|
+
fingerprint,
|
|
614
|
+
};
|
|
708
615
|
}
|
|
709
|
-
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
713
|
-
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
616
|
+
async authenticate(params, opt) {
|
|
617
|
+
//const { accessRights } = params || {};
|
|
618
|
+
const context = this.getContext();
|
|
619
|
+
const { req, authorization } = context;
|
|
620
|
+
let authByToken = false;
|
|
621
|
+
const [spaceResult, space] = await this.findSpace();
|
|
622
|
+
if (spaceResult !== true) {
|
|
623
|
+
return [spaceResult, space];
|
|
624
|
+
}
|
|
625
|
+
const { token, fingerprint } = await this.extractCredentials({
|
|
626
|
+
req,
|
|
627
|
+
authorization,
|
|
628
|
+
fingerprint: context.fingerprint,
|
|
629
|
+
space,
|
|
630
|
+
params,
|
|
631
|
+
});
|
|
632
|
+
const { jwtSecret, verifyTokenArgs } = await this.getJwtTokenSecretAndSettings(params);
|
|
633
|
+
if (token && jwtSecret) {
|
|
634
|
+
const verifyResult = verifyUserToken(token, jwtSecret, {
|
|
635
|
+
...verifyTokenArgs,
|
|
636
|
+
algorithms: verifyTokenArgs?.algorithms || [getDefaultJwtAlgorithm()],
|
|
637
|
+
audience: verifyTokenArgs?.audience || space.id,
|
|
638
|
+
issuer: verifyTokenArgs?.issuer || getDefaultJwtIssuer(),
|
|
717
639
|
fingerprint,
|
|
718
|
-
};
|
|
719
|
-
});
|
|
720
|
-
}
|
|
721
|
-
authenticate(params, opt) {
|
|
722
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
723
|
-
//const { accessRights } = params || {};
|
|
724
|
-
const context = this.getContext();
|
|
725
|
-
const { req, authorization } = context;
|
|
726
|
-
let authByToken = false;
|
|
727
|
-
const [spaceResult, space] = yield this.findSpace();
|
|
728
|
-
if (spaceResult !== true) {
|
|
729
|
-
return [spaceResult, space];
|
|
730
|
-
}
|
|
731
|
-
const { token, fingerprint } = yield this.extractCredentials({
|
|
732
|
-
req,
|
|
733
|
-
authorization,
|
|
734
|
-
fingerprint: context.fingerprint,
|
|
735
|
-
space,
|
|
736
|
-
params,
|
|
737
640
|
});
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
const verifyResult = verifyUserToken(token, jwtSecret, Object.assign(Object.assign({}, verifyTokenArgs), { algorithms: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.algorithms) || [getDefaultJwtAlgorithm()], audience: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.audience) || space.id, issuer: (verifyTokenArgs === null || verifyTokenArgs === void 0 ? void 0 : verifyTokenArgs.issuer) || getDefaultJwtIssuer(), fingerprint }));
|
|
741
|
-
if (verifyResult.didFail()) {
|
|
742
|
-
return [false, verifyResult.getErrorSummary('')];
|
|
743
|
-
}
|
|
744
|
-
const tokenData = verifyResult.getDataFirst();
|
|
745
|
-
context.accountId = tokenData.accountId;
|
|
746
|
-
context.roleId = tokenData.roleId;
|
|
747
|
-
context.userId = tokenData.userId;
|
|
748
|
-
context.isSuperAdmin = context.roleId === 'superadmin';
|
|
749
|
-
context.spaceId = context.spaceId || tokenData.spaceId;
|
|
750
|
-
context.lang = context.requestedLang || tokenData.lang;
|
|
751
|
-
authByToken = true;
|
|
641
|
+
if (verifyResult.didFail()) {
|
|
642
|
+
return [false, verifyResult.getErrorSummary('')];
|
|
752
643
|
}
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
644
|
+
const tokenData = verifyResult.getDataFirst();
|
|
645
|
+
context.accountId = tokenData.accountId;
|
|
646
|
+
context.roleId = tokenData.roleId;
|
|
647
|
+
context.userId = tokenData.userId;
|
|
648
|
+
context.isSuperAdmin = context.roleId === 'superadmin';
|
|
649
|
+
context.spaceId = context.spaceId || tokenData.spaceId;
|
|
650
|
+
context.lang = context.requestedLang || tokenData.lang;
|
|
651
|
+
authByToken = true;
|
|
652
|
+
}
|
|
653
|
+
if (!authByToken) {
|
|
654
|
+
if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
|
|
655
|
+
return [true, ''];
|
|
760
656
|
}
|
|
761
|
-
|
|
762
|
-
|
|
657
|
+
else if (space?.apiKey && space?.apiKey !== context.apiKey) {
|
|
658
|
+
return [false, 'API Key is invalid'];
|
|
659
|
+
}
|
|
660
|
+
}
|
|
661
|
+
return [true, ''];
|
|
763
662
|
}
|
|
764
663
|
processRemovedRecord(record) {
|
|
765
664
|
if (record && typeof record.id === 'string') {
|
|
@@ -778,33 +677,30 @@ class BaseCore {
|
|
|
778
677
|
});
|
|
779
678
|
return obj;
|
|
780
679
|
}
|
|
781
|
-
setOrderNo(args) {
|
|
782
|
-
|
|
783
|
-
|
|
784
|
-
|
|
785
|
-
|
|
786
|
-
|
|
787
|
-
}
|
|
788
|
-
|
|
789
|
-
|
|
790
|
-
}
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
|
|
794
|
-
|
|
795
|
-
|
|
796
|
-
|
|
797
|
-
|
|
680
|
+
async setOrderNo(args) {
|
|
681
|
+
const { params, filter, gateway } = args || {};
|
|
682
|
+
if (typeof params !== 'object') {
|
|
683
|
+
throw new Error(`The 'setOrderNo' expected the parameter to have 'params' object but got: ${JSON.stringify(params)} `);
|
|
684
|
+
}
|
|
685
|
+
if (typeof gateway !== 'object') {
|
|
686
|
+
throw new Error(`The 'setOrderNo' expected the parameter to have 'gateway' object but got: ${JSON.stringify(gateway)} `);
|
|
687
|
+
}
|
|
688
|
+
if (typeof gateway.list !== 'function') {
|
|
689
|
+
throw new Error(`The 'setOrderNo' expected the 'gateway' object to have 'list' method but got: ${JSON.stringify(gateway.list)} `);
|
|
690
|
+
}
|
|
691
|
+
if (!params.orderNo) {
|
|
692
|
+
const lastRecord = await gateway.list({
|
|
693
|
+
params: filter,
|
|
694
|
+
pagination: { pageSize: 1, sortBy: [{ name: FIELDS.ORDER_NO, order: ORDER_BYS.DESC }] },
|
|
695
|
+
});
|
|
696
|
+
if (lastRecord.length > 0) {
|
|
697
|
+
const order = new StrOrderHelpers({
|
|
698
|
+
start: lastRecord[0]?.orderNo || DEFAULT_ORDER_NO,
|
|
699
|
+
step: DEFAULT_ORDER_NO_STEP,
|
|
798
700
|
});
|
|
799
|
-
|
|
800
|
-
const order = new StrOrderHelpers({
|
|
801
|
-
start: ((_a = lastRecord[0]) === null || _a === void 0 ? void 0 : _a.orderNo) || DEFAULT_ORDER_NO,
|
|
802
|
-
step: DEFAULT_ORDER_NO_STEP,
|
|
803
|
-
});
|
|
804
|
-
params.orderNo = order.increase();
|
|
805
|
-
}
|
|
701
|
+
params.orderNo = order.increase();
|
|
806
702
|
}
|
|
807
|
-
}
|
|
703
|
+
}
|
|
808
704
|
}
|
|
809
705
|
}
|
|
810
706
|
export { BaseCore };
|