Commit 33be871cbad02e702367eaf71d9f1630771f20eb

Authored by Adhidarma Hadiwinoto
1 parent e909a6a55b
Exists in master

Fix eslint on index.js

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

1 const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000; 1 const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 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('tektrans-logger'); 10 const logger = require('tektrans-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,
63 payload,
64 GET_TASK_AXIOS_CONFIG,
63 ); 65 );
64 66
65 if (!result || !result.data) throw new Error('POSTPAID2-SDK: Empty CORE response on pulling task. MARK-26F332C6'); 67 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'); 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');
67 69
68 detectCoreStatusChange(true, coreEndpoint); 70 detectCoreStatusChange(true, coreEndpoint);
69 task = result.data.task; 71 task = result.data.task;
70 } catch (e) { 72 } catch (e) {
71 // skipNext = 3; 73 // skipNext = 3;
72 detectCoreStatusChange(false, coreEndpoint, e); 74 detectCoreStatusChange(false, coreEndpoint, e);
73 await sleep(DEFAULT_SLEEP_AFTER_CORE_ERROR_MS); 75 await sleep(DEFAULT_SLEEP_AFTER_CORE_ERROR_MS);
74 } 76 }
75 77
76 if (!task) return null; 78 if (!task) return null;
77 79
78 task.remote_product = ((config.remote_products || {})[task.product]) || task.product; 80 task.remote_product = ((config.remote_products || {})[task.product]) || task.product;
79 task.trx_id = Number(task.trx_id) + (config.sdk_trx_id_adder || 0); 81 task.trx_id = Number(task.trx_id) + (config.sdk_trx_id_adder || 0);
80 82
81 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task }); 83 logger.verbose('POSTPAID2-SDK: Got task from CORE on pulling task', { task });
82 84
83 if (task.inquiry_only) { 85 if (task.inquiry_only) {
84 if (typeof partner.inquiry === 'function') partner.inquiry(task); 86 if (typeof partner.inquiry === 'function') partner.inquiry(task);
85 return task; 87 return task;
86 } 88 }
87 89
88 if (typeof partner.pay === 'function') { 90 if (typeof partner.pay === 'function') {
89 partner.pay(task); 91 partner.pay(task);
90 return task; 92 return task;
91 } 93 }
92 94
93 return null; 95 return null;
94 } 96 }
95 97
96 const getTaskLooper = async () => { 98 const getTaskLooper = async () => {
97 await getTask(); 99 await getTask();
98 100
99 setTimeout(() => { 101 setTimeout(() => {
100 getTaskLooper(); 102 getTaskLooper();
101 }, config.postpaid_pull_interval_ms || config.pull_interval_ms || 1000); 103 }, config.postpaid_pull_interval_ms || config.pull_interval_ms || 1000);
102 }; 104 };
103 105
104 exports.setPartner = (val) => { 106 exports.setPartner = (val) => {
105 partner = val; 107 partner = val;
106 // setInterval(getTaskLooper, config.pull_interval_ms || 1000); 108 // setInterval(getTaskLooper, config.pull_interval_ms || 1000);
107 getTaskLooper(); 109 getTaskLooper();
108 }; 110 };
109 111
110 exports.report = async (data, xid, retry) => { 112 exports.report = async (data, xid, retry) => {
111 if (!data.trx_id) { 113 if (!data.trx_id) {
112 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data }); 114 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data });
113 return; 115 return;
114 } 116 }
115 117
116 if (!data.rc || typeof data.rc !== 'string') { 118 if (!data.rc || typeof data.rc !== 'string') {
117 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', { 119 logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', {
118 xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc, 120 xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc,
119 }); 121 });
120 } 122 }
121 123
122 const dataToReport = { 124 const dataToReport = {
123 handler: config.handler || config.handler_name, 125 handler: config.handler || config.handler_name,
124 command: data.command, 126 command: data.command,
125 trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0), 127 trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0),
126 rc: data.rc, 128 rc: data.rc,
127 sn: data.sn, 129 sn: data.sn,
128 amount: data.amount, 130 amount: data.amount,
129 base_bill_amount: data.base_bill_amount, 131 base_bill_amount: data.base_bill_amount,
130 bill_count: data.bill_count, 132 bill_count: data.bill_count,
131 message: data.message, 133 message: data.message,
132 info: data.info, 134 info: data.info,
133 detail: data.detail, 135 detail: data.detail,
134 data: data.data, 136 data: data.data,
135 struk: (typeof data.struk === 'string' && data.struk) 137 struk: (typeof data.struk === 'string' && data.struk)
136 || (data.struk && JSON.stringify(data.struk)) 138 || (data.struk && JSON.stringify(data.struk))
137 || null, 139 || null,
138 }; 140 };
139 141
140 const coreEndpoint = `${coreUrl}/postpaid2/report`; 142 const coreEndpoint = `${coreUrl}/postpaid2/report`;
141 143
142 logger.verbose('POSTPAID2-SDK: Going to report to CORE', { 144 logger.verbose('POSTPAID2-SDK: Going to report to CORE', {
143 xid, 145 xid,
144 trxId: data.trx_id, 146 trxId: data.trx_id,
145 rc: data.rc, 147 rc: data.rc,
146 sn: data.sn, 148 sn: data.sn,
147 amount: data.amount, 149 amount: data.amount,
148 baseBillAmount: data.base_bill_amount, 150 baseBillAmount: data.base_bill_amount,
149 coreEndpoint, 151 coreEndpoint,
150 }); 152 });
151 153
152 try { 154 try {
153 const reportResult = await axios.post( 155 const reportResult = await axios.post(
154 coreEndpoint, JSON.stringify(dataToReport), REPORT_AXIOS_CONFIG, 156 coreEndpoint,
157 JSON.stringify(dataToReport),
158 REPORT_AXIOS_CONFIG,
155 ); 159 );
156 160
157 if (!reportResult) { 161 if (!reportResult) {
158 logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB'); 162 logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB');
159 } 163 }
160 } catch (e) { 164 } catch (e) {
161 const newRetry = (retry || 0) + 1; 165 const newRetry = (retry || 0) + 1;
162 logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', { 166 logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', {
163 xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY, 167 xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY,
164 }); 168 });
165 169
166 if (!this || !this.report) { 170 if (!this || !this.report) {
167 logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!'); 171 logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!');
168 return; 172 return;
169 } 173 }
170 174
171 if (newRetry < MAX_REPORT_RETRY) { 175 if (newRetry < MAX_REPORT_RETRY) {
172 setTimeout(() => { 176 setTimeout(() => {
173 logger.info('POSTPAID2-SDK: Retrying to send report to CORE', { 177 logger.info('POSTPAID2-SDK: Retrying to send report to CORE', {
174 xid, 178 xid,
175 trxId: data.trx_id, 179 trxId: data.trx_id,
176 rc: data.rc, 180 rc: data.rc,
177 sn: data.sn, 181 sn: data.sn,
178 amount: data.amount, 182 amount: data.amount,
179 baseBillAmount: data.base_bill_amount, 183 baseBillAmount: data.base_bill_amount,
180 }); 184 });
181 185
182 this.report(data, xid, newRetry); 186 this.report(data, xid, newRetry);
183 }, REPORT_RETRY_SLEEP_MS); 187 }, REPORT_RETRY_SLEEP_MS);
184 } 188 }
185 } 189 }
186 }; 190 };
187 191