Commit 7c703aa6e6ded8a096fc1dd30b78aef2a41d195b
1 parent
f43c1230d8
Exists in
master
Move report.js to separate file
Showing 2 changed files with 108 additions and 91 deletions Side-by-side Diff
index.js
1 | +const config = require('komodo-sdk/config'); | |
2 | + | |
1 | 3 | const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000; |
2 | 4 | const DEFAULT_SLEEP_AFTER_CORE_ERROR_MS = 3000; |
3 | -const MAX_REPORT_RETRY = 30; | |
4 | -const REPORT_RETRY_SLEEP_MS = 2000; | |
5 | + | |
6 | +const DEFAULT_CORE_AXIOS_CONFIG = { | |
7 | + headers: { 'Content-Type': 'application/json' }, | |
8 | + timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, | |
9 | +}; | |
10 | + | |
11 | +const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; | |
5 | 12 | |
6 | 13 | const axios = require('axios').default; |
7 | 14 | |
8 | -const config = require('komodo-sdk/config'); | |
9 | 15 | const coreUrl = require('komodo-sdk/core-url'); |
10 | 16 | const logger = require('tektrans-logger'); |
17 | +const report = require('./lib/report'); | |
11 | 18 | |
12 | 19 | const sleep = require('./lib/sleep'); |
13 | 20 | |
14 | 21 | let partner; |
15 | 22 | let first = true; |
16 | 23 | let coreIsHealthy = null; |
17 | -// let skipNext = 0; | |
18 | - | |
19 | -const DEFAULT_CORE_AXIOS_CONFIG = { | |
20 | - headers: { 'Content-Type': 'application/json' }, | |
21 | - timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, | |
22 | -}; | |
23 | - | |
24 | -const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; | |
25 | -const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; | |
26 | 24 | |
27 | 25 | function detectCoreStatusChange(isHealthy, url, err) { |
28 | 26 | if (coreIsHealthy !== isHealthy) { |
... | ... | @@ -109,82 +107,4 @@ exports.setPartner = (val) => { |
109 | 107 | getTaskLooper(); |
110 | 108 | }; |
111 | 109 | |
112 | -exports.report = async (data, xid, retry) => { | |
113 | - if (!data.trx_id) { | |
114 | - logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data }); | |
115 | - return; | |
116 | - } | |
117 | - | |
118 | - if (!data.rc || typeof data.rc !== 'string') { | |
119 | - logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', { | |
120 | - xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc, | |
121 | - }); | |
122 | - } | |
123 | - | |
124 | - const dataToReport = { | |
125 | - handler: config.handler || config.handler_name, | |
126 | - command: data.command, | |
127 | - trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0), | |
128 | - rc: data.rc, | |
129 | - sn: data.sn, | |
130 | - amount: data.amount, | |
131 | - base_bill_amount: data.base_bill_amount, | |
132 | - bill_count: data.bill_count, | |
133 | - message: data.message, | |
134 | - info: data.info, | |
135 | - detail: data.detail, | |
136 | - data: data.data, | |
137 | - struk: (typeof data.struk === 'string' && data.struk) | |
138 | - || (data.struk && JSON.stringify(data.struk)) | |
139 | - || null, | |
140 | - }; | |
141 | - | |
142 | - const coreEndpoint = `${coreUrl}/postpaid2/report`; | |
143 | - | |
144 | - logger.verbose('POSTPAID2-SDK: Going to report to CORE', { | |
145 | - xid, | |
146 | - trxId: data.trx_id, | |
147 | - rc: data.rc, | |
148 | - sn: data.sn, | |
149 | - amount: data.amount, | |
150 | - baseBillAmount: data.base_bill_amount, | |
151 | - coreEndpoint, | |
152 | - }); | |
153 | - | |
154 | - try { | |
155 | - const reportResult = await axios.post( | |
156 | - coreEndpoint, | |
157 | - JSON.stringify(dataToReport), | |
158 | - REPORT_AXIOS_CONFIG, | |
159 | - ); | |
160 | - | |
161 | - if (!reportResult) { | |
162 | - logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB'); | |
163 | - } | |
164 | - } catch (e) { | |
165 | - const newRetry = (retry || 0) + 1; | |
166 | - logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', { | |
167 | - xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY, | |
168 | - }); | |
169 | - | |
170 | - if (!this || !this.report) { | |
171 | - logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!'); | |
172 | - return; | |
173 | - } | |
174 | - | |
175 | - if (newRetry < MAX_REPORT_RETRY) { | |
176 | - setTimeout(() => { | |
177 | - logger.info('POSTPAID2-SDK: Retrying to send report to CORE', { | |
178 | - xid, | |
179 | - trxId: data.trx_id, | |
180 | - rc: data.rc, | |
181 | - sn: data.sn, | |
182 | - amount: data.amount, | |
183 | - baseBillAmount: data.base_bill_amount, | |
184 | - }); | |
185 | - | |
186 | - this.report(data, xid, newRetry); | |
187 | - }, REPORT_RETRY_SLEEP_MS); | |
188 | - } | |
189 | - } | |
190 | -}; | |
110 | +exports.report = report; |
lib/report.js
... | ... | @@ -0,0 +1,97 @@ |
1 | +const MODULE_NAME = 'POSTPAID-SDK.REPORT'; | |
2 | + | |
3 | +const config = require('komodo-sdk/config'); | |
4 | + | |
5 | +const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000; | |
6 | +const MAX_REPORT_RETRY = 30; | |
7 | +const REPORT_RETRY_SLEEP_MS = 2000; | |
8 | + | |
9 | +const DEFAULT_CORE_AXIOS_CONFIG = { | |
10 | + headers: { 'Content-Type': 'application/json' }, | |
11 | + timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, | |
12 | +}; | |
13 | + | |
14 | +const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; | |
15 | + | |
16 | +const axios = require('axios').default; | |
17 | +const logger = require('tektrans-logger'); | |
18 | +const coreUrl = require('komodo-sdk/core-url'); | |
19 | + | |
20 | +const report = async (data, xid, retry) => { | |
21 | + if (!data.trx_id) { | |
22 | + logger.warn(`${MODULE_NAME} 3A37B7CA: INVALID DATA TO REPORT. No trx id in report`, { xid, data }); | |
23 | + return; | |
24 | + } | |
25 | + | |
26 | + if (!data.rc) { | |
27 | + logger.warn(`${MODULE_NAME} 41ED74FC: INVALID DATA TO REPORT. Rc is not valid`, { | |
28 | + xid, trxId: data.trx_id, rc: data.rc, | |
29 | + }); | |
30 | + } | |
31 | + | |
32 | + const dataToReport = { | |
33 | + handler: config.handler || config.handler_name, | |
34 | + command: data.command, | |
35 | + trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0), | |
36 | + rc: data.rc.toString(), | |
37 | + sn: data.sn, | |
38 | + amount: data.amount, | |
39 | + base_bill_amount: data.base_bill_amount, | |
40 | + bill_count: data.bill_count, | |
41 | + message: data.message, | |
42 | + info: data.info, | |
43 | + detail: data.detail, | |
44 | + data: data.data, | |
45 | + struk: (typeof data.struk === 'string' && data.struk) | |
46 | + || (data.struk && JSON.stringify(data.struk)) | |
47 | + || null, | |
48 | + }; | |
49 | + | |
50 | + const coreEndpoint = `${coreUrl}/postpaid2/report`; | |
51 | + | |
52 | + logger.verbose(`${MODULE_NAME} 91CF6849: Going to report to CORE`, { | |
53 | + xid, | |
54 | + trxId: data.trx_id, | |
55 | + rc: data.rc, | |
56 | + sn: data.sn, | |
57 | + amount: data.amount, | |
58 | + baseBillAmount: data.base_bill_amount, | |
59 | + coreEndpoint, | |
60 | + }); | |
61 | + | |
62 | + try { | |
63 | + const reportResult = await axios.post( | |
64 | + coreEndpoint, | |
65 | + JSON.stringify(dataToReport), | |
66 | + REPORT_AXIOS_CONFIG, | |
67 | + ); | |
68 | + | |
69 | + if (!reportResult) { | |
70 | + logger.warn(`${MODULE_NAME} 795C53DB: unknown result from CORE on reporting trx result`, { | |
71 | + xid, | |
72 | + }); | |
73 | + } | |
74 | + } catch (e) { | |
75 | + const newRetry = (retry || 0) + 1; | |
76 | + logger.warn(`${MODULE_NAME} E7F000D8: Exception on reporting trx result to CORE`, { | |
77 | + xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY, | |
78 | + }); | |
79 | + | |
80 | + if (newRetry < MAX_REPORT_RETRY) { | |
81 | + setTimeout(() => { | |
82 | + logger.info(`${MODULE_NAME} 69CA1A73: Retrying to send report to CORE`, { | |
83 | + xid, | |
84 | + trxId: data.trx_id, | |
85 | + rc: data.rc, | |
86 | + sn: data.sn, | |
87 | + amount: data.amount, | |
88 | + baseBillAmount: data.base_bill_amount, | |
89 | + }); | |
90 | + | |
91 | + report(data, xid, newRetry); | |
92 | + }, REPORT_RETRY_SLEEP_MS); | |
93 | + } | |
94 | + } | |
95 | +}; | |
96 | + | |
97 | +module.exports = report; |