@sdflc/backend-helpers 1.0.107 → 1.0.109
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/build/BaseCore.js
CHANGED
|
@@ -129,23 +129,33 @@ class BaseCore {
|
|
|
129
129
|
return result.toJS();
|
|
130
130
|
}
|
|
131
131
|
async checkResult(args) {
|
|
132
|
-
const { hasTransaction, result, sameRequest, doingWhat } = args;
|
|
132
|
+
const { hasTransaction, result, sameRequest, doingWhat, skipCheckResult } = args;
|
|
133
133
|
if (sameRequest) {
|
|
134
134
|
return;
|
|
135
135
|
}
|
|
136
|
-
if (hasTransaction
|
|
137
|
-
if (
|
|
138
|
-
this.logger.debug(`
|
|
136
|
+
if (hasTransaction) {
|
|
137
|
+
if (skipCheckResult === true) {
|
|
138
|
+
this.logger.debug(`Skip result check and commit DB transaction on ${doingWhat}`);
|
|
139
139
|
await this.getDb().commitTransaction();
|
|
140
140
|
}
|
|
141
|
+
else if (result) {
|
|
142
|
+
if (result.code >= api_helpers_1.OP_RESULT_CODES.OK) {
|
|
143
|
+
this.logger.debug(`Commit DB transaction on ${doingWhat}`);
|
|
144
|
+
await this.getDb().commitTransaction();
|
|
145
|
+
}
|
|
146
|
+
else {
|
|
147
|
+
this.logger.debug(`Rollback DB transaction on ${doingWhat} as action result code is '${result.code}'`);
|
|
148
|
+
await this.getDb().rollbackTransaction();
|
|
149
|
+
}
|
|
150
|
+
}
|
|
141
151
|
else {
|
|
142
|
-
this.logger.debug(`
|
|
143
|
-
await this.getDb().
|
|
152
|
+
this.logger.debug(`Commit DB transaction on no result on ${doingWhat}`);
|
|
153
|
+
await this.getDb().commitTransaction();
|
|
144
154
|
}
|
|
145
155
|
}
|
|
146
156
|
}
|
|
147
157
|
async runAction(opt) {
|
|
148
|
-
const { args, validate, doAuth, roles, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace, } = opt || {};
|
|
158
|
+
const { args, validate, doAuth, roles, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace, skipCheckResult, } = opt || {};
|
|
149
159
|
// If we process action with the same requestId as requestId passed to the constructor it means
|
|
150
160
|
// we are processing the same request from FE and can skip some of the operations like transactions and etc
|
|
151
161
|
const sameRequest = !!this.config.context.requestId && args?.requestId === this.config.context.requestId;
|
|
@@ -239,7 +249,7 @@ class BaseCore {
|
|
|
239
249
|
let time = this.measureTool.stop();
|
|
240
250
|
this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
|
|
241
251
|
const result = await action(args, { ...actionOpt, validatedItems, args });
|
|
242
|
-
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
252
|
+
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat, skipCheckResult });
|
|
243
253
|
time = this.measureTool.stop(doingWhat);
|
|
244
254
|
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
245
255
|
return result;
|
package/build/baseController.js
CHANGED
|
@@ -41,6 +41,10 @@ class BaseController {
|
|
|
41
41
|
if (result instanceof api_helpers_1.OpResult) {
|
|
42
42
|
res.status(result.getHttpStatus()).json(result.toJS());
|
|
43
43
|
}
|
|
44
|
+
else if (result?.redirectUrl) {
|
|
45
|
+
res.redirect(result?.redirectUrl);
|
|
46
|
+
next();
|
|
47
|
+
}
|
|
44
48
|
else if (result) {
|
|
45
49
|
res.status(200).json(result);
|
|
46
50
|
}
|
package/build/esm/BaseCore.js
CHANGED
|
@@ -123,23 +123,33 @@ class BaseCore {
|
|
|
123
123
|
return result.toJS();
|
|
124
124
|
}
|
|
125
125
|
async checkResult(args) {
|
|
126
|
-
const { hasTransaction, result, sameRequest, doingWhat } = args;
|
|
126
|
+
const { hasTransaction, result, sameRequest, doingWhat, skipCheckResult } = args;
|
|
127
127
|
if (sameRequest) {
|
|
128
128
|
return;
|
|
129
129
|
}
|
|
130
|
-
if (hasTransaction
|
|
131
|
-
if (
|
|
132
|
-
this.logger.debug(`
|
|
130
|
+
if (hasTransaction) {
|
|
131
|
+
if (skipCheckResult === true) {
|
|
132
|
+
this.logger.debug(`Skip result check and commit DB transaction on ${doingWhat}`);
|
|
133
133
|
await this.getDb().commitTransaction();
|
|
134
134
|
}
|
|
135
|
+
else if (result) {
|
|
136
|
+
if (result.code >= OP_RESULT_CODES.OK) {
|
|
137
|
+
this.logger.debug(`Commit DB transaction on ${doingWhat}`);
|
|
138
|
+
await this.getDb().commitTransaction();
|
|
139
|
+
}
|
|
140
|
+
else {
|
|
141
|
+
this.logger.debug(`Rollback DB transaction on ${doingWhat} as action result code is '${result.code}'`);
|
|
142
|
+
await this.getDb().rollbackTransaction();
|
|
143
|
+
}
|
|
144
|
+
}
|
|
135
145
|
else {
|
|
136
|
-
this.logger.debug(`
|
|
137
|
-
await this.getDb().
|
|
146
|
+
this.logger.debug(`Commit DB transaction on no result on ${doingWhat}`);
|
|
147
|
+
await this.getDb().commitTransaction();
|
|
138
148
|
}
|
|
139
149
|
}
|
|
140
150
|
}
|
|
141
151
|
async runAction(opt) {
|
|
142
|
-
const { args, validate, doAuth, roles, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace, } = opt || {};
|
|
152
|
+
const { args, validate, doAuth, roles, action, hasTransaction, doingWhat, onException, shouldLogArgs, requireSpace, skipCheckResult, } = opt || {};
|
|
143
153
|
// If we process action with the same requestId as requestId passed to the constructor it means
|
|
144
154
|
// we are processing the same request from FE and can skip some of the operations like transactions and etc
|
|
145
155
|
const sameRequest = !!this.config.context.requestId && args?.requestId === this.config.context.requestId;
|
|
@@ -233,7 +243,7 @@ class BaseCore {
|
|
|
233
243
|
let time = this.measureTool.stop();
|
|
234
244
|
this.logger.debug(`Authorization and validation time for the ${doingWhat} took ${time.durationMs} ms`);
|
|
235
245
|
const result = await action(args, { ...actionOpt, validatedItems, args });
|
|
236
|
-
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat });
|
|
246
|
+
await this.checkResult({ hasTransaction, result, sameRequest, doingWhat, skipCheckResult });
|
|
237
247
|
time = this.measureTool.stop(doingWhat);
|
|
238
248
|
this.logger.debug(`The ${time.name} process time is ${time.durationMs} ms`);
|
|
239
249
|
return result;
|
|
@@ -35,6 +35,10 @@ class BaseController {
|
|
|
35
35
|
if (result instanceof OpResult) {
|
|
36
36
|
res.status(result.getHttpStatus()).json(result.toJS());
|
|
37
37
|
}
|
|
38
|
+
else if (result?.redirectUrl) {
|
|
39
|
+
res.redirect(result?.redirectUrl);
|
|
40
|
+
next();
|
|
41
|
+
}
|
|
38
42
|
else if (result) {
|
|
39
43
|
res.status(200).json(result);
|
|
40
44
|
}
|