skyeye-svc-common-utils 0.0.203 → 0.0.205
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/package.json +1 -1
- package/src/index.ts +2 -1
- package/src/utils/baseClass/baseController.ts +66 -73
package/package.json
CHANGED
package/src/index.ts
CHANGED
|
@@ -17,4 +17,5 @@ export * from './lib/azure/azureTableStorage';
|
|
|
17
17
|
export * from './lib/azure/azureBlobStorage';
|
|
18
18
|
export * from './utils/documentation/swagger';
|
|
19
19
|
export * from './lib/httpClient';
|
|
20
|
-
export * from './utils/monitoring/apm';
|
|
20
|
+
export * from './utils/monitoring/apm';
|
|
21
|
+
export * from './models/baseControllerParameter';
|
|
@@ -21,20 +21,27 @@ export abstract class BaseController {
|
|
|
21
21
|
queryRunner = connection.createQueryRunner();
|
|
22
22
|
await queryRunner.startTransaction();
|
|
23
23
|
|
|
24
|
-
return queryRunner;
|
|
24
|
+
return Promise.resolve(queryRunner);
|
|
25
25
|
} catch (error) {
|
|
26
26
|
if (queryRunner && !queryRunner.isReleased) {
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
})
|
|
27
|
+
try {
|
|
28
|
+
await queryRunner.release();
|
|
29
|
+
} catch (err) {
|
|
30
|
+
logger.error(`BaseController/startTransaction Release Error: ${err}`);
|
|
31
|
+
}
|
|
30
32
|
}
|
|
31
33
|
|
|
32
|
-
return
|
|
34
|
+
return Promise.reject(`Connect DB Fail!`);
|
|
33
35
|
}
|
|
34
36
|
}
|
|
35
37
|
|
|
36
38
|
async endTransaction(baseControllerParameter: BaseControllerParameter) {
|
|
37
39
|
try {
|
|
40
|
+
if(!baseControllerParameter.queryRunner) {
|
|
41
|
+
logger.warn(`BaseController/endTransaction: null / undefined queryRunner`);
|
|
42
|
+
return;
|
|
43
|
+
}
|
|
44
|
+
|
|
38
45
|
if (baseControllerParameter.queryRunner.isReleased) {
|
|
39
46
|
logger.error(`BaseController/endTransaction: Transaction Released before commit`);
|
|
40
47
|
}
|
|
@@ -55,7 +62,7 @@ export abstract class BaseController {
|
|
|
55
62
|
try {
|
|
56
63
|
await baseControllerParameter.queryRunner.release();
|
|
57
64
|
} catch (err) {
|
|
58
|
-
logger.error(`BaseController/endTransaction: ${err}`);
|
|
65
|
+
logger.error(`BaseController/endTransaction Release Error: ${err}`);
|
|
59
66
|
}
|
|
60
67
|
}
|
|
61
68
|
}
|
|
@@ -68,36 +75,31 @@ export abstract class BaseController {
|
|
|
68
75
|
baseControllerParameter.serviceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
|
|
69
76
|
|
|
70
77
|
try {
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
78
|
+
baseControllerParameter.userInfo = getUserInfo(res);
|
|
79
|
+
if (commonAppConfig.SqlEnable) {
|
|
80
|
+
BaseController.mutex.runExclusive(async () => {
|
|
74
81
|
this.startTransaction().then((queryRunner: QueryRunner) => {
|
|
75
|
-
|
|
76
|
-
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
sendResponse(res, next, baseControllerParameter.serviceResponse);
|
|
81
|
-
});
|
|
82
|
-
}).catch((err: Error) => {
|
|
83
|
-
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
84
|
-
this.endTransaction(baseControllerParameter).then(() => {
|
|
85
|
-
next(err);
|
|
86
|
-
});
|
|
87
|
-
});
|
|
88
|
-
} else {
|
|
89
|
-
res.status(500).send("Connect DB Fail!");
|
|
90
|
-
}
|
|
91
|
-
});
|
|
92
|
-
} else {
|
|
93
|
-
this.executeImpl(req.body, baseControllerParameter).then(() => {
|
|
82
|
+
baseControllerParameter.queryRunner = queryRunner;
|
|
83
|
+
this.executeImpl(req.body, baseControllerParameter);
|
|
84
|
+
}).then(() => {
|
|
85
|
+
this.endTransaction(baseControllerParameter);
|
|
86
|
+
}).then(() => {
|
|
94
87
|
sendResponse(res, next, baseControllerParameter.serviceResponse);
|
|
95
88
|
}).catch((err: Error) => {
|
|
96
89
|
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
90
|
+
this.endTransaction(baseControllerParameter).then(() => {
|
|
91
|
+
next(err);
|
|
92
|
+
});
|
|
93
|
+
});;
|
|
94
|
+
});
|
|
95
|
+
} else {
|
|
96
|
+
this.executeImpl(req.body, baseControllerParameter).then(() => {
|
|
97
|
+
sendResponse(res, next, baseControllerParameter.serviceResponse);
|
|
98
|
+
}).catch((err: Error) => {
|
|
99
|
+
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
100
|
+
next(err);
|
|
101
|
+
});
|
|
102
|
+
}
|
|
101
103
|
} catch (err) {
|
|
102
104
|
next(err);
|
|
103
105
|
}
|
|
@@ -110,55 +112,46 @@ export abstract class BaseController {
|
|
|
110
112
|
baseControllerParameter.serviceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
|
|
111
113
|
|
|
112
114
|
try {
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
|
|
121
|
-
|
|
115
|
+
const authorization: string = call.metadata.get(commonAppConst.httpHeader.AUTHORIZATION);
|
|
116
|
+
if (!!authorization && authorization.length) {
|
|
117
|
+
const headerInfo: string[] = authorization[0].split(' ');
|
|
118
|
+
if (headerInfo.length === 2 && headerInfo[0] === commonAppConst.httpHeader.BEARER) {
|
|
119
|
+
verifyJWT (headerInfo[1]).then((userInfo: UserInfo) => {
|
|
120
|
+
baseControllerParameter.userInfo = userInfo;
|
|
121
|
+
|
|
122
|
+
if (commonAppConfig.SqlEnable) {
|
|
123
|
+
BaseController.mutex.runExclusive(async () => {
|
|
122
124
|
this.startTransaction().then((queryRunner: QueryRunner) => {
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
return callback(null, baseControllerParameter.serviceResponse);
|
|
129
|
-
});
|
|
130
|
-
}).catch((err: Error) => {
|
|
131
|
-
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
132
|
-
baseControllerParameter.serviceResponse.error = true;
|
|
133
|
-
baseControllerParameter.serviceResponse.message = err.message;
|
|
134
|
-
|
|
135
|
-
this.endTransaction(baseControllerParameter).then(() => {
|
|
136
|
-
throw err;
|
|
137
|
-
});
|
|
138
|
-
});
|
|
139
|
-
} else {
|
|
140
|
-
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
141
|
-
baseControllerParameter.serviceResponse.error = true;
|
|
142
|
-
baseControllerParameter.serviceResponse.message = 'Connect DB Fail!';
|
|
143
|
-
|
|
144
|
-
throw new Error('Connect DB Fail!');
|
|
145
|
-
}
|
|
146
|
-
});
|
|
147
|
-
} else {
|
|
148
|
-
this.executeImpl(call.request, baseControllerParameter).then(() => {
|
|
125
|
+
baseControllerParameter.queryRunner = queryRunner;
|
|
126
|
+
this.executeImpl(call.request, baseControllerParameter);
|
|
127
|
+
}).then(() => {
|
|
128
|
+
this.endTransaction(baseControllerParameter);
|
|
129
|
+
}).then(() => {
|
|
149
130
|
return callback(null, baseControllerParameter.serviceResponse);
|
|
150
131
|
}).catch((err: Error) => {
|
|
151
132
|
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
152
133
|
baseControllerParameter.serviceResponse.error = true;
|
|
153
134
|
baseControllerParameter.serviceResponse.message = err.message;
|
|
154
135
|
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
136
|
+
this.endTransaction(baseControllerParameter).then(() => {
|
|
137
|
+
throw err;
|
|
138
|
+
});
|
|
139
|
+
});;
|
|
140
|
+
});
|
|
141
|
+
} else {
|
|
142
|
+
this.executeImpl(call.request, baseControllerParameter).then(() => {
|
|
143
|
+
return callback(null, baseControllerParameter.serviceResponse);
|
|
144
|
+
}).catch((err: Error) => {
|
|
145
|
+
baseControllerParameter.serviceResponse.isSuccess = false;
|
|
146
|
+
baseControllerParameter.serviceResponse.error = true;
|
|
147
|
+
baseControllerParameter.serviceResponse.message = err.message;
|
|
148
|
+
|
|
149
|
+
throw err;
|
|
150
|
+
});
|
|
151
|
+
}
|
|
152
|
+
});
|
|
160
153
|
}
|
|
161
|
-
}
|
|
154
|
+
}
|
|
162
155
|
} catch (err) {
|
|
163
156
|
logger.error(`BaseController/executeGrpc: ${err}`);
|
|
164
157
|
baseControllerParameter.serviceResponse.isSuccess = false;
|