report.js
2.96 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
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;