@sdflc/backend-helpers 1.0.76 → 1.0.78
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/README.md +178 -178
- package/build/BaseCore.d.ts +88 -88
- package/build/BaseCore.js +819 -819
- package/build/BaseGateway.d.ts +49 -49
- package/build/BaseGateway.js +484 -483
- package/build/DataCache.d.ts +14 -14
- package/build/DataCache.js +104 -104
- package/build/Database.d.ts +32 -32
- package/build/Database.js +198 -198
- package/build/DbMigratorAndSeeder.d.ts +49 -49
- package/build/DbMigratorAndSeeder.js +238 -228
- package/build/RedisDataLoader.d.ts +41 -41
- package/build/RedisDataLoader.js +248 -248
- package/build/apiContext.d.ts +11 -11
- package/build/apiContext.js +143 -143
- package/build/{BaseController.d.ts → baseController.d.ts} +17 -17
- package/build/{BaseController.js → baseController.js} +66 -66
- package/build/checkItHelpers.d.ts +3 -3
- package/build/checkItHelpers.js +28 -28
- package/build/commonValidators.d.ts +17 -17
- package/build/commonValidators.js +298 -298
- package/build/configBulder.d.ts +2 -2
- package/build/configBulder.js +32 -32
- package/build/constants.d.ts +30 -30
- package/build/constants.js +80 -81
- package/build/dbHelpers.d.ts +43 -43
- package/build/dbHelpers.js +166 -166
- package/build/dbSeeder.d.ts +2 -2
- package/build/dbSeeder.js +92 -82
- package/build/dbTableHelpers.d.ts +11 -11
- package/build/dbTableHelpers.js +110 -110
- package/build/downloadHelpers.d.ts +2 -2
- package/build/downloadHelpers.js +83 -83
- package/build/index.d.ts +21 -21
- package/build/index.js +37 -37
- package/build/interfaces/ApiContext.d.ts +24 -24
- package/build/interfaces/ApiContext.js +2 -2
- package/build/interfaces/ApiContextInterface.d.ts +12 -12
- package/build/interfaces/ApiContextInterface.js +2 -2
- package/build/interfaces/BaseCoreActionsInterface.d.ts +38 -38
- package/build/interfaces/BaseCoreActionsInterface.js +2 -2
- package/build/interfaces/BaseCoreDoAuthOnActionInterface.d.ts +12 -12
- package/build/interfaces/BaseCoreDoAuthOnActionInterface.js +2 -2
- package/build/interfaces/BaseCoreDoingWhatInterface.d.ts +13 -13
- package/build/interfaces/BaseCoreDoingWhatInterface.js +2 -2
- package/build/interfaces/BaseCorePropsInterface.d.ts +36 -36
- package/build/interfaces/BaseCorePropsInterface.js +2 -2
- package/build/interfaces/BaseCoreValidatorsInterface.d.ts +13 -13
- package/build/interfaces/BaseCoreValidatorsInterface.js +2 -2
- package/build/interfaces/BaseGatewayPropsInterface.d.ts +39 -39
- package/build/interfaces/BaseGatewayPropsInterface.js +2 -2
- package/build/interfaces/BuildConfigOptions.d.ts +6 -6
- package/build/interfaces/BuildConfigOptions.js +2 -2
- package/build/interfaces/BuildDefaultResolversOptions.d.ts +17 -17
- package/build/interfaces/BuildDefaultResolversOptions.js +2 -2
- package/build/interfaces/BuildListQueryArgsInterface.d.ts +6 -6
- package/build/interfaces/BuildListQueryArgsInterface.js +2 -2
- package/build/interfaces/CacheInterface.d.ts +15 -15
- package/build/interfaces/CacheInterface.js +2 -2
- package/build/interfaces/CommonValidatorArgs.d.ts +3 -3
- package/build/interfaces/CommonValidatorArgs.js +2 -2
- package/build/interfaces/CoreActionInterface.d.ts +14 -14
- package/build/interfaces/CoreActionInterface.js +2 -2
- package/build/interfaces/CreateUserTokenArgs.d.ts +11 -11
- package/build/interfaces/CreateUserTokenArgs.js +2 -2
- package/build/interfaces/CreateUserTokenResult.d.ts +4 -4
- package/build/interfaces/CreateUserTokenResult.js +2 -2
- package/build/interfaces/DatabaseOptions.d.ts +22 -22
- package/build/interfaces/DatabaseOptions.js +2 -2
- package/build/interfaces/DatabaseReadInterface.d.ts +14 -14
- package/build/interfaces/DatabaseReadInterface.js +2 -2
- package/build/interfaces/DbFieldAddDefaultInterface.d.ts +9 -9
- package/build/interfaces/DbFieldAddDefaultInterface.js +2 -2
- package/build/interfaces/DownloadFileArgs.d.ts +6 -6
- package/build/interfaces/DownloadFileArgs.js +2 -2
- package/build/interfaces/ExtractCredentialsArgs.d.ts +19 -19
- package/build/interfaces/ExtractCredentialsArgs.js +2 -2
- package/build/interfaces/JwtTokenSecretAndSettings.d.ts +5 -5
- package/build/interfaces/JwtTokenSecretAndSettings.js +2 -2
- package/build/interfaces/ListArgsInterface.d.ts +5 -5
- package/build/interfaces/ListArgsInterface.js +2 -2
- package/build/interfaces/LoaderInterface.d.ts +4 -4
- package/build/interfaces/LoaderInterface.js +2 -2
- package/build/interfaces/PaginationInterface.d.ts +7 -7
- package/build/interfaces/PaginationInterface.js +2 -2
- package/build/interfaces/SortItemInterface.d.ts +4 -4
- package/build/interfaces/SortItemInterface.js +2 -2
- package/build/interfaces/UserTokenParams.d.ts +8 -8
- package/build/interfaces/UserTokenParams.js +2 -2
- package/build/interfaces/VerifyUserTokenArgs.d.ts +13 -13
- package/build/interfaces/VerifyUserTokenArgs.js +2 -2
- package/build/interfaces/index.d.ts +28 -28
- package/build/interfaces/index.js +43 -43
- package/build/knexConfigBuilder.d.ts +1 -1
- package/build/knexConfigBuilder.js +80 -80
- package/build/passwordHelpers.d.ts +5 -5
- package/build/passwordHelpers.js +30 -30
- package/build/redisClient.d.ts +17 -17
- package/build/redisClient.js +97 -97
- package/build/resolversHelpers.d.ts +3 -3
- package/build/resolversHelpers.js +62 -62
- package/build/userToken.d.ts +8 -8
- package/build/userToken.js +86 -86
- package/build/utils.d.ts +2 -2
- package/build/utils.js +21 -21
- package/package.json +71 -71
package/build/BaseCore.js
CHANGED
|
@@ -1,819 +1,819 @@
|
|
|
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
|
-
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
-
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
-
};
|
|
14
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
-
exports.BaseCore = void 0;
|
|
16
|
-
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
17
|
-
const utils_1 = require("@sdflc/utils");
|
|
18
|
-
const check_types_1 = __importDefault(require("check-types"));
|
|
19
|
-
const lodash_1 = require("lodash");
|
|
20
|
-
const commonValidators_1 = require("./commonValidators");
|
|
21
|
-
const constants_1 = require("./constants");
|
|
22
|
-
const userToken_1 = require("./userToken");
|
|
23
|
-
const defaultValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
|
|
24
|
-
return [true, {}];
|
|
25
|
-
};
|
|
26
|
-
const defaultMutationValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
|
|
27
|
-
return [true, {}];
|
|
28
|
-
};
|
|
29
|
-
const defaultValidators = {
|
|
30
|
-
list: defaultValidate,
|
|
31
|
-
get: commonValidators_1.validateGet,
|
|
32
|
-
getMany: commonValidators_1.validateGetMany,
|
|
33
|
-
create: defaultValidate,
|
|
34
|
-
createMany: defaultValidate,
|
|
35
|
-
update: defaultValidate,
|
|
36
|
-
updateMany: defaultValidate,
|
|
37
|
-
set: defaultValidate,
|
|
38
|
-
remove: commonValidators_1.validateRemove,
|
|
39
|
-
removeMany: commonValidators_1.validateRemoveMany,
|
|
40
|
-
};
|
|
41
|
-
class BaseCore {
|
|
42
|
-
constructor(props) {
|
|
43
|
-
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
44
|
-
this.measureTool = new utils_1.MeasureTool();
|
|
45
|
-
this.config = props || {};
|
|
46
|
-
this.logger = this.config.logger instanceof utils_1.Logger ? this.config.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
47
|
-
this.config.doAuthOnAction = {
|
|
48
|
-
list: ((_a = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.list) != null ? (_b = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _b === void 0 ? void 0 : _b.list : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
49
|
-
get: ((_c = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _c === void 0 ? void 0 : _c.get) != null ? (_d = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _d === void 0 ? void 0 : _d.get : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
50
|
-
getMany: ((_e = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _e === void 0 ? void 0 : _e.getMany) != null ? (_f = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _f === void 0 ? void 0 : _f.getMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
51
|
-
create: ((_g = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _g === void 0 ? void 0 : _g.create) != null ? (_h = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _h === void 0 ? void 0 : _h.create : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
52
|
-
createMany: ((_j = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _j === void 0 ? void 0 : _j.createMany) != null ? (_k = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _k === void 0 ? void 0 : _k.createMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
53
|
-
update: ((_l = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _l === void 0 ? void 0 : _l.update) != null ? (_m = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _m === void 0 ? void 0 : _m.update : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
54
|
-
updateMany: ((_o = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _o === void 0 ? void 0 : _o.updateMany) != null ? (_p = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _p === void 0 ? void 0 : _p.updateMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
55
|
-
set: ((_q = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _q === void 0 ? void 0 : _q.set) != null ? (_r = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _r === void 0 ? void 0 : _r.set : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
56
|
-
remove: ((_s = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _s === void 0 ? void 0 : _s.remove) != null ? (_t = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _t === void 0 ? void 0 : _t.remove : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
57
|
-
removeMany: ((_u = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _u === void 0 ? void 0 : _u.removeMany) != null ? (_v = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _v === void 0 ? void 0 : _v.removeMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
58
|
-
};
|
|
59
|
-
}
|
|
60
|
-
getIdType() {
|
|
61
|
-
var _a;
|
|
62
|
-
return (_a = this.config.idType) !== null && _a !== void 0 ? _a : constants_1.ID_TYPES.UNKNOWN;
|
|
63
|
-
}
|
|
64
|
-
getDb() {
|
|
65
|
-
return this.config.db;
|
|
66
|
-
}
|
|
67
|
-
getRequestId() {
|
|
68
|
-
return this.config.context.requestId;
|
|
69
|
-
}
|
|
70
|
-
getContext() {
|
|
71
|
-
return this.config.context;
|
|
72
|
-
}
|
|
73
|
-
getGateways() {
|
|
74
|
-
return this.config.context.gateways || {};
|
|
75
|
-
}
|
|
76
|
-
getGateway(name) {
|
|
77
|
-
var _a;
|
|
78
|
-
return (_a = this.getGateways()) === null || _a === void 0 ? void 0 : _a[name || this.config.gatewayName];
|
|
79
|
-
}
|
|
80
|
-
getValidators() {
|
|
81
|
-
return defaultValidators;
|
|
82
|
-
}
|
|
83
|
-
success(data, code) {
|
|
84
|
-
const code2use = code != null ? code : api_helpers_1.OP_RESULT_CODES.OK;
|
|
85
|
-
let data2use = data;
|
|
86
|
-
if (!Array.isArray(data2use)) {
|
|
87
|
-
if (data) {
|
|
88
|
-
data2use = [data];
|
|
89
|
-
}
|
|
90
|
-
else {
|
|
91
|
-
data2use = [];
|
|
92
|
-
}
|
|
93
|
-
}
|
|
94
|
-
return new api_helpers_1.OpResult({
|
|
95
|
-
code: code2use,
|
|
96
|
-
data: data2use,
|
|
97
|
-
errors: [],
|
|
98
|
-
}).toJS();
|
|
99
|
-
}
|
|
100
|
-
failure(code, errorMsg, data) {
|
|
101
|
-
const result = new api_helpers_1.OpResult({ data });
|
|
102
|
-
result.addError('', errorMsg, code);
|
|
103
|
-
return result.toJS();
|
|
104
|
-
}
|
|
105
|
-
validationFailure(ex) {
|
|
106
|
-
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
|
|
107
|
-
if (ex instanceof api_helpers_1.OpResult) {
|
|
108
|
-
return result.toJS();
|
|
109
|
-
}
|
|
110
|
-
Object.keys((ex === null || ex === void 0 ? void 0 : ex.errors) || {}).forEach((key) => {
|
|
111
|
-
const obj = ex.errors[key];
|
|
112
|
-
if (Array.isArray(obj.errors)) {
|
|
113
|
-
// if we have multiple rules failures the add them all
|
|
114
|
-
obj.errors.forEach((item) => {
|
|
115
|
-
result.addError(key, item.message);
|
|
116
|
-
});
|
|
117
|
-
}
|
|
118
|
-
else {
|
|
119
|
-
result.addError(key, obj.message);
|
|
120
|
-
}
|
|
121
|
-
});
|
|
122
|
-
return result.toJS();
|
|
123
|
-
}
|
|
124
|
-
exceptionFailure(ex, doingWhat, data) {
|
|
125
|
-
const [isDbError, dbErrorMsg] = this.getDb().isDbError(ex);
|
|
126
|
-
const isOpResult = typeof ex.getErrorSummary === 'function';
|
|
127
|
-
const message = isOpResult ? ex.getErrorSummary() : ex.message;
|
|
128
|
-
const sysMsg = `An exception has occurred on ${doingWhat} with message ${message}:`;
|
|
129
|
-
let msg = '';
|
|
130
|
-
if (isDbError) {
|
|
131
|
-
msg = dbErrorMsg;
|
|
132
|
-
}
|
|
133
|
-
else {
|
|
134
|
-
msg = isOpResult ? ex.getErrorSummary() : `Sorry, something went wrong on ${doingWhat}`;
|
|
135
|
-
this.logger.error(sysMsg, ex);
|
|
136
|
-
}
|
|
137
|
-
const result = new api_helpers_1.OpResult({ data });
|
|
138
|
-
result.addError('', msg, api_helpers_1.OP_RESULT_CODES.EXCEPTION);
|
|
139
|
-
return result.toJS();
|
|
140
|
-
}
|
|
141
|
-
checkResult(args) {
|
|
142
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
-
const { hasTransaction, result, sameRequest, doingWhat } = args;
|
|
144
|
-
if (sameRequest) {
|
|
145
|
-
return;
|
|
146
|
-
}
|
|
147
|
-
if (hasTransaction && result) {
|
|
148
|
-
if (result.code >= api_helpers_1.OP_RESULT_CODES.OK) {
|
|
149
|
-
this.logger.debug(`Commit DB transaction on ${doingWhat}`);
|
|
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
|
-
}
|
|
156
|
-
}
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
runAction(opt) {
|
|
160
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
-
const { args, validate, doAuth, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace } = opt || {};
|
|
162
|
-
// If we process action with the same requestId as requestId passed to the constructor it means
|
|
163
|
-
// we are processing the same request from FE and can skip some of the operations like transactions and etc
|
|
164
|
-
const sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
|
|
165
|
-
// TODO: Calculate total thread block time between async calls withing the request
|
|
166
|
-
// this.measureTool.threadBlockTime(doingWhat, ({ name, durationMs }: any) => {
|
|
167
|
-
// this.logger.debug(`The ${name} blocked thread for the time of ${durationMs} ms`);
|
|
168
|
-
// });
|
|
169
|
-
if (shouldLogArgs == null || shouldLogArgs === true) {
|
|
170
|
-
this.logger.debug(`The ${doingWhat} started:`, args);
|
|
171
|
-
}
|
|
172
|
-
else {
|
|
173
|
-
this.logger.debug(`The ${doingWhat} started`);
|
|
174
|
-
}
|
|
175
|
-
this.measureTool.start();
|
|
176
|
-
let step = doingWhat || '';
|
|
177
|
-
const setStep = (stepName) => {
|
|
178
|
-
step = stepName || step;
|
|
179
|
-
const time = this.measureTool.addCheckpoint(stepName);
|
|
180
|
-
if (stepName) {
|
|
181
|
-
this.logger.debug(`${time.name}. Since last step: ${time.durationMs} ms`);
|
|
182
|
-
}
|
|
183
|
-
};
|
|
184
|
-
const actionOpt = {
|
|
185
|
-
core: this,
|
|
186
|
-
setStep,
|
|
187
|
-
sameRequest,
|
|
188
|
-
hasTransaction,
|
|
189
|
-
};
|
|
190
|
-
try {
|
|
191
|
-
if (!sameRequest) {
|
|
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
|
-
}
|
|
217
|
-
}
|
|
218
|
-
}
|
|
219
|
-
let validatedItems = {};
|
|
220
|
-
if (typeof validate === 'function') {
|
|
221
|
-
this.measureTool.addCheckpoint('Validate parameters: start');
|
|
222
|
-
// If we have validator provided, use it to validate data before running action
|
|
223
|
-
// validator is expected to return an array:
|
|
224
|
-
// [0] - true or validation error object,
|
|
225
|
-
// [1] - an object with some fetched data to pass ot the action handler
|
|
226
|
-
const valideResult = yield validate(args, actionOpt);
|
|
227
|
-
let result = valideResult;
|
|
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 });
|
|
239
|
-
}
|
|
240
|
-
}
|
|
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
|
-
}
|
|
249
|
-
catch (ex) {
|
|
250
|
-
this.logger.error(`An exception has occurred on ${step}:`, ex);
|
|
251
|
-
if (!sameRequest && hasTransaction) {
|
|
252
|
-
this.logger.debug(`Rolling back DB transaction on ${step} due to an excaption`);
|
|
253
|
-
yield this.getDb().rollbackTransaction();
|
|
254
|
-
}
|
|
255
|
-
let errMessage = '';
|
|
256
|
-
try {
|
|
257
|
-
errMessage = yield this.onException(args, Object.assign(Object.assign({}, actionOpt), { doingWhat, exception: ex }));
|
|
258
|
-
if (check_types_1.default.function(onException)) {
|
|
259
|
-
errMessage = yield onException(args, Object.assign(Object.assign({}, actionOpt), { doingWhat, exception: ex }));
|
|
260
|
-
}
|
|
261
|
-
}
|
|
262
|
-
catch (ex2) {
|
|
263
|
-
this.logger.error(`An exception has occured when handling exception:`, ex2);
|
|
264
|
-
}
|
|
265
|
-
if (!errMessage) {
|
|
266
|
-
errMessage = doingWhat;
|
|
267
|
-
}
|
|
268
|
-
if (doingWhat !== step) {
|
|
269
|
-
errMessage = `${step} failed on ${doingWhat}`;
|
|
270
|
-
}
|
|
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
|
-
}
|
|
275
|
-
});
|
|
276
|
-
}
|
|
277
|
-
list(args) {
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
return this.runAction({
|
|
281
|
-
args,
|
|
282
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.list,
|
|
283
|
-
actionName: constants_1.ACTIONS.LIST,
|
|
284
|
-
validate: this.getValidators().list,
|
|
285
|
-
accessRights: [utils_1.ACCESS_RIGHTS.LIST],
|
|
286
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
287
|
-
var
|
|
288
|
-
const object = yield this.beforeList(args, opt);
|
|
289
|
-
if (object instanceof api_helpers_1.OpResult) {
|
|
290
|
-
return object;
|
|
291
|
-
}
|
|
292
|
-
const items = yield ((
|
|
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
|
-
|
|
304
|
-
|
|
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
|
|
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 ((
|
|
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
|
-
});
|
|
331
|
-
}
|
|
332
|
-
getMany(args) {
|
|
333
|
-
|
|
334
|
-
|
|
335
|
-
return this.runAction({
|
|
336
|
-
args,
|
|
337
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.getMany,
|
|
338
|
-
actionName: constants_1.ACTIONS.GET_MANY,
|
|
339
|
-
validate: this.getValidators().getMany,
|
|
340
|
-
accessRights: [utils_1.ACCESS_RIGHTS.GET],
|
|
341
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
342
|
-
var
|
|
343
|
-
const args2use = yield this.beforeGetMany(args, opt);
|
|
344
|
-
if (args2use instanceof api_helpers_1.OpResult) {
|
|
345
|
-
return args2use;
|
|
346
|
-
}
|
|
347
|
-
if (!Array.isArray(args2use === null || args2use === void 0 ? void 0 : args2use.ids)) {
|
|
348
|
-
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
349
|
-
}
|
|
350
|
-
const items = yield ((
|
|
351
|
-
const result = yield this.afterGetMany(items, opt);
|
|
352
|
-
return this.success(result);
|
|
353
|
-
}),
|
|
354
|
-
hasTransaction: false,
|
|
355
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.getMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'geting multiple items',
|
|
356
|
-
});
|
|
357
|
-
});
|
|
358
|
-
}
|
|
359
|
-
getGatewayToSetOrerNo() {
|
|
360
|
-
return this.getContext().gateways[this.config.gatewayName];
|
|
361
|
-
}
|
|
362
|
-
getFilterToSetOrderNo(args) {
|
|
363
|
-
return {};
|
|
364
|
-
}
|
|
365
|
-
create(args) {
|
|
366
|
-
|
|
367
|
-
|
|
368
|
-
return this.runAction({
|
|
369
|
-
args,
|
|
370
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.create,
|
|
371
|
-
actionName: constants_1.ACTIONS.CREATE,
|
|
372
|
-
validate: this.getValidators().create,
|
|
373
|
-
accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
|
|
374
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
375
|
-
var
|
|
376
|
-
const { params } = args || {};
|
|
377
|
-
if (this.config.hasOrderNo === true) {
|
|
378
|
-
this.setOrderNo({
|
|
379
|
-
params,
|
|
380
|
-
filter: this.getFilterToSetOrderNo({ params, opt }),
|
|
381
|
-
gateway: this.getGatewayToSetOrerNo(),
|
|
382
|
-
});
|
|
383
|
-
}
|
|
384
|
-
const object = yield this.beforeCreate(params, opt);
|
|
385
|
-
if (object instanceof api_helpers_1.OpResult) {
|
|
386
|
-
return object;
|
|
387
|
-
}
|
|
388
|
-
const items = yield ((
|
|
389
|
-
const result = yield this.afterCreate(items, opt);
|
|
390
|
-
return this.success(result);
|
|
391
|
-
}),
|
|
392
|
-
hasTransaction: true,
|
|
393
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.create) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'creating an item',
|
|
394
|
-
});
|
|
395
|
-
});
|
|
396
|
-
}
|
|
397
|
-
createMany(args) {
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
return this.runAction({
|
|
401
|
-
args,
|
|
402
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.createMany,
|
|
403
|
-
actionName: constants_1.ACTIONS.CREATE_MANY,
|
|
404
|
-
validate: this.getValidators().createMany,
|
|
405
|
-
accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
|
|
406
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
407
|
-
var
|
|
408
|
-
const { params } = args || {};
|
|
409
|
-
const objects = yield this.beforeCreateMany(params, opt);
|
|
410
|
-
if (objects instanceof api_helpers_1.OpResult) {
|
|
411
|
-
return objects;
|
|
412
|
-
}
|
|
413
|
-
const items = yield ((
|
|
414
|
-
const result = yield this.afterCreateMany(items, opt);
|
|
415
|
-
return this.success(result);
|
|
416
|
-
}),
|
|
417
|
-
hasTransaction: true,
|
|
418
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.createMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'creating multiple items',
|
|
419
|
-
});
|
|
420
|
-
});
|
|
421
|
-
}
|
|
422
|
-
update(args) {
|
|
423
|
-
|
|
424
|
-
|
|
425
|
-
return this.runAction({
|
|
426
|
-
args,
|
|
427
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.update,
|
|
428
|
-
actionName: constants_1.ACTIONS.UPDATE,
|
|
429
|
-
validate: this.getValidators().update,
|
|
430
|
-
accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
|
|
431
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
432
|
-
var
|
|
433
|
-
const { where, params } = args || {};
|
|
434
|
-
const object = yield this.beforeUpdate(params, opt);
|
|
435
|
-
if (object instanceof api_helpers_1.OpResult) {
|
|
436
|
-
return object;
|
|
437
|
-
}
|
|
438
|
-
const items = yield ((
|
|
439
|
-
const result = yield this.afterUpdate(items, opt);
|
|
440
|
-
return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
|
|
441
|
-
}),
|
|
442
|
-
hasTransaction: true,
|
|
443
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.update) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating an item',
|
|
444
|
-
});
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
|
-
updateMany(args) {
|
|
448
|
-
|
|
449
|
-
|
|
450
|
-
return this.runAction({
|
|
451
|
-
args,
|
|
452
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.updateMany,
|
|
453
|
-
actionName: constants_1.ACTIONS.UPDATE_MANY,
|
|
454
|
-
validate: this.getValidators().updateMany,
|
|
455
|
-
accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
|
|
456
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
457
|
-
var
|
|
458
|
-
const { params } = args || {};
|
|
459
|
-
const object = yield this.beforeUpdateMany(params, opt);
|
|
460
|
-
if (object instanceof api_helpers_1.OpResult) {
|
|
461
|
-
return object;
|
|
462
|
-
}
|
|
463
|
-
const items = yield ((
|
|
464
|
-
const result = yield this.afterUpdateMany(items, opt);
|
|
465
|
-
return this.success(result);
|
|
466
|
-
}),
|
|
467
|
-
hasTransaction: true,
|
|
468
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.updateMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating multiple items',
|
|
469
|
-
});
|
|
470
|
-
});
|
|
471
|
-
}
|
|
472
|
-
set(args) {
|
|
473
|
-
|
|
474
|
-
|
|
475
|
-
return this.runAction({
|
|
476
|
-
args,
|
|
477
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.set,
|
|
478
|
-
actionName: constants_1.ACTIONS.SET,
|
|
479
|
-
validate: this.getValidators().set,
|
|
480
|
-
accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
|
|
481
|
-
action: (args, opt /* { setStep, validatedItems, sameRequest } */) => __awaiter(this, void 0, void 0, function* () {
|
|
482
|
-
var
|
|
483
|
-
const { params } = args || {};
|
|
484
|
-
const object = yield this.beforeSet(params, opt);
|
|
485
|
-
if (object instanceof api_helpers_1.OpResult) {
|
|
486
|
-
return object;
|
|
487
|
-
}
|
|
488
|
-
const items = yield ((
|
|
489
|
-
const result = yield this.afterSet(items, opt);
|
|
490
|
-
return this.success(result);
|
|
491
|
-
}),
|
|
492
|
-
hasTransaction: true,
|
|
493
|
-
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.updateMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating multiple items',
|
|
494
|
-
});
|
|
495
|
-
});
|
|
496
|
-
}
|
|
497
|
-
remove(args) {
|
|
498
|
-
|
|
499
|
-
|
|
500
|
-
return this.runAction({
|
|
501
|
-
args,
|
|
502
|
-
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.remove,
|
|
503
|
-
actionName: constants_1.ACTIONS.REMOVE,
|
|
504
|
-
validate: this.getValidators().remove,
|
|
505
|
-
accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
|
|
506
|
-
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
507
|
-
var
|
|
508
|
-
const { where } = args || {};
|
|
509
|
-
const object = yield this.beforeRemove(where, opt);
|
|
510
|
-
if (object instanceof api_helpers_1.OpResult) {
|
|
511
|
-
return object;
|
|
512
|
-
}
|
|
513
|
-
const items = yield ((
|
|
514
|
-
const result = yield this.afterRemove(items, opt);
|
|
515
|
-
return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
|
|
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
|
-
|
|
524
|
-
|
|
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
|
|
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 ((
|
|
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
|
-
});
|
|
545
|
-
});
|
|
546
|
-
}
|
|
547
|
-
// eslint-disable-next-line
|
|
548
|
-
defaultAfterAndBefore(params, opt) {
|
|
549
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
550
|
-
return params;
|
|
551
|
-
});
|
|
552
|
-
}
|
|
553
|
-
/**
|
|
554
|
-
* Used to transform an object before sending it in the response
|
|
555
|
-
* @param {any} item An object coming from the database
|
|
556
|
-
* @param {BaseCoreActionsInterface} opt Options
|
|
557
|
-
* @returns {any} transformed object
|
|
558
|
-
*/
|
|
559
|
-
processItemOnOut(item, opt) {
|
|
560
|
-
return this.defaultAfterAndBefore(item, opt);
|
|
561
|
-
}
|
|
562
|
-
/**
|
|
563
|
-
* Used to transform an object before processing it on the request
|
|
564
|
-
* @param {any} item An object coming from the outside
|
|
565
|
-
* @param {BaseCoreActionsInterface} opt Options
|
|
566
|
-
* @returns {any} transformed object
|
|
567
|
-
*/
|
|
568
|
-
processItemOnIn(item, opt) {
|
|
569
|
-
return this.defaultAfterAndBefore(item, opt);
|
|
570
|
-
}
|
|
571
|
-
beforeGet(item, opt) {
|
|
572
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
573
|
-
return this.defaultAfterAndBefore(item, opt);
|
|
574
|
-
});
|
|
575
|
-
}
|
|
576
|
-
beforeGetMany(items, opt) {
|
|
577
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
578
|
-
return this.defaultAfterAndBefore(items, opt);
|
|
579
|
-
});
|
|
580
|
-
}
|
|
581
|
-
beforeList(filter, opt) {
|
|
582
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
583
|
-
return this.defaultAfterAndBefore(filter, opt);
|
|
584
|
-
});
|
|
585
|
-
}
|
|
586
|
-
afterGet(item, opt) {
|
|
587
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
588
|
-
return this.processItemOnOut(item, opt);
|
|
589
|
-
});
|
|
590
|
-
}
|
|
591
|
-
afterGetMany(items, opt) {
|
|
592
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
593
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
594
|
-
});
|
|
595
|
-
}
|
|
596
|
-
afterList(items, opt) {
|
|
597
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
598
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
599
|
-
});
|
|
600
|
-
}
|
|
601
|
-
beforeCreate(params, opt) {
|
|
602
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
603
|
-
return this.processItemOnIn(params, opt);
|
|
604
|
-
});
|
|
605
|
-
}
|
|
606
|
-
afterCreate(items, opt) {
|
|
607
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
608
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
609
|
-
});
|
|
610
|
-
}
|
|
611
|
-
beforeCreateMany(items, opt) {
|
|
612
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
613
|
-
return items.map((item) => this.processItemOnIn(item, opt));
|
|
614
|
-
});
|
|
615
|
-
}
|
|
616
|
-
afterCreateMany(items, opt) {
|
|
617
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
618
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
beforeUpdate(params, opt) {
|
|
622
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
623
|
-
return this.processItemOnIn(params, opt);
|
|
624
|
-
});
|
|
625
|
-
}
|
|
626
|
-
afterUpdate(items, opt) {
|
|
627
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
628
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
629
|
-
});
|
|
630
|
-
}
|
|
631
|
-
beforeUpdateMany(items, opt) {
|
|
632
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
633
|
-
return items.map((item) => {
|
|
634
|
-
return {
|
|
635
|
-
params: this.processItemOnIn(item.params, opt),
|
|
636
|
-
where: item.where,
|
|
637
|
-
};
|
|
638
|
-
});
|
|
639
|
-
});
|
|
640
|
-
}
|
|
641
|
-
afterUpdateMany(items, opt) {
|
|
642
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
643
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
644
|
-
});
|
|
645
|
-
}
|
|
646
|
-
beforeSet(params, opt) {
|
|
647
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
648
|
-
return (0, lodash_1.castArray)(params).map((item) => this.processItemOnIn(item, opt));
|
|
649
|
-
});
|
|
650
|
-
}
|
|
651
|
-
afterSet(items, opt) {
|
|
652
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
653
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
654
|
-
});
|
|
655
|
-
}
|
|
656
|
-
beforeRemove(params, opt) {
|
|
657
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
658
|
-
return this.defaultAfterAndBefore(params, opt);
|
|
659
|
-
});
|
|
660
|
-
}
|
|
661
|
-
afterRemove(items, opt) {
|
|
662
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
663
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
664
|
-
});
|
|
665
|
-
}
|
|
666
|
-
beforeRemoveMany(params, opt) {
|
|
667
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
668
|
-
return this.defaultAfterAndBefore(params, opt);
|
|
669
|
-
});
|
|
670
|
-
}
|
|
671
|
-
afterRemoveMany(items, opt) {
|
|
672
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
673
|
-
return items.map((item) => this.processItemOnOut(item, opt));
|
|
674
|
-
});
|
|
675
|
-
}
|
|
676
|
-
onException(params, opt) {
|
|
677
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
678
|
-
return '';
|
|
679
|
-
});
|
|
680
|
-
}
|
|
681
|
-
/**
|
|
682
|
-
* Returns JWT secret and its settings to verify received JWT token
|
|
683
|
-
* @param params Object of parameters passed to the authenticate method
|
|
684
|
-
* @returns Object with the jwtSecret and verifyTokenArgs
|
|
685
|
-
*/
|
|
686
|
-
// eslint-disable-next-line
|
|
687
|
-
getJwtTokenSecretAndSettings(params) {
|
|
688
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
689
|
-
const { space } = this.getContext();
|
|
690
|
-
return {
|
|
691
|
-
jwtSecret: (space === null || space === void 0 ? void 0 : space.tokenSecret) || this.config.jwtSecret || '',
|
|
692
|
-
verifyTokenArgs: this.config.verifyTokenArgs,
|
|
693
|
-
};
|
|
694
|
-
});
|
|
695
|
-
}
|
|
696
|
-
findSpace() {
|
|
697
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
698
|
-
const { context } = this.config;
|
|
699
|
-
if (context.space) {
|
|
700
|
-
return [true, context.space];
|
|
701
|
-
}
|
|
702
|
-
let space = null;
|
|
703
|
-
if (context.spaceGw) {
|
|
704
|
-
if (!context.spaceId) {
|
|
705
|
-
return [false, 'Space not specified'];
|
|
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;
|
|
713
|
-
}
|
|
714
|
-
return [true, space];
|
|
715
|
-
});
|
|
716
|
-
}
|
|
717
|
-
extractCredentials(args) {
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
const { req, space, authorization } = args;
|
|
721
|
-
const token = ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a[space.cookieName]) || authorization;
|
|
722
|
-
const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || args.fingerprint || '';
|
|
723
|
-
return {
|
|
724
|
-
token,
|
|
725
|
-
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
|
-
});
|
|
747
|
-
const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
|
|
748
|
-
if (token && jwtSecret) {
|
|
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;
|
|
761
|
-
}
|
|
762
|
-
if (!authByToken) {
|
|
763
|
-
if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
|
|
764
|
-
return [true, ''];
|
|
765
|
-
}
|
|
766
|
-
else if ((space === null || space === void 0 ? void 0 : space.apiKey) && (space === null || space === void 0 ? void 0 : space.apiKey) !== context.apiKey) {
|
|
767
|
-
return [false, 'API Key is invalid'];
|
|
768
|
-
}
|
|
769
|
-
}
|
|
770
|
-
return [true, ''];
|
|
771
|
-
});
|
|
772
|
-
}
|
|
773
|
-
processRemovedRecord(record) {
|
|
774
|
-
if (record && typeof record.id === 'string') {
|
|
775
|
-
record.id = record.id.split('~')[0];
|
|
776
|
-
}
|
|
777
|
-
return record;
|
|
778
|
-
}
|
|
779
|
-
addRemovedModifierToFields(fields) {
|
|
780
|
-
const obj = {};
|
|
781
|
-
if (!fields) {
|
|
782
|
-
this.logger.log(`The 'addRemovedModifierToFields' method expected its fields parameter to be a field name or an array of table field names but got:`, fields);
|
|
783
|
-
return obj;
|
|
784
|
-
}
|
|
785
|
-
(0, lodash_1.castArray)(fields).forEach((field) => {
|
|
786
|
-
obj[field] = this.getDb().raw(`${field} || '~removed~' || uuid_generate_v4()`);
|
|
787
|
-
});
|
|
788
|
-
return obj;
|
|
789
|
-
}
|
|
790
|
-
setOrderNo(args) {
|
|
791
|
-
|
|
792
|
-
|
|
793
|
-
const { params, filter, gateway } = args || {};
|
|
794
|
-
if (typeof params !== 'object') {
|
|
795
|
-
throw new Error(`The 'setOrderNo' expected the parameter to have 'params' object but got: ${JSON.stringify(params)} `);
|
|
796
|
-
}
|
|
797
|
-
if (typeof gateway !== 'object') {
|
|
798
|
-
throw new Error(`The 'setOrderNo' expected the parameter to have 'gateway' object but got: ${JSON.stringify(gateway)} `);
|
|
799
|
-
}
|
|
800
|
-
if (typeof gateway.list !== 'function') {
|
|
801
|
-
throw new Error(`The 'setOrderNo' expected the 'gateway' object to have 'list' method but got: ${JSON.stringify(gateway.list)} `);
|
|
802
|
-
}
|
|
803
|
-
if (!params.orderNo) {
|
|
804
|
-
const lastRecord = yield gateway.list({
|
|
805
|
-
params: filter,
|
|
806
|
-
pagination: { pageSize: 1, sortBy: [{ name: constants_1.FIELDS.ORDER_NO, order: constants_1.ORDER_BYS.DESC }] },
|
|
807
|
-
});
|
|
808
|
-
if (lastRecord.length > 0) {
|
|
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
|
-
}
|
|
815
|
-
}
|
|
816
|
-
});
|
|
817
|
-
}
|
|
818
|
-
}
|
|
819
|
-
exports.BaseCore = BaseCore;
|
|
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
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.BaseCore = void 0;
|
|
16
|
+
const api_helpers_1 = require("@sdflc/api-helpers");
|
|
17
|
+
const utils_1 = require("@sdflc/utils");
|
|
18
|
+
const check_types_1 = __importDefault(require("check-types"));
|
|
19
|
+
const lodash_1 = require("lodash");
|
|
20
|
+
const commonValidators_1 = require("./commonValidators");
|
|
21
|
+
const constants_1 = require("./constants");
|
|
22
|
+
const userToken_1 = require("./userToken");
|
|
23
|
+
const defaultValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
|
|
24
|
+
return [true, {}];
|
|
25
|
+
};
|
|
26
|
+
const defaultMutationValidate = ( /*args: any, opt?: BaseCoreActionsInterface*/) => {
|
|
27
|
+
return [true, {}];
|
|
28
|
+
};
|
|
29
|
+
const defaultValidators = {
|
|
30
|
+
list: defaultValidate,
|
|
31
|
+
get: commonValidators_1.validateGet,
|
|
32
|
+
getMany: commonValidators_1.validateGetMany,
|
|
33
|
+
create: defaultValidate,
|
|
34
|
+
createMany: defaultValidate,
|
|
35
|
+
update: defaultValidate,
|
|
36
|
+
updateMany: defaultValidate,
|
|
37
|
+
set: defaultValidate,
|
|
38
|
+
remove: commonValidators_1.validateRemove,
|
|
39
|
+
removeMany: commonValidators_1.validateRemoveMany,
|
|
40
|
+
};
|
|
41
|
+
class BaseCore {
|
|
42
|
+
constructor(props) {
|
|
43
|
+
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m, _o, _p, _q, _r, _s, _t, _u, _v;
|
|
44
|
+
this.measureTool = new utils_1.MeasureTool();
|
|
45
|
+
this.config = props || {};
|
|
46
|
+
this.logger = this.config.logger instanceof utils_1.Logger ? this.config.logger : new utils_1.Logger({ level: utils_1.LoggerLevels.ERROR });
|
|
47
|
+
this.config.doAuthOnAction = {
|
|
48
|
+
list: ((_a = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.list) != null ? (_b = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _b === void 0 ? void 0 : _b.list : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
49
|
+
get: ((_c = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _c === void 0 ? void 0 : _c.get) != null ? (_d = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _d === void 0 ? void 0 : _d.get : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
50
|
+
getMany: ((_e = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _e === void 0 ? void 0 : _e.getMany) != null ? (_f = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _f === void 0 ? void 0 : _f.getMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
51
|
+
create: ((_g = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _g === void 0 ? void 0 : _g.create) != null ? (_h = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _h === void 0 ? void 0 : _h.create : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
52
|
+
createMany: ((_j = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _j === void 0 ? void 0 : _j.createMany) != null ? (_k = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _k === void 0 ? void 0 : _k.createMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
53
|
+
update: ((_l = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _l === void 0 ? void 0 : _l.update) != null ? (_m = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _m === void 0 ? void 0 : _m.update : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
54
|
+
updateMany: ((_o = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _o === void 0 ? void 0 : _o.updateMany) != null ? (_p = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _p === void 0 ? void 0 : _p.updateMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
55
|
+
set: ((_q = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _q === void 0 ? void 0 : _q.set) != null ? (_r = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _r === void 0 ? void 0 : _r.set : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
56
|
+
remove: ((_s = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _s === void 0 ? void 0 : _s.remove) != null ? (_t = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _t === void 0 ? void 0 : _t.remove : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
57
|
+
removeMany: ((_u = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _u === void 0 ? void 0 : _u.removeMany) != null ? (_v = props === null || props === void 0 ? void 0 : props.doAuthOnAction) === null || _v === void 0 ? void 0 : _v.removeMany : props === null || props === void 0 ? void 0 : props.doAuth,
|
|
58
|
+
};
|
|
59
|
+
}
|
|
60
|
+
getIdType() {
|
|
61
|
+
var _a;
|
|
62
|
+
return (_a = this.config.idType) !== null && _a !== void 0 ? _a : constants_1.ID_TYPES.UNKNOWN;
|
|
63
|
+
}
|
|
64
|
+
getDb() {
|
|
65
|
+
return this.config.db;
|
|
66
|
+
}
|
|
67
|
+
getRequestId() {
|
|
68
|
+
return this.config.context.requestId;
|
|
69
|
+
}
|
|
70
|
+
getContext() {
|
|
71
|
+
return this.config.context;
|
|
72
|
+
}
|
|
73
|
+
getGateways() {
|
|
74
|
+
return this.config.context.gateways || {};
|
|
75
|
+
}
|
|
76
|
+
getGateway(name) {
|
|
77
|
+
var _a;
|
|
78
|
+
return (_a = this.getGateways()) === null || _a === void 0 ? void 0 : _a[name || this.config.gatewayName];
|
|
79
|
+
}
|
|
80
|
+
getValidators() {
|
|
81
|
+
return defaultValidators;
|
|
82
|
+
}
|
|
83
|
+
success(data, code) {
|
|
84
|
+
const code2use = code != null ? code : api_helpers_1.OP_RESULT_CODES.OK;
|
|
85
|
+
let data2use = data;
|
|
86
|
+
if (!Array.isArray(data2use)) {
|
|
87
|
+
if (data) {
|
|
88
|
+
data2use = [data];
|
|
89
|
+
}
|
|
90
|
+
else {
|
|
91
|
+
data2use = [];
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return new api_helpers_1.OpResult({
|
|
95
|
+
code: code2use,
|
|
96
|
+
data: data2use,
|
|
97
|
+
errors: [],
|
|
98
|
+
}).toJS();
|
|
99
|
+
}
|
|
100
|
+
failure(code, errorMsg, data) {
|
|
101
|
+
const result = new api_helpers_1.OpResult({ data });
|
|
102
|
+
result.addError('', errorMsg, code);
|
|
103
|
+
return result.toJS();
|
|
104
|
+
}
|
|
105
|
+
validationFailure(ex) {
|
|
106
|
+
const result = new api_helpers_1.OpResult({ code: api_helpers_1.OP_RESULT_CODES.VALIDATION_FAILED });
|
|
107
|
+
if (ex instanceof api_helpers_1.OpResult) {
|
|
108
|
+
return result.toJS();
|
|
109
|
+
}
|
|
110
|
+
Object.keys((ex === null || ex === void 0 ? void 0 : ex.errors) || {}).forEach((key) => {
|
|
111
|
+
const obj = ex.errors[key];
|
|
112
|
+
if (Array.isArray(obj.errors)) {
|
|
113
|
+
// if we have multiple rules failures the add them all
|
|
114
|
+
obj.errors.forEach((item) => {
|
|
115
|
+
result.addError(key, item.message);
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
else {
|
|
119
|
+
result.addError(key, obj.message);
|
|
120
|
+
}
|
|
121
|
+
});
|
|
122
|
+
return result.toJS();
|
|
123
|
+
}
|
|
124
|
+
exceptionFailure(ex, doingWhat, data) {
|
|
125
|
+
const [isDbError, dbErrorMsg] = this.getDb().isDbError(ex);
|
|
126
|
+
const isOpResult = typeof ex.getErrorSummary === 'function';
|
|
127
|
+
const message = isOpResult ? ex.getErrorSummary() : ex.message;
|
|
128
|
+
const sysMsg = `An exception has occurred on ${doingWhat} with message ${message}:`;
|
|
129
|
+
let msg = '';
|
|
130
|
+
if (isDbError) {
|
|
131
|
+
msg = dbErrorMsg;
|
|
132
|
+
}
|
|
133
|
+
else {
|
|
134
|
+
msg = isOpResult ? ex.getErrorSummary() : `Sorry, something went wrong on ${doingWhat}`;
|
|
135
|
+
this.logger.error(sysMsg, ex);
|
|
136
|
+
}
|
|
137
|
+
const result = new api_helpers_1.OpResult({ data });
|
|
138
|
+
result.addError('', msg, api_helpers_1.OP_RESULT_CODES.EXCEPTION);
|
|
139
|
+
return result.toJS();
|
|
140
|
+
}
|
|
141
|
+
checkResult(args) {
|
|
142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
const { hasTransaction, result, sameRequest, doingWhat } = args;
|
|
144
|
+
if (sameRequest) {
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
if (hasTransaction && result) {
|
|
148
|
+
if (result.code >= api_helpers_1.OP_RESULT_CODES.OK) {
|
|
149
|
+
this.logger.debug(`Commit DB transaction on ${doingWhat}`);
|
|
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
|
+
}
|
|
156
|
+
}
|
|
157
|
+
});
|
|
158
|
+
}
|
|
159
|
+
runAction(opt) {
|
|
160
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
161
|
+
const { args, validate, doAuth, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace } = opt || {};
|
|
162
|
+
// If we process action with the same requestId as requestId passed to the constructor it means
|
|
163
|
+
// we are processing the same request from FE and can skip some of the operations like transactions and etc
|
|
164
|
+
const sameRequest = !!this.config.context.requestId && (args === null || args === void 0 ? void 0 : args.requestId) === this.config.context.requestId;
|
|
165
|
+
// TODO: Calculate total thread block time between async calls withing the request
|
|
166
|
+
// this.measureTool.threadBlockTime(doingWhat, ({ name, durationMs }: any) => {
|
|
167
|
+
// this.logger.debug(`The ${name} blocked thread for the time of ${durationMs} ms`);
|
|
168
|
+
// });
|
|
169
|
+
if (shouldLogArgs == null || shouldLogArgs === true) {
|
|
170
|
+
this.logger.debug(`The ${doingWhat} started:`, args);
|
|
171
|
+
}
|
|
172
|
+
else {
|
|
173
|
+
this.logger.debug(`The ${doingWhat} started`);
|
|
174
|
+
}
|
|
175
|
+
this.measureTool.start();
|
|
176
|
+
let step = doingWhat || '';
|
|
177
|
+
const setStep = (stepName) => {
|
|
178
|
+
step = stepName || step;
|
|
179
|
+
const time = this.measureTool.addCheckpoint(stepName);
|
|
180
|
+
if (stepName) {
|
|
181
|
+
this.logger.debug(`${time.name}. Since last step: ${time.durationMs} ms`);
|
|
182
|
+
}
|
|
183
|
+
};
|
|
184
|
+
const actionOpt = {
|
|
185
|
+
core: this,
|
|
186
|
+
setStep,
|
|
187
|
+
sameRequest,
|
|
188
|
+
hasTransaction,
|
|
189
|
+
};
|
|
190
|
+
try {
|
|
191
|
+
if (!sameRequest) {
|
|
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
|
+
}
|
|
217
|
+
}
|
|
218
|
+
}
|
|
219
|
+
let validatedItems = {};
|
|
220
|
+
if (typeof validate === 'function') {
|
|
221
|
+
this.measureTool.addCheckpoint('Validate parameters: start');
|
|
222
|
+
// If we have validator provided, use it to validate data before running action
|
|
223
|
+
// validator is expected to return an array:
|
|
224
|
+
// [0] - true or validation error object,
|
|
225
|
+
// [1] - an object with some fetched data to pass ot the action handler
|
|
226
|
+
const valideResult = yield validate(args, actionOpt);
|
|
227
|
+
let result = valideResult;
|
|
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 });
|
|
239
|
+
}
|
|
240
|
+
}
|
|
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
|
+
}
|
|
249
|
+
catch (ex) {
|
|
250
|
+
this.logger.error(`An exception has occurred on ${step}:`, ex);
|
|
251
|
+
if (!sameRequest && hasTransaction) {
|
|
252
|
+
this.logger.debug(`Rolling back DB transaction on ${step} due to an excaption`);
|
|
253
|
+
yield this.getDb().rollbackTransaction();
|
|
254
|
+
}
|
|
255
|
+
let errMessage = '';
|
|
256
|
+
try {
|
|
257
|
+
errMessage = yield this.onException(args, Object.assign(Object.assign({}, actionOpt), { doingWhat, exception: ex }));
|
|
258
|
+
if (check_types_1.default.function(onException)) {
|
|
259
|
+
errMessage = yield onException(args, Object.assign(Object.assign({}, actionOpt), { doingWhat, exception: ex }));
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
catch (ex2) {
|
|
263
|
+
this.logger.error(`An exception has occured when handling exception:`, ex2);
|
|
264
|
+
}
|
|
265
|
+
if (!errMessage) {
|
|
266
|
+
errMessage = doingWhat;
|
|
267
|
+
}
|
|
268
|
+
if (doingWhat !== step) {
|
|
269
|
+
errMessage = `${step} failed on ${doingWhat}`;
|
|
270
|
+
}
|
|
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
|
+
}
|
|
275
|
+
});
|
|
276
|
+
}
|
|
277
|
+
list(args) {
|
|
278
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
279
|
+
var _a, _b, _c, _d;
|
|
280
|
+
return this.runAction({
|
|
281
|
+
args,
|
|
282
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.list,
|
|
283
|
+
actionName: constants_1.ACTIONS.LIST,
|
|
284
|
+
validate: this.getValidators().list,
|
|
285
|
+
accessRights: [utils_1.ACCESS_RIGHTS.LIST],
|
|
286
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
var _a;
|
|
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
|
+
});
|
|
331
|
+
}
|
|
332
|
+
getMany(args) {
|
|
333
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
334
|
+
var _a, _b, _c, _d;
|
|
335
|
+
return this.runAction({
|
|
336
|
+
args,
|
|
337
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.getMany,
|
|
338
|
+
actionName: constants_1.ACTIONS.GET_MANY,
|
|
339
|
+
validate: this.getValidators().getMany,
|
|
340
|
+
accessRights: [utils_1.ACCESS_RIGHTS.GET],
|
|
341
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
342
|
+
var _a;
|
|
343
|
+
const args2use = yield this.beforeGetMany(args, opt);
|
|
344
|
+
if (args2use instanceof api_helpers_1.OpResult) {
|
|
345
|
+
return args2use;
|
|
346
|
+
}
|
|
347
|
+
if (!Array.isArray(args2use === null || args2use === void 0 ? void 0 : args2use.ids)) {
|
|
348
|
+
return this.failure(api_helpers_1.OP_RESULT_CODES.NOT_FOUND, '', []);
|
|
349
|
+
}
|
|
350
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.getMany(args2use.ids));
|
|
351
|
+
const result = yield this.afterGetMany(items, opt);
|
|
352
|
+
return this.success(result);
|
|
353
|
+
}),
|
|
354
|
+
hasTransaction: false,
|
|
355
|
+
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.getMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'geting multiple items',
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
}
|
|
359
|
+
getGatewayToSetOrerNo() {
|
|
360
|
+
return this.getContext().gateways[this.config.gatewayName];
|
|
361
|
+
}
|
|
362
|
+
getFilterToSetOrderNo(args) {
|
|
363
|
+
return {};
|
|
364
|
+
}
|
|
365
|
+
create(args) {
|
|
366
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
367
|
+
var _a, _b, _c, _d;
|
|
368
|
+
return this.runAction({
|
|
369
|
+
args,
|
|
370
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.create,
|
|
371
|
+
actionName: constants_1.ACTIONS.CREATE,
|
|
372
|
+
validate: this.getValidators().create,
|
|
373
|
+
accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
|
|
374
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
375
|
+
var _a;
|
|
376
|
+
const { params } = args || {};
|
|
377
|
+
if (this.config.hasOrderNo === true) {
|
|
378
|
+
this.setOrderNo({
|
|
379
|
+
params,
|
|
380
|
+
filter: this.getFilterToSetOrderNo({ params, opt }),
|
|
381
|
+
gateway: this.getGatewayToSetOrerNo(),
|
|
382
|
+
});
|
|
383
|
+
}
|
|
384
|
+
const object = yield this.beforeCreate(params, opt);
|
|
385
|
+
if (object instanceof api_helpers_1.OpResult) {
|
|
386
|
+
return object;
|
|
387
|
+
}
|
|
388
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.create(object));
|
|
389
|
+
const result = yield this.afterCreate(items, opt);
|
|
390
|
+
return this.success(result);
|
|
391
|
+
}),
|
|
392
|
+
hasTransaction: true,
|
|
393
|
+
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.create) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'creating an item',
|
|
394
|
+
});
|
|
395
|
+
});
|
|
396
|
+
}
|
|
397
|
+
createMany(args) {
|
|
398
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
399
|
+
var _a, _b, _c, _d;
|
|
400
|
+
return this.runAction({
|
|
401
|
+
args,
|
|
402
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.createMany,
|
|
403
|
+
actionName: constants_1.ACTIONS.CREATE_MANY,
|
|
404
|
+
validate: this.getValidators().createMany,
|
|
405
|
+
accessRights: [utils_1.ACCESS_RIGHTS.CREATE],
|
|
406
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
407
|
+
var _a;
|
|
408
|
+
const { params } = args || {};
|
|
409
|
+
const objects = yield this.beforeCreateMany(params, opt);
|
|
410
|
+
if (objects instanceof api_helpers_1.OpResult) {
|
|
411
|
+
return objects;
|
|
412
|
+
}
|
|
413
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.create(objects));
|
|
414
|
+
const result = yield this.afterCreateMany(items, opt);
|
|
415
|
+
return this.success(result);
|
|
416
|
+
}),
|
|
417
|
+
hasTransaction: true,
|
|
418
|
+
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.createMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'creating multiple items',
|
|
419
|
+
});
|
|
420
|
+
});
|
|
421
|
+
}
|
|
422
|
+
update(args) {
|
|
423
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
424
|
+
var _a, _b, _c, _d;
|
|
425
|
+
return this.runAction({
|
|
426
|
+
args,
|
|
427
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.update,
|
|
428
|
+
actionName: constants_1.ACTIONS.UPDATE,
|
|
429
|
+
validate: this.getValidators().update,
|
|
430
|
+
accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
|
|
431
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
432
|
+
var _a;
|
|
433
|
+
const { where, params } = args || {};
|
|
434
|
+
const object = yield this.beforeUpdate(params, opt);
|
|
435
|
+
if (object instanceof api_helpers_1.OpResult) {
|
|
436
|
+
return object;
|
|
437
|
+
}
|
|
438
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.update(where, object));
|
|
439
|
+
const result = yield this.afterUpdate(items, opt);
|
|
440
|
+
return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
|
|
441
|
+
}),
|
|
442
|
+
hasTransaction: true,
|
|
443
|
+
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.update) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating an item',
|
|
444
|
+
});
|
|
445
|
+
});
|
|
446
|
+
}
|
|
447
|
+
updateMany(args) {
|
|
448
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
449
|
+
var _a, _b, _c, _d;
|
|
450
|
+
return this.runAction({
|
|
451
|
+
args,
|
|
452
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.updateMany,
|
|
453
|
+
actionName: constants_1.ACTIONS.UPDATE_MANY,
|
|
454
|
+
validate: this.getValidators().updateMany,
|
|
455
|
+
accessRights: [utils_1.ACCESS_RIGHTS.UPDATE],
|
|
456
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
457
|
+
var _a;
|
|
458
|
+
const { params } = args || {};
|
|
459
|
+
const object = yield this.beforeUpdateMany(params, opt);
|
|
460
|
+
if (object instanceof api_helpers_1.OpResult) {
|
|
461
|
+
return object;
|
|
462
|
+
}
|
|
463
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.updateMany(object));
|
|
464
|
+
const result = yield this.afterUpdateMany(items, opt);
|
|
465
|
+
return this.success(result);
|
|
466
|
+
}),
|
|
467
|
+
hasTransaction: true,
|
|
468
|
+
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.updateMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating multiple items',
|
|
469
|
+
});
|
|
470
|
+
});
|
|
471
|
+
}
|
|
472
|
+
set(args) {
|
|
473
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
474
|
+
var _a, _b, _c, _d;
|
|
475
|
+
return this.runAction({
|
|
476
|
+
args,
|
|
477
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.set,
|
|
478
|
+
actionName: constants_1.ACTIONS.SET,
|
|
479
|
+
validate: this.getValidators().set,
|
|
480
|
+
accessRights: [utils_1.ACCESS_RIGHTS.CREATE, utils_1.ACCESS_RIGHTS.UPDATE],
|
|
481
|
+
action: (args, opt /* { setStep, validatedItems, sameRequest } */) => __awaiter(this, void 0, void 0, function* () {
|
|
482
|
+
var _a;
|
|
483
|
+
const { params } = args || {};
|
|
484
|
+
const object = yield this.beforeSet(params, opt);
|
|
485
|
+
if (object instanceof api_helpers_1.OpResult) {
|
|
486
|
+
return object;
|
|
487
|
+
}
|
|
488
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.set(object));
|
|
489
|
+
const result = yield this.afterSet(items, opt);
|
|
490
|
+
return this.success(result);
|
|
491
|
+
}),
|
|
492
|
+
hasTransaction: true,
|
|
493
|
+
doingWhat: (_d = (_c = (_b = this.config.doingWhat) === null || _b === void 0 ? void 0 : _b.updateMany) !== null && _c !== void 0 ? _c : this.config.name) !== null && _d !== void 0 ? _d : 'updating multiple items',
|
|
494
|
+
});
|
|
495
|
+
});
|
|
496
|
+
}
|
|
497
|
+
remove(args) {
|
|
498
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
499
|
+
var _a, _b, _c, _d;
|
|
500
|
+
return this.runAction({
|
|
501
|
+
args,
|
|
502
|
+
doAuth: (_a = this.config.doAuthOnAction) === null || _a === void 0 ? void 0 : _a.remove,
|
|
503
|
+
actionName: constants_1.ACTIONS.REMOVE,
|
|
504
|
+
validate: this.getValidators().remove,
|
|
505
|
+
accessRights: [utils_1.ACCESS_RIGHTS.REMOVE],
|
|
506
|
+
action: (args, opt) => __awaiter(this, void 0, void 0, function* () {
|
|
507
|
+
var _a;
|
|
508
|
+
const { where } = args || {};
|
|
509
|
+
const object = yield this.beforeRemove(where, opt);
|
|
510
|
+
if (object instanceof api_helpers_1.OpResult) {
|
|
511
|
+
return object;
|
|
512
|
+
}
|
|
513
|
+
const items = yield ((_a = this.getContext().gateways[this.config.gatewayName]) === null || _a === void 0 ? void 0 : _a.remove(object));
|
|
514
|
+
const result = yield this.afterRemove(items, opt);
|
|
515
|
+
return this.success(result, result.length > 0 ? api_helpers_1.OP_RESULT_CODES.OK : api_helpers_1.OP_RESULT_CODES.NOT_FOUND);
|
|
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
|
+
});
|
|
545
|
+
});
|
|
546
|
+
}
|
|
547
|
+
// eslint-disable-next-line
|
|
548
|
+
defaultAfterAndBefore(params, opt) {
|
|
549
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
550
|
+
return params;
|
|
551
|
+
});
|
|
552
|
+
}
|
|
553
|
+
/**
|
|
554
|
+
* Used to transform an object before sending it in the response
|
|
555
|
+
* @param {any} item An object coming from the database
|
|
556
|
+
* @param {BaseCoreActionsInterface} opt Options
|
|
557
|
+
* @returns {any} transformed object
|
|
558
|
+
*/
|
|
559
|
+
processItemOnOut(item, opt) {
|
|
560
|
+
return this.defaultAfterAndBefore(item, opt);
|
|
561
|
+
}
|
|
562
|
+
/**
|
|
563
|
+
* Used to transform an object before processing it on the request
|
|
564
|
+
* @param {any} item An object coming from the outside
|
|
565
|
+
* @param {BaseCoreActionsInterface} opt Options
|
|
566
|
+
* @returns {any} transformed object
|
|
567
|
+
*/
|
|
568
|
+
processItemOnIn(item, opt) {
|
|
569
|
+
return this.defaultAfterAndBefore(item, opt);
|
|
570
|
+
}
|
|
571
|
+
beforeGet(item, opt) {
|
|
572
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
573
|
+
return this.defaultAfterAndBefore(item, opt);
|
|
574
|
+
});
|
|
575
|
+
}
|
|
576
|
+
beforeGetMany(items, opt) {
|
|
577
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
578
|
+
return this.defaultAfterAndBefore(items, opt);
|
|
579
|
+
});
|
|
580
|
+
}
|
|
581
|
+
beforeList(filter, opt) {
|
|
582
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
583
|
+
return this.defaultAfterAndBefore(filter, opt);
|
|
584
|
+
});
|
|
585
|
+
}
|
|
586
|
+
afterGet(item, opt) {
|
|
587
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
588
|
+
return this.processItemOnOut(item, opt);
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
afterGetMany(items, opt) {
|
|
592
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
593
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
afterList(items, opt) {
|
|
597
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
598
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
599
|
+
});
|
|
600
|
+
}
|
|
601
|
+
beforeCreate(params, opt) {
|
|
602
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
603
|
+
return this.processItemOnIn(params, opt);
|
|
604
|
+
});
|
|
605
|
+
}
|
|
606
|
+
afterCreate(items, opt) {
|
|
607
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
608
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
609
|
+
});
|
|
610
|
+
}
|
|
611
|
+
beforeCreateMany(items, opt) {
|
|
612
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
613
|
+
return items.map((item) => this.processItemOnIn(item, opt));
|
|
614
|
+
});
|
|
615
|
+
}
|
|
616
|
+
afterCreateMany(items, opt) {
|
|
617
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
618
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
619
|
+
});
|
|
620
|
+
}
|
|
621
|
+
beforeUpdate(params, opt) {
|
|
622
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
623
|
+
return this.processItemOnIn(params, opt);
|
|
624
|
+
});
|
|
625
|
+
}
|
|
626
|
+
afterUpdate(items, opt) {
|
|
627
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
628
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
629
|
+
});
|
|
630
|
+
}
|
|
631
|
+
beforeUpdateMany(items, opt) {
|
|
632
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
633
|
+
return items.map((item) => {
|
|
634
|
+
return {
|
|
635
|
+
params: this.processItemOnIn(item.params, opt),
|
|
636
|
+
where: item.where,
|
|
637
|
+
};
|
|
638
|
+
});
|
|
639
|
+
});
|
|
640
|
+
}
|
|
641
|
+
afterUpdateMany(items, opt) {
|
|
642
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
643
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
644
|
+
});
|
|
645
|
+
}
|
|
646
|
+
beforeSet(params, opt) {
|
|
647
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
648
|
+
return (0, lodash_1.castArray)(params).map((item) => this.processItemOnIn(item, opt));
|
|
649
|
+
});
|
|
650
|
+
}
|
|
651
|
+
afterSet(items, opt) {
|
|
652
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
653
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
654
|
+
});
|
|
655
|
+
}
|
|
656
|
+
beforeRemove(params, opt) {
|
|
657
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
658
|
+
return this.defaultAfterAndBefore(params, opt);
|
|
659
|
+
});
|
|
660
|
+
}
|
|
661
|
+
afterRemove(items, opt) {
|
|
662
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
663
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
664
|
+
});
|
|
665
|
+
}
|
|
666
|
+
beforeRemoveMany(params, opt) {
|
|
667
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
668
|
+
return this.defaultAfterAndBefore(params, opt);
|
|
669
|
+
});
|
|
670
|
+
}
|
|
671
|
+
afterRemoveMany(items, opt) {
|
|
672
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
673
|
+
return items.map((item) => this.processItemOnOut(item, opt));
|
|
674
|
+
});
|
|
675
|
+
}
|
|
676
|
+
onException(params, opt) {
|
|
677
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
678
|
+
return '';
|
|
679
|
+
});
|
|
680
|
+
}
|
|
681
|
+
/**
|
|
682
|
+
* Returns JWT secret and its settings to verify received JWT token
|
|
683
|
+
* @param params Object of parameters passed to the authenticate method
|
|
684
|
+
* @returns Object with the jwtSecret and verifyTokenArgs
|
|
685
|
+
*/
|
|
686
|
+
// eslint-disable-next-line
|
|
687
|
+
getJwtTokenSecretAndSettings(params) {
|
|
688
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
689
|
+
const { space } = this.getContext();
|
|
690
|
+
return {
|
|
691
|
+
jwtSecret: (space === null || space === void 0 ? void 0 : space.tokenSecret) || this.config.jwtSecret || '',
|
|
692
|
+
verifyTokenArgs: this.config.verifyTokenArgs,
|
|
693
|
+
};
|
|
694
|
+
});
|
|
695
|
+
}
|
|
696
|
+
findSpace() {
|
|
697
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
698
|
+
const { context } = this.config;
|
|
699
|
+
if (context.space) {
|
|
700
|
+
return [true, context.space];
|
|
701
|
+
}
|
|
702
|
+
let space = null;
|
|
703
|
+
if (context.spaceGw) {
|
|
704
|
+
if (!context.spaceId) {
|
|
705
|
+
return [false, 'Space not specified'];
|
|
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;
|
|
713
|
+
}
|
|
714
|
+
return [true, space];
|
|
715
|
+
});
|
|
716
|
+
}
|
|
717
|
+
extractCredentials(args) {
|
|
718
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
719
|
+
var _a, _b;
|
|
720
|
+
const { req, space, authorization } = args;
|
|
721
|
+
const token = ((_a = req.cookies) === null || _a === void 0 ? void 0 : _a[space.cookieName]) || authorization;
|
|
722
|
+
const fingerprint = ((_b = req.cookies) === null || _b === void 0 ? void 0 : _b[space.fingerprintCookieName]) || args.fingerprint || '';
|
|
723
|
+
return {
|
|
724
|
+
token,
|
|
725
|
+
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
|
+
});
|
|
747
|
+
const { jwtSecret, verifyTokenArgs } = yield this.getJwtTokenSecretAndSettings(params);
|
|
748
|
+
if (token && jwtSecret) {
|
|
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;
|
|
761
|
+
}
|
|
762
|
+
if (!authByToken) {
|
|
763
|
+
if (this.config.interserviceApiKey && context.apiKey === this.config.interserviceApiKey) {
|
|
764
|
+
return [true, ''];
|
|
765
|
+
}
|
|
766
|
+
else if ((space === null || space === void 0 ? void 0 : space.apiKey) && (space === null || space === void 0 ? void 0 : space.apiKey) !== context.apiKey) {
|
|
767
|
+
return [false, 'API Key is invalid'];
|
|
768
|
+
}
|
|
769
|
+
}
|
|
770
|
+
return [true, ''];
|
|
771
|
+
});
|
|
772
|
+
}
|
|
773
|
+
processRemovedRecord(record) {
|
|
774
|
+
if (record && typeof record.id === 'string') {
|
|
775
|
+
record.id = record.id.split('~')[0];
|
|
776
|
+
}
|
|
777
|
+
return record;
|
|
778
|
+
}
|
|
779
|
+
addRemovedModifierToFields(fields) {
|
|
780
|
+
const obj = {};
|
|
781
|
+
if (!fields) {
|
|
782
|
+
this.logger.log(`The 'addRemovedModifierToFields' method expected its fields parameter to be a field name or an array of table field names but got:`, fields);
|
|
783
|
+
return obj;
|
|
784
|
+
}
|
|
785
|
+
(0, lodash_1.castArray)(fields).forEach((field) => {
|
|
786
|
+
obj[field] = this.getDb().raw(`${field} || '~removed~' || uuid_generate_v4()`);
|
|
787
|
+
});
|
|
788
|
+
return obj;
|
|
789
|
+
}
|
|
790
|
+
setOrderNo(args) {
|
|
791
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
792
|
+
var _a;
|
|
793
|
+
const { params, filter, gateway } = args || {};
|
|
794
|
+
if (typeof params !== 'object') {
|
|
795
|
+
throw new Error(`The 'setOrderNo' expected the parameter to have 'params' object but got: ${JSON.stringify(params)} `);
|
|
796
|
+
}
|
|
797
|
+
if (typeof gateway !== 'object') {
|
|
798
|
+
throw new Error(`The 'setOrderNo' expected the parameter to have 'gateway' object but got: ${JSON.stringify(gateway)} `);
|
|
799
|
+
}
|
|
800
|
+
if (typeof gateway.list !== 'function') {
|
|
801
|
+
throw new Error(`The 'setOrderNo' expected the 'gateway' object to have 'list' method but got: ${JSON.stringify(gateway.list)} `);
|
|
802
|
+
}
|
|
803
|
+
if (!params.orderNo) {
|
|
804
|
+
const lastRecord = yield gateway.list({
|
|
805
|
+
params: filter,
|
|
806
|
+
pagination: { pageSize: 1, sortBy: [{ name: constants_1.FIELDS.ORDER_NO, order: constants_1.ORDER_BYS.DESC }] },
|
|
807
|
+
});
|
|
808
|
+
if (lastRecord.length > 0) {
|
|
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
|
+
}
|
|
815
|
+
}
|
|
816
|
+
});
|
|
817
|
+
}
|
|
818
|
+
}
|
|
819
|
+
exports.BaseCore = BaseCore;
|