Commit 0606b9c1356080e0e8a77c585aba9b263ceb2070

Authored by Adhidarma Hadiwinoto
1 parent 522cfb9eed
Exists in master

config.sdk_trx_id_adder aware

Showing 1 changed file with 2 additions and 1 deletions Inline Diff

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