Commit d03db4675bcd4bece0ca69898964657480429e9f

Authored by Adhidarma Hadiwinoto
1 parent bb387d2ec8
Exists in master

sisa stock

Showing 2 changed files with 87 additions and 10 deletions Inline Diff

partner-scrappingkisel.js
1 var fs = require('fs'); 1 var fs = require('fs');
2 var https = require('https'); 2 var https = require('https');
3 var http = require('http'); 3 var http = require('http');
4 var url = require('url'); 4 var url = require('url');
5 var request = require('request'); 5 var request = require('request');
6 var xml2js = require('xml2js').parseString; 6 var xml2js = require('xml2js').parseString;
7 var strftime = require('strftime'); 7 var strftime = require('strftime');
8 var math = require('mathjs'); 8 var math = require('mathjs');
9 9
10 var config; 10 var config;
11 var httpServer; 11 var httpServer;
12 12
13 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0'; 13 process.env.NODE_TLS_REJECT_UNAUTHORIZED = '0';
14 14
15 var logTag = __filename.split('/').reverse()[0]; 15 var logTag = __filename.split('/').reverse()[0];
16 16
17 function topupRequest(task) { 17 function topupRequest(task) {
18 if (config.globals.requests_count == undefined) { 18 if (config.globals.requests_count == undefined) {
19 config.globals.requests_count = 1; 19 config.globals.requests_count = 1;
20 } else { 20 } else {
21 config.globals.requests_count++; 21 config.globals.requests_count++;
22 } 22 }
23 23
24 if (config.globals.active_requests_count == undefined) { 24 if (config.globals.active_requests_count == undefined) {
25 config.globals.active_requests_count = 1; 25 config.globals.active_requests_count = 1;
26 } else { 26 } else {
27 config.globals.active_requests_count++; 27 config.globals.active_requests_count++;
28 } 28 }
29 29
30 if (config.globals.max_active_requests_count == undefined) { 30 if (config.globals.max_active_requests_count == undefined) {
31 config.globals.max_active_requests_count = config.globals.active_requests_count; 31 config.globals.max_active_requests_count = config.globals.active_requests_count;
32 } else { 32 } else {
33 config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count); 33 config.globals.max_active_requests_count = math.max(config.globals.max_active_requests_count, config.globals.active_requests_count);
34 } 34 }
35 35
36 var options = { 36 var options = {
37 url: config.h2h_out.partner, 37 url: config.h2h_out.partner,
38 qs: { 38 qs: {
39 reqid: task['requestId'], 39 reqid: task['requestId'],
40 msisdn: task['destination'], 40 msisdn: task['destination'],
41 product: task['remoteProduct'] 41 product: task['remoteProduct']
42 } 42 }
43 }; 43 };
44 console.log(options); 44 console.log(options);
45 45
46 request(options, function (error, response, body) { 46 request(options, function (error, response, body) {
47 if (config.globals.active_requests_count == undefined) { 47 if (config.globals.active_requests_count == undefined) {
48 config.globals.active_requests_count = 0; 48 config.globals.active_requests_count = 0;
49 } else { 49 } else {
50 config.globals.active_requests_count--; 50 config.globals.active_requests_count--;
51 } 51 }
52 52
53 if (error || response.statusCode != 200) { 53 if (error || response.statusCode != 200) {
54 console.log(logTag + ': Gateway Error'); 54 console.log(logTag + ': Gateway Error');
55 callbackReport(task['requestId'], '40', 'Gateway Error'); 55 callbackReport(task['requestId'], '40', 'Gateway Error');
56 return; 56 return;
57 } 57 }
58 58
59 console.log(logTag + ': Supplier response:'); 59 console.log(logTag + ': Supplier response:');
60 console.log(body); 60 console.log(body);
61 61
62 xml2js(body, function (err, result) { 62 xml2js(body, function (err, result) {
63 if (err) { 63 if (err) {
64 callbackReport(task['requestId'], '40', body); 64 callbackReport(task['requestId'], '40', body);
65 return; 65 return;
66 } 66 }
67 67
68 console.log(result); 68 console.log(result);
69 69
70 var response_code = '68'; 70 var response_code = '68';
71 var message = result.trx_response.info[0].trim(); 71 var message = result.trx_response.info[0].trim();
72 72
73 if (message == 'Error Parsing') { 73 if (message == 'Error Parsing') {
74 74
75 response_code = '40'; 75 response_code = '40';
76 76
77 } else if (message == "Phone number's not found") { 77 } else if (message == "Phone number's not found") {
78 78
79 response_code = '14'; 79 response_code = '14';
80 80
81 } else if (message == "Duplicate transaction") { 81 } else if (message == "Duplicate transaction") {
82 82
83 response_code = '55'; 83 response_code = '55';
84 84
85 } else if (message == "TRANSAKSI SUKSES !!!") { 85 } else if (message == "TRANSAKSI SUKSES !!!") {
86 var destination = result.trx_response.msisdn.join(' ').trim(); 86 var destination = result.trx_response.msisdn.join(' ').trim();
87 var product = result.trx_response.product.join(' ').trim(); 87 var product = result.trx_response.product.join(' ').trim();
88 var ref_num = result.trx_response.ref_num.join(' ').trim(); 88 var ref_num = result.trx_response.ref_num.join(' ').trim();
89 var harga = result.trx_response.harga.join(' ').trim(); 89 var harga = result.trx_response.harga.join(' ').trim();
90 var kode_voucher = result.trx_response.kode_voucher.join(' ').trim(); 90 var kode_voucher = result.trx_response.kode_voucher.join(' ').trim();
91 var stocks = result.trx_response.stock(' ').trim();
92 var stock = parseStock(stocks, product);
91 93
92 response_code = '00'; 94 response_code = '00';
93 95
94 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher; 96 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit';
95 } 97 }
96 98
97 callbackReport(task['requestId'], response_code, message); 99 callbackReport(task['requestId'], response_code, message);
98 }); 100 });
99 }); 101 });
100 } 102 }
101 103
102 function start(_config, _callbackReport) { 104 function start(_config, _callbackReport) {
103 config = _config; 105 config = _config;
104 callbackReport = _callbackReport 106 callbackReport = _callbackReport
105 } 107 }
106 108
109 function stockKeyword(product_desc) {
110 var keywords = product_desc.match(/\s\d+K$/);
111 if (!keywords) {
112 return;
113 }
114
115 if (keywords.length < 1) {
116 return;
117 }
118
119 return keywords[0].trim();
120 }
121
122 function parseStock(stocks, product_desc) {
123 product_keyword = stockKeyword(product_desc);
124
125 var _stocks = stocks.split('; ');
126 count = _stocks.length;
127 for (var i = 0; i < count; i++) {
128 var stock = _stocks[i].split(' = ');
129 if (stock.length < 2) {
130 continue;
131 }
132
133 if (stock[0] == product_keyword) {
134 return parseInt(stock[1]);
135 }
136 }
137
138 return 0;
139 }
140
107 exports.start = start; 141 exports.start = start;
108 exports.topupRequest = topupRequest; 142 exports.topupRequest = topupRequest;
143 exports.stockKeyword = stockKeyword;
144 exports.parseStock = parseStock;
109 145
1 var assert = require("assert"); 1 var assert = require("assert");
2 2
3 3
4 describe('aaa', function() { 4 describe('aaa', function() {
5 var aaa = require('./aaa'); 5 var aaa = require('./aaa');
6 6
7 describe("#unaliasResponseCode()", function() { 7 describe("#unaliasResponseCode()", function() {
8 it('should return 68', function() { 8 it('should return 68', function() {
9 assert.equal('68', aaa.unaliasResponseCode('01', '01:68')); 9 assert.equal('68', aaa.unaliasResponseCode('01', '01:68'));
10 }); 10 });
11 11
12 it('should return 68', function() { 12 it('should return 68', function() {
13 assert.equal('68', aaa.unaliasResponseCode('68', '01:68')); 13 assert.equal('68', aaa.unaliasResponseCode('68', '01:68'));
14 }); 14 });
15 15
16 it('should return 00', function() { 16 it('should return 00', function() {
17 assert.equal('00', aaa.unaliasResponseCode('00', '01:68')); 17 assert.equal('00', aaa.unaliasResponseCode('00', '01:68'));
18 }); 18 });
19 19
20 it('should return 40', function() { 20 it('should return 40', function() {
21 assert.equal('40', aaa.unaliasResponseCode('40', '')); 21 assert.equal('40', aaa.unaliasResponseCode('40', ''));
22 }); 22 });
23 23
24 it('should return 40', function() { 24 it('should return 40', function() {
25 assert.equal('40', aaa.unaliasResponseCode('40', '')); 25 assert.equal('40', aaa.unaliasResponseCode('40', ''));
26 }); 26 });
27 27
28 it('should return 40', function() { 28 it('should return 40', function() {
29 assert.equal('40', aaa.unaliasResponseCode('40')); 29 assert.equal('40', aaa.unaliasResponseCode('40'));
30 }); 30 });
31 31
32 }); 32 });
33 }); 33 });
34 34
35 describe('aaa', function() { 35 describe('aaa', function() {
36 var partner = require('./httppulsakita'); 36 var partner = require('./partner-scrappingkisel');
37 37
38 describe("#parseResult()", function() { 38 describe("#stockKeyword", function() {
39 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. \ 39 it('should return 10K', function() {
40 Sisa saldo Rp. 5,000,000 - Rp. 18,700 = Rp. 4,981,300</pesan></respon>'; 40 assert.equal('10K', partner.stockKeyword('Telkomsel 10K'));
41 41 });
42 data = partner.parseResult(message); 42
43 console.log(data); 43 it('should return 20K', function() {
44 assert.equal('20K', partner.stockKeyword('Telkomsel 20K'));
45 });
46
47 it('should return 25K', function() {
48 assert.equal('25K', partner.stockKeyword('Telkomsel 25K'));
49 });
50
51 it('should return 50K', function() {
52 assert.equal('50K', partner.stockKeyword('Telkomsel 50K'));
53 });
54
55 it('should return 100K', function() {
56 assert.equal('100K', partner.stockKeyword('Telkomsel 100K'));
57 });
58 });
59
60 describe("#parseStock", function() {
61 it('should return 476', function() {
62 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'));
63 });
64
65 it('should return 598', function() {
66 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'));
67 });
68
69 it('should return 0', function() {
70 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'));
71 });
72
73 it('should return 0', function() {
74 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'));
75 });
76
77 it('should return 0', function() {
78 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'));
79 });
44 80
45 it('should return 2015/6/16 15:43:35', function() { 81 it('should return 0', function() {
46 assert.equal('2015/6/16 15:43:35', data.respon.tanggal); 82 assert.equal(0, partner.parseStock('', 'Telkomsel 2000'));
47 }); 83 });
84
85 it('should return 0', function() {
86 assert.equal(0, partner.parseStock('10K = 476; 20K = 598; 25K = 600; 50K = 700; 100K = 400; 150K = 0; 200K = 0; 300K = 0; 500K = 0', ''));
87 });
88
48 }); 89 });
49 }); 90 });
50 91