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