index.js
3.94 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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
"use strict";
const rcFromMsg = require('komodo-sdk/rc-from-msg');
const organicRc = require('./rc');
function isInquiryResponseMessage(msg) {
return (msg || '').indexOf('CEK TAGIHAN') >= 0;
}
function isPaymentResponseMessage(msg) {
return (msg || '').indexOf('BAYAR TAGIHAN') >= 0;
}
function isPostpaidResponseMessage(msg) {
return isInquiryResponseMessage(msg) || isPaymentResponseMessage(msg);
}
function getRcFromMessage(msg, customRc) {
let rc;
if (customRc && Array.isArray(customRc) && customRc.length) {
rc = rcFromMsg(msg, customRc);
}
if (!rc) {
rc = rcFromMsg(msg, organicRc);
}
return rc;
}
function getPriceFromMessage(msg, rule) {
if (typeof msg !== 'string') {
return;
}
if (process.env.DEBUG_IRS && !rule) {
console.log('** IRS.getPriceFromMessage no rule'); // eslint-disable-line no-console
}
if (process.env.DEBUG_IRS && rule) {
console.log('** IRS.getPriceFromMessage rule: ' + JSON.stringify(rule, null, 2)); // eslint-disable-line no-console
}
//const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "Harga: ([\\d\\.]+?) ";
const pattern = (rule && typeof rule.pattern === 'string') ? rule.pattern : "\\d+\\s+-\\s+([\\d,\\.]+)\\s+=";
const match_idx = (rule && typeof rule.match_idx === 'number') ? rule.match_idx : 1;
const re = new RegExp(pattern);
const matches = msg.match(re);
if (process.env.DEBUG_IRS) {
console.log('** IRS.getPriceFromMessage msg: "' + msg + '" active_pattern: "' + pattern + '" active_match_idx: ' + match_idx); // eslint-disable-line no-console
console.log('** IRS.getPriceFromMessage matches:\n' + JSON.stringify(matches)); // eslint-disable-line no-console
}
if (matches && matches[match_idx]) {
const result = Number(matches[match_idx].replace(/\./g, ''));
if (process.env.DEBUG_IRS) {
console.log('** IRS.getPriceFromMessage SUPPLIER-PRICE: ' + result); // eslint-disable-line no-console
}
return result;
}
}
function extractFromMessage(msg, rule) {
if (typeof msg !== 'string') { return; }
if (!rule) { return; }
if (typeof rule !== 'object') {
return;
}
rule.match_idx = Number(rule.match_idx);
if (!rule.match_idx) {
rule.match_idx = 1;
}
const re = new RegExp(rule.pattern);
const matches = msg.match(re);
if (matches && matches[rule.match_idx] && typeof matches[rule.match_idx] === 'string') {
return matches[rule.match_idx];
}
}
function getSnFromMessage(msg, rule) {
if (!rule) {
rule = {
pattern: "[: ]S/*N:\\s*(.+?)\\s",
match_idx: 1
}
}
let sn = extractFromMessage(msg, rule);
if (!sn || typeof sn !== 'string') { return; }
return sn.toUpperCase().replace(/[^a-zA-Z0-9:,/]/g, '-').replace(/-+/g, '-').replace(/-*\/-*/g, '/').replace(/^-+/, '').replace(/-+$/, '');
}
function getBalanceFromMessage(msg, rule) {
if (!rule) {
rule = {
pattern: "Sisa Saldo: .+? = ([\\d\\.]+) ",
match_idx: 1
}
}
let result = extractFromMessage(msg, rule);
if (!result || typeof result !== 'string') { return; }
return Number(result.replace(/\./g, ''));
}
function getDetailFromMessage(msg, rule) {
if (!rule) {
rule = {
pattern: " Detail:\\s*(.+?)\\s+Sisa [Ss]aldo",
match_idx: 1,
}
}
let result = extractFromMessage(msg, rule);
return (result || '').trim();
}
exports.getRcFromMessage = getRcFromMessage;
exports.getPriceFromMessage = getPriceFromMessage;
exports.getSnFromMessage = getSnFromMessage;
exports.getBalanceFromMessage = getBalanceFromMessage;
exports.isInquiryResponseMessage = isInquiryResponseMessage;
exports.isPaymentResponseMessage = isPaymentResponseMessage;
exports.isPostpaidResponseMessage = isPostpaidResponseMessage;
exports.getDetailFromMessage = getDetailFromMessage;