diff --git a/partner-scrappingkisel.js b/partner-scrappingkisel.js index c581b58..130971c 100644 --- a/partner-scrappingkisel.js +++ b/partner-scrappingkisel.js @@ -88,10 +88,12 @@ function topupRequest(task) { var ref_num = result.trx_response.ref_num.join(' ').trim(); var harga = result.trx_response.harga.join(' ').trim(); var kode_voucher = result.trx_response.kode_voucher.join(' ').trim(); + var stocks = result.trx_response.stock(' ').trim(); + var stock = parseStock(stocks, product); response_code = '00'; - message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher; + message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit'; } callbackReport(task['requestId'], response_code, message); @@ -104,5 +106,39 @@ function start(_config, _callbackReport) { callbackReport = _callbackReport } +function stockKeyword(product_desc) { + var keywords = product_desc.match(/\s\d+K$/); + if (!keywords) { + return; + } + + if (keywords.length < 1) { + return; + } + + return keywords[0].trim(); +} + +function parseStock(stocks, product_desc) { + product_keyword = stockKeyword(product_desc); + + var _stocks = stocks.split('; '); + count = _stocks.length; + for (var i = 0; i < count; i++) { + var stock = _stocks[i].split(' = '); + if (stock.length < 2) { + continue; + } + + if (stock[0] == product_keyword) { + return parseInt(stock[1]); + } + } + + return 0; +} + exports.start = start; exports.topupRequest = topupRequest; +exports.stockKeyword = stockKeyword; +exports.parseStock = parseStock; diff --git a/test.js b/test.js index 46a97e3..7a7d429 100644 --- a/test.js +++ b/test.js @@ -33,17 +33,58 @@ describe('aaa', function() { }); describe('aaa', function() { - var partner = require('./httppulsakita'); + var partner = require('./partner-scrappingkisel'); - describe("#parseResult()", function() { - message = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><respon><tanggal>2015/6/16 15:43:35</tanggal><idagen>P0039</idagen><refid>AEE15B32987941D89FFF4BC7EF676C13</refid><produk>PLN20</produk><tujuan>14204279369</tujuan><rc>0000</rc><data> </data><token> </token><pesan>#14836 PLN20 ke:14204279369 SUKSES. SN:3520-2887-6627-6699-4826/TestDummyPanjang6955555/P1/7000VA/32,4. \ -Sisa saldo Rp. 5,000,000 - Rp. 18,700 = Rp. 4,981,300</pesan></respon>'; - - data = partner.parseResult(message); - console.log(data); + describe("#stockKeyword", function() { + it('should return 10K', function() { + assert.equal('10K', partner.stockKeyword('Telkomsel 10K')); + }); + + it('should return 20K', function() { + assert.equal('20K', partner.stockKeyword('Telkomsel 20K')); + }); + + it('should return 25K', function() { + assert.equal('25K', partner.stockKeyword('Telkomsel 25K')); + }); + + it('should return 50K', function() { + assert.equal('50K', partner.stockKeyword('Telkomsel 50K')); + }); + + it('should return 100K', function() { + assert.equal('100K', partner.stockKeyword('Telkomsel 100K')); + }); + }); + + describe("#parseStock", function() { + it('should return 476', function() { + assert.equal(476, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', 'Telkomsel 10K')); + }); + + it('should return 598', function() { + assert.equal(598, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', 'Telkomsel 20K')); + }); + + it('should return 0', function() { + assert.equal(0, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', 'Telkomsel 200K')); + }); + + it('should return 0', function() { + assert.equal(0, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', 'Telkomsel 2000K')); + }); + + it('should return 0', function() { + assert.equal(0, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', 'Telkomsel 2000')); + }); - it('should return 2015/6/16 15:43:35', function() { - assert.equal('2015/6/16 15:43:35', data.respon.tanggal); + it('should return 0', function() { + assert.equal(0, partner.parseStock('', 'Telkomsel 2000')); }); + + it('should return 0', function() { + assert.equal(0, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', '')); + }); + }); });