Commit 7c703aa6e6ded8a096fc1dd30b78aef2a41d195b

Authored by Adhidarma Hadiwinoto
1 parent f43c1230d8
Exists in master

Move report.js to separate file

Showing 2 changed files with 108 additions and 91 deletions Inline Diff

1 const config = require('komodo-sdk/config');
2
1 const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000; 3 const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000;
2 const DEFAULT_SLEEP_AFTER_CORE_ERROR_MS = 3000; 4 const DEFAULT_SLEEP_AFTER_CORE_ERROR_MS = 3000;
3 const MAX_REPORT_RETRY = 30; 5
4 const REPORT_RETRY_SLEEP_MS = 2000; 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 const axios = require('axios').default; 13 const axios = require('axios').default;
7 14
8 const config = require('komodo-sdk/config');
9 const coreUrl = require('komodo-sdk/core-url'); 15 const coreUrl = require('komodo-sdk/core-url');
10 const logger = require('tektrans-logger'); 16 const logger = require('tektrans-logger');
17 const report = require('./lib/report');
11 18
12 const sleep = require('./lib/sleep'); 19 const sleep = require('./lib/sleep');
13 20
14 let partner; 21 let partner;
15 let first = true; 22 let first = true;
16 let coreIsHealthy = null; 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 function detectCoreStatusChange(isHealthy, url, err) { 25 function detectCoreStatusChange(isHealthy, url, err) {
28 if (coreIsHealthy !== isHealthy) { 26 if (coreIsHealthy !== isHealthy) {
29 coreIsHealthy = isHealthy; 27 coreIsHealthy = isHealthy;
30 28
31 if (isHealthy) { 29 if (isHealthy) {
32 logger.info('POSTPAID2-SDK: CORE is healthy now', { url, err: err && err.message }); 30 logger.info('POSTPAID2-SDK: CORE is healthy now', { url, err: err && err.message });
33 } else { 31 } else {
34 logger.warn('POSTPAID2-SDK: CORE is unhealthy now', { url, err: err && err.message }); 32 logger.warn('POSTPAID2-SDK: CORE is unhealthy now', { url, err: err && err.message });
35 } 33 }
36 } 34 }
37 } 35 }
38 36
39 async function getTask() { 37 async function getTask() {
40 if (!partner) return null; 38 if (!partner) return null;
41 39
42 // if (skipNext > 0) { 40 // if (skipNext > 0) {
43 // skipNext -= 1; 41 // skipNext -= 1;
44 // return null; 42 // return null;
45 // } 43 // }
46 44
47 if (first) { 45 if (first) {
48 first = false; 46 first = false;
49 logger.verbose('POSTPAID2-SDK: first pull request to CORE'); 47 logger.verbose('POSTPAID2-SDK: first pull request to CORE');
50 } 48 }
51 49
52 const coreEndpoint = `${coreUrl}/postpaid2/task`; 50 const coreEndpoint = `${coreUrl}/postpaid2/task`;
53 51
54 const payload = { 52 const payload = {
55 handler: config.handler || config.handler_name, 53 handler: config.handler || config.handler_name,
56 products: config.products || [], 54 products: config.products || [],
57 }; 55 };
58 56
59 let task; 57 let task;
60 try { 58 try {
61 const result = await axios.post( 59 const result = await axios.post(
62 coreEndpoint, 60 coreEndpoint,
63 payload, 61 payload,
64 GET_TASK_AXIOS_CONFIG, 62 GET_TASK_AXIOS_CONFIG,
65 ); 63 );
66 64
67 if (!result || !result.data) throw new Error('POSTPAID2-SDK: Empty CORE response on pulling task. MARK-26F332C6'); 65 if (!result || !result.data) throw new Error('POSTPAID2-SDK: Empty CORE response on pulling task. MARK-26F332C6');
68 if (result && result.data && result.data.task && !result.data.task.postpaid) throw new Error('POSTPAID2-SDK: CORE returning non postpaid task on pulling task. MARK-B338CEF8'); 66 if (result && result.data && result.data.task && !result.data.task.postpaid) throw new Error('POSTPAID2-SDK: CORE returning non postpaid task on pulling task. MARK-B338CEF8');
69 67
70 detectCoreStatusChange(true, coreEndpoint); 68 detectCoreStatusChange(true, coreEndpoint);
71 task = result.data.task; 69 task = result.data.task;
72 } catch (e) { 70 } catch (e) {
73 // skipNext = 3; 71 // skipNext = 3;
74 detectCoreStatusChange(false, coreEndpoint, e); 72 detectCoreStatusChange(false, coreEndpoint, e);
75 await sleep(DEFAULT_SLEEP_AFTER_CORE_ERROR_MS); 73 await sleep(DEFAULT_SLEEP_AFTER_CORE_ERROR_MS);
76 } 74 }
77 75
78 if (!task) return null; 76 if (!task) return null;
79 77
80 task.remote_product = ((config.remote_products || {})[task.product]) || task.product; 78 task.remote_product = ((config.remote_products || {})[task.product]) || task.product;
81 task.trx_id = Number(task.trx_id) + (config.sdk_trx_id_adder || 0); 79 task.trx_id = Number(task.trx_id) + (config.sdk_trx_id_adder || 0);
82 80
83 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task }); 81 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task });
84 82
85 if (task.inquiry_only) { 83 if (task.inquiry_only) {
86 if (typeof partner.inquiry === 'function') partner.inquiry(task); 84 if (typeof partner.inquiry === 'function') partner.inquiry(task);
87 return task; 85 return task;
88 } 86 }
89 87
90 if (typeof partner.pay === 'function') { 88 if (typeof partner.pay === 'function') {
91 partner.pay(task); 89 partner.pay(task);
92 return task; 90 return task;
93 } 91 }
94 92
95 return null; 93 return null;
96 } 94 }
97 95
98 const getTaskLooper = async () => { 96 const getTaskLooper = async () => {
99 await getTask(); 97 await getTask();
100 98
101 setTimeout(() => { 99 setTimeout(() => {
102 getTaskLooper(); 100 getTaskLooper();
103 }, config.postpaid_pull_interval_ms || config.pull_interval_ms || 1000); 101 }, config.postpaid_pull_interval_ms || config.pull_interval_ms || 1000);
104 }; 102 };
105 103
106 exports.setPartner = (val) => { 104 exports.setPartner = (val) => {
107 partner = val; 105 partner = val;
108 // setInterval(getTaskLooper, config.pull_interval_ms || 1000); 106 // setInterval(getTaskLooper, config.pull_interval_ms || 1000);
109 getTaskLooper(); 107 getTaskLooper();
110 }; 108 };
111 109
112 exports.report = async (data, xid, retry) => { 110 exports.report = report;
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,
File was created 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;
98