skyeye-svc-common-utils 0.0.201 → 0.0.203

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 CHANGED
@@ -1,13 +1,13 @@
1
1
  {
2
2
  "name": "skyeye-svc-common-utils",
3
- "version": "0.0.201",
3
+ "version": "0.0.203",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",
7
7
  "scripts": {
8
8
  "build": "tsc -p ./tsconfig.json",
9
9
  "postinstall": "npm run build",
10
- "clean": "rm -rf node_modules && rm -rf dist && rm -rf package-lock.json",
10
+ "clean": "rm -rf package-lock.json node_modules dist",
11
11
  "cleanPublish": "npm run clean && npm publish"
12
12
  },
13
13
  "repository": {
@@ -0,0 +1,12 @@
1
+ import { ServiceResponse, UserInfo } from "../interfaces";
2
+ import { QueryRunner } from "typeorm";
3
+
4
+ export class BaseControllerParameter {
5
+ queryRunner: QueryRunner;
6
+ serviceResponse: ServiceResponse;
7
+ userInfo: UserInfo;
8
+
9
+ constructor() {
10
+ }
11
+ }
12
+
@@ -0,0 +1,5 @@
1
+ import { BaseControllerParameter } from "./baseControllerParameter";
2
+
3
+ export {
4
+ BaseControllerParameter
5
+ };
@@ -1,7 +1,8 @@
1
1
  import { Request, Response, NextFunction } from 'express';
2
2
  import { sendResponse, getUserInfo, verifyJWT } from '../commonUtils';
3
3
  import { logger } from '../logger/logger';
4
- import { UserInfo, ServiceResponse } from '../../interfaces';
4
+ import { UserInfo } from '../../interfaces';
5
+ import { BaseControllerParameter } from '../../models';
5
6
  import { QueryRunner, getConnection } from 'typeorm';
6
7
  import { commonAppConst } from '../appConst';
7
8
  import { commonAppConfig } from '../appConfig';
@@ -9,39 +10,50 @@ import { Mutex } from 'async-mutex';
9
10
 
10
11
 
11
12
  export abstract class BaseController {
12
- protected abstract executeImpl(params: any): Promise<any>;
13
- protected queryRunner: QueryRunner;
14
- protected userInfo: UserInfo;
15
- protected serviceResponse: ServiceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
13
+ protected abstract executeImpl(params: any, baseControllerParameter: BaseControllerParameter): Promise<any>;
16
14
  static mutex: Mutex = new Mutex();
17
15
 
18
- async startTransaction() {
16
+ async startTransaction(): Promise<QueryRunner> {
17
+ let queryRunner: QueryRunner;
18
+
19
19
  try {
20
20
  const connection = getConnection();
21
- this.queryRunner = connection.createQueryRunner();
22
- await this.queryRunner.startTransaction();
23
- return this.queryRunner.isTransactionActive;
21
+ queryRunner = connection.createQueryRunner();
22
+ await queryRunner.startTransaction();
23
+
24
+ return queryRunner;
24
25
  } catch (error) {
25
- if(this.queryRunner && !this.queryRunner.isReleased) await this.queryRunner.release().catch((e)=>{logger.error(`Release Error in startTransaction`)});
26
- return false;
26
+ if (queryRunner && !queryRunner.isReleased) {
27
+ await queryRunner.release().catch((e) => {
28
+ logger.error(`Release Error in startTransaction`);
29
+ });
30
+ }
31
+
32
+ return null;
27
33
  }
28
34
  }
29
35
 
30
- async endTransaction(requireRollback: boolean = false) {
36
+ async endTransaction(baseControllerParameter: BaseControllerParameter) {
31
37
  try {
32
- if (this.queryRunner.isReleased) { logger.error(`Transaction Released before commit`); }
38
+ if (baseControllerParameter.queryRunner.isReleased) {
39
+ logger.error(`BaseController/endTransaction: Transaction Released before commit`);
40
+ }
33
41
 
34
- if (this.queryRunner.isTransactionActive) {
35
- requireRollback ? await this.queryRunner.rollbackTransaction() : await this.queryRunner.commitTransaction();
42
+ if (baseControllerParameter.queryRunner.isTransactionActive) {
43
+ if (baseControllerParameter.serviceResponse.isSuccess) {
44
+ await baseControllerParameter.queryRunner.commitTransaction();
45
+ } else {
46
+ await baseControllerParameter.queryRunner.rollbackTransaction();
47
+ }
36
48
  } else {
37
- logger.error('BaseController/endTransaction: Transaction is inactive after released');
49
+ logger.error(`BaseController/endTransaction: Transaction is inactive after released`);
38
50
  }
39
51
  } catch (err) {
40
52
  logger.error(`BaseController/endTransaction: ${err}`);
41
53
  } finally {
42
- if (!this.queryRunner.isReleased) {
54
+ if (!baseControllerParameter.queryRunner.isReleased) {
43
55
  try {
44
- await this.queryRunner.release();
56
+ await baseControllerParameter.queryRunner.release();
45
57
  } catch (err) {
46
58
  logger.error(`BaseController/endTransaction: ${err}`);
47
59
  }
@@ -51,60 +63,107 @@ export abstract class BaseController {
51
63
 
52
64
  public async executeRest(req: Request, res: Response, next: NextFunction): Promise<void> {
53
65
  logger.profile(`${req.originalUrl}`);
66
+
67
+ let baseControllerParameter: BaseControllerParameter = new BaseControllerParameter();
68
+ baseControllerParameter.serviceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
69
+
54
70
  try {
55
- this.serviceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
56
- if (commonAppConfig.SqlEnable) {
57
- BaseController.mutex.runExclusive(async () => {
58
- if (await this.startTransaction()) {
59
- this.userInfo = getUserInfo(res);
60
- await this.executeImpl(req.body);
61
- await this.endTransaction(!this.serviceResponse.isSuccess);
62
- await sendResponse(res, next, this.serviceResponse);
63
- } else {
64
- res.status(500).send("Connect DB Fail!");
65
- }
66
- })
67
- }else {
68
- res.status(500).send("SQL Disabled");
69
- }
71
+ BaseController.mutex.runExclusive(async () => {
72
+ baseControllerParameter.userInfo = getUserInfo(res);
73
+ if (commonAppConfig.SqlEnable) {
74
+ this.startTransaction().then((queryRunner: QueryRunner) => {
75
+ if (!!queryRunner) {
76
+ baseControllerParameter.queryRunner = queryRunner;
77
+
78
+ this.executeImpl(req.body, baseControllerParameter).then(() => {
79
+ this.endTransaction(baseControllerParameter).then(() => {
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(() => {
94
+ sendResponse(res, next, baseControllerParameter.serviceResponse);
95
+ }).catch((err: Error) => {
96
+ baseControllerParameter.serviceResponse.isSuccess = false;
97
+ next(err);
98
+ });
99
+ }
100
+ });
70
101
  } catch (err) {
71
102
  next(err);
72
103
  }
104
+
73
105
  logger.profile(`${req.originalUrl}`);
74
106
  }
75
107
 
76
108
  public async executeGrpc(call: any, callback: any): Promise<any> {
77
- this.serviceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
109
+ let baseControllerParameter: BaseControllerParameter = new BaseControllerParameter();
110
+ baseControllerParameter.serviceResponse = { isSuccess: false, message: undefined, data: undefined, totalCount: undefined };
111
+
78
112
  try {
79
- if (commonAppConfig.SqlEnable) {
80
- BaseController.mutex.runExclusive(async () => {
81
- if (await this.startTransaction()) {
82
- const authorization: string = call.metadata.get(commonAppConst.httpHeader.AUTHORIZATION);
83
- this.userInfo = undefined;
84
- if (!!authorization && authorization.length) {
85
- const headerInfo: string[] = authorization[0].split(' ');
86
- if (headerInfo.length === 2 && headerInfo[0] === commonAppConst.httpHeader.BEARER) {
87
- this.userInfo = await verifyJWT(headerInfo[1]);
113
+ BaseController.mutex.runExclusive(async () => {
114
+ const authorization: string = call.metadata.get(commonAppConst.httpHeader.AUTHORIZATION);
115
+ if (!!authorization && authorization.length) {
116
+ const headerInfo: string[] = authorization[0].split(' ');
117
+ if (headerInfo.length === 2 && headerInfo[0] === commonAppConst.httpHeader.BEARER) {
118
+ verifyJWT (headerInfo[1]).then((userInfo: UserInfo) => {
119
+ baseControllerParameter.userInfo = userInfo;
120
+
121
+ if (commonAppConfig.SqlEnable) {
122
+ this.startTransaction().then((queryRunner: QueryRunner) => {
123
+ if (!!queryRunner) {
124
+ baseControllerParameter.queryRunner = queryRunner;
125
+
126
+ this.executeImpl(call.request, baseControllerParameter).then(() => {
127
+ this.endTransaction(baseControllerParameter).then(() => {
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(() => {
149
+ return callback(null, baseControllerParameter.serviceResponse);
150
+ }).catch((err: Error) => {
151
+ baseControllerParameter.serviceResponse.isSuccess = false;
152
+ baseControllerParameter.serviceResponse.error = true;
153
+ baseControllerParameter.serviceResponse.message = err.message;
154
+
155
+ throw err;
156
+ });
88
157
  }
89
- }
90
- await this.executeImpl(call.request);
91
- await this.endTransaction(!this.serviceResponse.isSuccess);
92
- } else {
93
- this.serviceResponse.message = 'Connect DB Fail!';
94
- this.serviceResponse.error = true;
158
+ });
95
159
  }
96
- return callback(null, this.serviceResponse);
97
- })
98
- } else {
99
- this.serviceResponse.message = 'SQL Disabled';
100
- this.serviceResponse.error = true;
101
- throw new Error(`SQL Disabled`);
102
- }
160
+ }
161
+ });
103
162
  } catch (err) {
104
163
  logger.error(`BaseController/executeGrpc: ${err}`);
105
- this.serviceResponse.isSuccess = false;
106
- this.serviceResponse.error = true;
107
- return callback(null, this.serviceResponse);
164
+ baseControllerParameter.serviceResponse.isSuccess = false;
165
+ baseControllerParameter.serviceResponse.error = true;
166
+ return callback(null, baseControllerParameter.serviceResponse);
108
167
  }
109
168
  }
110
169
  };