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;