Commit 2a576aa1b0eed7ed97297eda7ae95526cd080f25
1 parent
c6a955c4bd
Exists in
master
st24.extractBalanceFromMsg
Showing 4 changed files with 44 additions and 1 deletions Side-by-side Diff
lib/partner.js
... | ... | @@ -149,6 +149,7 @@ function _topUpInquiry(task) { |
149 | 149 | message: stringify(value), |
150 | 150 | sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE, config.sn_pattern), |
151 | 151 | amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE, config.amount_pattern), |
152 | + balance: st24.extractBalanceFromMsg(value.MESSAGE, config.balance_pattern), | |
152 | 153 | raw: value, |
153 | 154 | misc: { |
154 | 155 | task: task |
... | ... | @@ -205,7 +206,7 @@ function report(data) { |
205 | 206 | return; |
206 | 207 | } |
207 | 208 | |
208 | - const task = data.misc.task; | |
209 | + const task = data.misc.task; | |
209 | 210 | logger.verbose('Registering resend delay', {trx_id: task.trx_id, destination: task.destination, product: task.product}) |
210 | 211 | resendDelay.register(task, advice); |
211 | 212 |
lib/reverse-report.js
... | ... | @@ -40,6 +40,7 @@ function create() { |
40 | 40 | message: value.MESSAGE, |
41 | 41 | sn: (value.SN || '').replace(/;$/, '') || st24.extractSnFromMessage(value.MESSAGE), |
42 | 42 | amount: value.PRICE || st24.extractPriceFromMsg(value.MESSAGE), |
43 | + balance: st24.extractBalanceFromMsg(value.MESSAGE, config.balance_pattern), | |
43 | 44 | raw: value, |
44 | 45 | misc: { |
45 | 46 | } |
lib/st24.js
... | ... | @@ -44,6 +44,19 @@ function extractPriceFromMsg(msg, custom_rule) { |
44 | 44 | return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); |
45 | 45 | } |
46 | 46 | |
47 | +function extractBalanceFromMsg(msg, custom_rule) { | |
48 | + const default_pattern = "SAL=([\\d\\.]+)"; | |
49 | + const default_match_idx = 1; | |
50 | + | |
51 | + let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); | |
52 | + if (!balance || typeof balance !== 'string') { | |
53 | + return; | |
54 | + } | |
55 | + | |
56 | + return Number(balance.replace(/[^\d]/g, '')); | |
57 | +} | |
58 | + | |
47 | 59 | |
48 | 60 | exports.extractSnFromMessage = extractSnFromMessage; |
49 | 61 | exports.extractPriceFromMsg = extractPriceFromMsg; |
62 | +exports.extractBalanceFromMsg = extractBalanceFromMsg; |
test.js
... | ... | @@ -35,4 +35,32 @@ describe('#st24', function() { |
35 | 35 | }) |
36 | 36 | }) |
37 | 37 | |
38 | + describe('#extractBalanceFromMsg', function() { | |
39 | + describe('using native ST24 response', function() { | |
40 | + it('should return correct balance', function() { | |
41 | + st24.extractBalanceFromMsg('SN=0516150344145563101; 16/05/18 15:03 ISI TR5 KE 0895350249796, SUKSES.SAL=426.078,HRG=5.250,ID=47285513,SN=0516150344145563101; ..trx lancar').should.equal(426078); | |
42 | + st24.extractBalanceFromMsg('15/05/18 17:19 ISI SAN10 KE 08535686667, NOMOR YANG ANDA MASUKKAN SALAH, MOHON TELITI KEMBALI..SAL=1.144.578,ID=47250459, ..trx lancar').should.equal(1144578) | |
43 | + }) | |
44 | + | |
45 | + it('should return null if there is no balance info', function() { | |
46 | + should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL')); | |
47 | + }) | |
48 | + }) | |
49 | + | |
50 | + describe('using custom rule', function() { | |
51 | + const custom_rule = { | |
52 | + pattern: "SALDO=(\\d+)", | |
53 | + match_idx: 1 | |
54 | + } | |
55 | + | |
56 | + it('should return correct balance', function() { | |
57 | + st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920); | |
58 | + }) | |
59 | + | |
60 | + it('should return null if there is no balance info', function() { | |
61 | + should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule)) | |
62 | + }) | |
63 | + }) | |
64 | + }) | |
65 | + | |
38 | 66 | }) |