Commit c52ed4be8a7085efc9d0d69c5abaf7d4aa930683

Authored by Adhidarma Hadiwinoto
1 parent f1e47204c1
Exists in master

Adaptasi metro-h2h

Showing 2 changed files with 5 additions and 3 deletions Inline Diff

1 "use strict"; 1 "use strict";
2 2
3 function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) { 3 function extractFromMessage(msg, default_pattern, default_match_idx, custom_rule) {
4 if (!msg || typeof msg !== 'string') { 4 if (!msg || typeof msg !== 'string') {
5 return; 5 return;
6 } 6 }
7 7
8 let pattern; 8 let pattern;
9 let match_idx; 9 let match_idx;
10 10
11 if (custom_rule && custom_rule.pattern) { 11 if (custom_rule && custom_rule.pattern) {
12 pattern = custom_rule.pattern; 12 pattern = custom_rule.pattern;
13 match_idx = custom_rule.match_idx; 13 match_idx = custom_rule.match_idx;
14 } 14 }
15 else { 15 else {
16 pattern = default_pattern; 16 pattern = default_pattern;
17 match_idx = default_match_idx; 17 match_idx = default_match_idx;
18 } 18 }
19 19
20 const re = new RegExp(pattern); 20 const re = new RegExp(pattern);
21 const matches = msg.match(re); 21 const matches = msg.match(re);
22 22
23 if (!matches) return; 23 if (!matches) return;
24 24
25 if (match_idx < matches.length) { 25 if (match_idx < matches.length) {
26 return matches[match_idx] || null; 26 return matches[match_idx] || null;
27 } else { 27 } else {
28 return; 28 return;
29 } 29 }
30 30
31 } 31 }
32 32
33 function extractSnFromMessage(msg, custom_rule) { 33 function extractSnFromMessage(msg, custom_rule) {
34 const default_pattern = "SN=(.*?);"; 34 const default_pattern = "SN=(.*?);";
35 const default_match_idx = 1; 35 const default_match_idx = 1;
36 36
37 return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 37 return extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
38 } 38 }
39 39
40 function extractPriceFromMsg(msg, custom_rule) { 40 function extractPriceFromMsg(msg, custom_rule) {
41 const default_pattern = "\\d,HRG=(.*?),ID="; 41 const default_pattern = "\\d,HRG= *(?:RP)*\\.* *(.*?),ID=";
42 const default_match_idx = 1; 42 const default_match_idx = 1;
43 43
44 let price = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 44 let price = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
45 price = (typeof price === 'string') ? Number(price.replace(/\./g, '')) : null; 45 price = (typeof price === 'string') ? Number(price.replace(/[^\d]/g, '')) : null;
46 return price; 46 return price;
47 } 47 }
48 48
49 function extractBalanceFromMsg(msg, custom_rule) { 49 function extractBalanceFromMsg(msg, custom_rule) {
50 const default_pattern = "SAL=([\\d\\.]+)"; 50 const default_pattern = 'SAL= *(?:RP)*\\.* *([\\d\\.,]+)';
51 const default_match_idx = 1; 51 const default_match_idx = 1;
52 52
53 let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule); 53 let balance = extractFromMessage(msg, default_pattern, default_match_idx, custom_rule);
54 if (!balance || typeof balance !== 'string') { 54 if (!balance || typeof balance !== 'string') {
55 return; 55 return;
56 } 56 }
57 57
58 return Number(balance.replace(/[^\d]/g, '')); 58 return Number(balance.replace(/[^\d]/g, ''));
59 } 59 }
60 60
61 61
62 exports.extractSnFromMessage = extractSnFromMessage; 62 exports.extractSnFromMessage = extractSnFromMessage;
63 exports.extractPriceFromMsg = extractPriceFromMsg; 63 exports.extractPriceFromMsg = extractPriceFromMsg;
64 exports.extractBalanceFromMsg = extractBalanceFromMsg; 64 exports.extractBalanceFromMsg = extractBalanceFromMsg;
65 65
1 "use strict"; 1 "use strict";
2 2
3 /* global describe it */ 3 /* global describe it */
4 4
5 const should = require('should'); 5 const should = require('should');
6 6
7 const st24 = require('./lib/st24'); 7 const st24 = require('./lib/st24');
8 8
9 describe('#st24', function() { 9 describe('#st24', function() {
10 10
11 describe('#extractSnFromMessage', function() { 11 describe('#extractSnFromMessage', function() {
12 12
13 describe('#using default st24 rule', function() { 13 describe('#using default st24 rule', function() {
14 it('should return correct sn from topUpRequest response', function() { 14 it('should return correct sn from topUpRequest response', function() {
15 st24.extractSnFromMessage('SN=0419165234155980102;19/04/18 16:52 ISI TR5 KE 0895621829255, SUKSES.SAL=1.323.934,HRG=5.250,ID=46398092,SN=0419165234155980102; ..trx lancar').should.equal('0419165234155980102'); 15 st24.extractSnFromMessage('SN=0419165234155980102;19/04/18 16:52 ISI TR5 KE 0895621829255, SUKSES.SAL=1.323.934,HRG=5.250,ID=46398092,SN=0419165234155980102; ..trx lancar').should.equal('0419165234155980102');
16 }) 16 })
17 17
18 it('should return correct sn from topUpInquiry response', function() { 18 it('should return correct sn from topUpInquiry response', function() {
19 st24.extractSnFromMessage('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal('0041002635369521'); 19 st24.extractSnFromMessage('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal('0041002635369521');
20 }) 20 })
21 }) 21 })
22 22
23 describe('#using custom rule', function() { 23 describe('#using custom rule', function() {
24 const custom_rule = { 24 const custom_rule = {
25 pattern: 'SN=(.*?)\\.', 25 pattern: 'SN=(.*?)\\.',
26 match_idx: 1 26 match_idx: 1
27 } 27 }
28 28
29 it('should return correct sn', function() { 29 it('should return correct sn', function() {
30 st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=0041002310111470.HRG=10035.SALDO=54489150', custom_rule).should.equal('0041002310111470'); 30 st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=0041002310111470.HRG=10035.SALDO=54489150', custom_rule).should.equal('0041002310111470');
31 31
32 }) 32 })
33 33
34 it('should return null on message not containing sn', function() { 34 it('should return null on message not containing sn', function() {
35 should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule)); 35 should.not.exists(st24.extractSnFromMessage('ISI Ke 081311084419 GAGAL.TRXID=20180403123020042979', custom_rule));
36 }) 36 })
37 37
38 it('should return null on message empty sn', function() { 38 it('should return null on message empty sn', function() {
39 should.not.exists(st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=.HRG=10035.SALDO=54489150', custom_rule)); 39 should.not.exists(st24.extractSnFromMessage('ISI Telkomsel 10 ke 085261208081 BERHASIL.SN=.HRG=10035.SALDO=54489150', custom_rule));
40 }) 40 })
41 }) 41 })
42 }) 42 })
43 43
44 describe('#extractBalanceFromMsg', function() { 44 describe('#extractBalanceFromMsg', function() {
45 describe('using native ST24 response', function() { 45 describe('using native ST24 response', function() {
46 it('should return correct balance', function() { 46 it('should return correct balance', function() {
47 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); 47 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);
48 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) 48 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)
49 st24.extractBalanceFromMsg('SN=3527-7416-4999-5485-7433/SARJUNI/R1/1300/31;11/05/20 17:24 TRANSAKSI PLNB50 KE 45019534903, (ISO00) BERHASIL. SAL=RP 4,729,975,HRG=RP 50,005,ID=62102710,SN=3527-7416-4999-5485-7433/SARJUNI/R1/1300/31@ *[Gaspolll_TanpaBatas] CS: @CSMETROH2H | 62 85810555505 . Channel : https://t.me/metroreloadinfoh2h').should.equal(4729975, 'metro');
49 }) 50 })
50 51
51 it('should return null if there is no balance info', function() { 52 it('should return null if there is no balance info', function() {
52 should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL')); 53 should.not.exists(st24.extractBalanceFromMsg('PENGECEKAN GAGAL'));
53 }) 54 })
54 }) 55 })
55 56
56 describe('using custom rule', function() { 57 describe('using custom rule', function() {
57 const custom_rule = { 58 const custom_rule = {
58 pattern: "SALDO=(\\d+)", 59 pattern: "SALDO=(\\d+)",
59 match_idx: 1 60 match_idx: 1
60 } 61 }
61 62
62 it('should return correct balance', function() { 63 it('should return correct balance', function() {
63 st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920); 64 st24.extractBalanceFromMsg('ISI Telkomsel 10 ke 082139822309 BERHASIL.SN=0041002442595407.HRG=10400.SALDO=104911920', custom_rule).should.equal(104911920);
64 }) 65 })
65 66
66 it('should return null if there is no balance info', function() { 67 it('should return null if there is no balance info', function() {
67 should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule)) 68 should.not.exists(st24.extractBalanceFromMsg('ISI Ke 08523548915 GAGAL.TRXID=20180516123010017371', custom_rule))
68 }) 69 })
69 }) 70 })
70 }) 71 })
71 72
72 describe('#extractPriceFromMsg', function() { 73 describe('#extractPriceFromMsg', function() {
73 describe('using native ST24 topUpRequest direct response', function() { 74 describe('using native ST24 topUpRequest direct response', function() {
74 it('should return correct price', function() { 75 it('should return correct price', function() {
75 st24.extractPriceFromMsg('SN=0041002635395450;;19/07/18 21:01 ISI SPT20 KE 08125100091, SUKSES. SAL=798.500,HRG=19.700,ID=48761075,SN=0041002635395450;; ..trx lancar').should.equal(19700); 76 st24.extractPriceFromMsg('SN=0041002635395450;;19/07/18 21:01 ISI SPT20 KE 08125100091, SUKSES. SAL=798.500,HRG=19.700,ID=48761075,SN=0041002635395450;; ..trx lancar').should.equal(19700);
76 st24.extractPriceFromMsg('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(5250); 77 st24.extractPriceFromMsg('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(5250);
78 st24.extractPriceFromMsg('SN=3527-7416-4999-5485-7433/SARJUNI/R1/1300/31;11/05/20 17:24 TRANSAKSI PLNB50 KE 45019534903, (ISO00) BERHASIL. SAL=RP 4,729,975,HRG=RP 50,005,ID=62102710,SN=3527-7416-4999-5485-7433/SARJUNI/R1/1300/31@ *[Gaspolll_TanpaBatas] CS: @CSMETROH2H | 62 85810555505 . Channel : https://t.me/metroreloadinfoh2h').should.equal(50005, 'metro');
77 }); 79 });
78 }) 80 })
79 81
80 describe('using native ST24 topUpInquiry response', function() { 82 describe('using native ST24 topUpInquiry response', function() {
81 it('should return correct price', function() { 83 it('should return correct price', function() {
82 st24.extractPriceFromMsg('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal(19700); 84 st24.extractPriceFromMsg('19/07/18 20:53 ISI SPT20 KE 081264858057, SUKSES.SAL=828.425,HRG=19.700,ID=48761021,SN=0041002635369521;').should.equal(19700);
83 }) 85 })
84 }) 86 })
85 }) 87 })
86 88
87 }) 89 })
88 90