diff --git a/index.js b/index.js index 685ec80..f91184d 100644 --- a/index.js +++ b/index.js @@ -1,28 +1,26 @@ +const config = require('komodo-sdk/config'); + const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000; const DEFAULT_SLEEP_AFTER_CORE_ERROR_MS = 3000; -const MAX_REPORT_RETRY = 30; -const REPORT_RETRY_SLEEP_MS = 2000; + +const DEFAULT_CORE_AXIOS_CONFIG = { + headers: { 'Content-Type': 'application/json' }, + timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, +}; + +const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; const axios = require('axios').default; -const config = require('komodo-sdk/config'); const coreUrl = require('komodo-sdk/core-url'); const logger = require('tektrans-logger'); +const report = require('./lib/report'); const sleep = require('./lib/sleep'); let partner; let first = true; let coreIsHealthy = null; -// let skipNext = 0; - -const DEFAULT_CORE_AXIOS_CONFIG = { - headers: { 'Content-Type': 'application/json' }, - timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, -}; - -const GET_TASK_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; -const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; function detectCoreStatusChange(isHealthy, url, err) { if (coreIsHealthy !== isHealthy) { @@ -109,82 +107,4 @@ exports.setPartner = (val) => { getTaskLooper(); }; -exports.report = async (data, xid, retry) => { - if (!data.trx_id) { - logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. No trx id in report. MARK-3A37B7CA', { xid, data }); - return; - } - - if (!data.rc || typeof data.rc !== 'string') { - logger.warn('POSTPAID2-SDK: INVALID DATA TO REPORT. Rc is not valid. MARK-41ED74FC', { - xid, trxId: data.trx_id, rc: data.rc, typeofRc: typeof data.rc, - }); - } - - const dataToReport = { - handler: config.handler || config.handler_name, - command: data.command, - trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0), - rc: data.rc, - sn: data.sn, - amount: data.amount, - base_bill_amount: data.base_bill_amount, - bill_count: data.bill_count, - message: data.message, - info: data.info, - detail: data.detail, - data: data.data, - struk: (typeof data.struk === 'string' && data.struk) - || (data.struk && JSON.stringify(data.struk)) - || null, - }; - - const coreEndpoint = `${coreUrl}/postpaid2/report`; - - logger.verbose('POSTPAID2-SDK: Going to report to CORE', { - xid, - trxId: data.trx_id, - rc: data.rc, - sn: data.sn, - amount: data.amount, - baseBillAmount: data.base_bill_amount, - coreEndpoint, - }); - - try { - const reportResult = await axios.post( - coreEndpoint, - JSON.stringify(dataToReport), - REPORT_AXIOS_CONFIG, - ); - - if (!reportResult) { - logger.warn('POSTPAID2-SDK: unknown result from CORE on reporting trx result. MARK-795C53DB'); - } - } catch (e) { - const newRetry = (retry || 0) + 1; - logger.warn('POSTPAID2-SDK: Exception on reporting trx result to CORE. MARK-E7F000D8', { - xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY, - }); - - if (!this || !this.report) { - logger.warn('POSTPAID2-SDK: Can not retry report because of unkown this.report!'); - return; - } - - if (newRetry < MAX_REPORT_RETRY) { - setTimeout(() => { - logger.info('POSTPAID2-SDK: Retrying to send report to CORE', { - xid, - trxId: data.trx_id, - rc: data.rc, - sn: data.sn, - amount: data.amount, - baseBillAmount: data.base_bill_amount, - }); - - this.report(data, xid, newRetry); - }, REPORT_RETRY_SLEEP_MS); - } - } -}; +exports.report = report; diff --git a/lib/report.js b/lib/report.js new file mode 100644 index 0000000..71f3b9f --- /dev/null +++ b/lib/report.js @@ -0,0 +1,97 @@ +const MODULE_NAME = 'POSTPAID-SDK.REPORT'; + +const config = require('komodo-sdk/config'); + +const DEFAULT_CORE_REQUEST_TIMEOUT = 15 * 1000; +const MAX_REPORT_RETRY = 30; +const REPORT_RETRY_SLEEP_MS = 2000; + +const DEFAULT_CORE_AXIOS_CONFIG = { + headers: { 'Content-Type': 'application/json' }, + timeout: config.core_request_timeout || config.request_timeout || DEFAULT_CORE_REQUEST_TIMEOUT, +}; + +const REPORT_AXIOS_CONFIG = DEFAULT_CORE_AXIOS_CONFIG; + +const axios = require('axios').default; +const logger = require('tektrans-logger'); +const coreUrl = require('komodo-sdk/core-url'); + +const report = async (data, xid, retry) => { + if (!data.trx_id) { + logger.warn(`${MODULE_NAME} 3A37B7CA: INVALID DATA TO REPORT. No trx id in report`, { xid, data }); + return; + } + + if (!data.rc) { + logger.warn(`${MODULE_NAME} 41ED74FC: INVALID DATA TO REPORT. Rc is not valid`, { + xid, trxId: data.trx_id, rc: data.rc, + }); + } + + const dataToReport = { + handler: config.handler || config.handler_name, + command: data.command, + trx_id: Number(data.trx_id) - (config.sdk_trx_id_adder || 0), + rc: data.rc.toString(), + sn: data.sn, + amount: data.amount, + base_bill_amount: data.base_bill_amount, + bill_count: data.bill_count, + message: data.message, + info: data.info, + detail: data.detail, + data: data.data, + struk: (typeof data.struk === 'string' && data.struk) + || (data.struk && JSON.stringify(data.struk)) + || null, + }; + + const coreEndpoint = `${coreUrl}/postpaid2/report`; + + logger.verbose(`${MODULE_NAME} 91CF6849: Going to report to CORE`, { + xid, + trxId: data.trx_id, + rc: data.rc, + sn: data.sn, + amount: data.amount, + baseBillAmount: data.base_bill_amount, + coreEndpoint, + }); + + try { + const reportResult = await axios.post( + coreEndpoint, + JSON.stringify(dataToReport), + REPORT_AXIOS_CONFIG, + ); + + if (!reportResult) { + logger.warn(`${MODULE_NAME} 795C53DB: unknown result from CORE on reporting trx result`, { + xid, + }); + } + } catch (e) { + const newRetry = (retry || 0) + 1; + logger.warn(`${MODULE_NAME} E7F000D8: Exception on reporting trx result to CORE`, { + xid, err: e.message, retry: newRetry, maxRetry: MAX_REPORT_RETRY, + }); + + if (newRetry < MAX_REPORT_RETRY) { + setTimeout(() => { + logger.info(`${MODULE_NAME} 69CA1A73: Retrying to send report to CORE`, { + xid, + trxId: data.trx_id, + rc: data.rc, + sn: data.sn, + amount: data.amount, + baseBillAmount: data.base_bill_amount, + }); + + report(data, xid, newRetry); + }, REPORT_RETRY_SLEEP_MS); + } + } +}; + +module.exports = report;