Commit d927538f2bc028da88fdd7370c1e2bb9726e5543

Authored by Adhidarma Hadiwinoto
1 parent 08a7fd23ee
Exists in master

out of stock detection

Showing 2 changed files with 43 additions and 1 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.join(' ').trim(); 91 var stocks = result.trx_response.stock.join(' ').trim();
92 var stock = parseStock(stocks, product); 92 var stock = parseStock(stocks, product);
93 93
94 if (stock == 0) {
95 console.log('OUT OF STOCK: ' + task['product']);
96 config.globals.products = productsWithout(task['product']);
97 }
98
94 response_code = '00'; 99 response_code = '00';
95 100
96 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit'; 101 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit';
97 } 102 }
98 103
99 console.log('Message to AAA: ' + message); 104 console.log('Message to AAA: ' + message);
100 callbackReport(task['requestId'], response_code, message); 105 callbackReport(task['requestId'], response_code, message);
101 }); 106 });
102 }); 107 });
103 } 108 }
104 109
105 function start(_config, _callbackReport) { 110 function start(_config, _callbackReport) {
106 config = _config; 111 config = _config;
107 callbackReport = _callbackReport 112 callbackReport = _callbackReport
108 } 113 }
109 114
110 function stockKeyword(product_desc) { 115 function stockKeyword(product_desc) {
111 var keywords = product_desc.match(/\s\d+K$/); 116 var keywords = product_desc.match(/\s\d+K$/);
112 if (!keywords) { 117 if (!keywords) {
113 return; 118 return;
114 } 119 }
115 120
116 if (keywords.length < 1) { 121 if (keywords.length < 1) {
117 return; 122 return;
118 } 123 }
119 124
120 return keywords[0].trim(); 125 return keywords[0].trim();
121 } 126 }
122 127
123 function parseStock(stocks, product_desc) { 128 function parseStock(stocks, product_desc) {
124 product_keyword = stockKeyword(product_desc); 129 product_keyword = stockKeyword(product_desc);
125 130
126 var _stocks = stocks.split('; '); 131 var _stocks = stocks.split('; ');
127 count = _stocks.length; 132 count = _stocks.length;
128 for (var i = 0; i < count; i++) { 133 for (var i = 0; i < count; i++) {
129 var stock = _stocks[i].split(' = '); 134 var stock = _stocks[i].split(' = ');
130 if (stock.length < 2) { 135 if (stock.length < 2) {
131 continue; 136 continue;
132 } 137 }
133 138
134 if (stock[0] == product_keyword) { 139 if (stock[0] == product_keyword) {
135 return parseInt(stock[1]); 140 return parseInt(stock[1]);
136 } 141 }
137 } 142 }
138 143
139 return 0; 144 return 0;
140 } 145 }
141 146
147 function productsWithout(to_be_removed, products) {
148 if (products === undefined) {
149 products = config.globals.products;
150 }
151
152 if (products.search(/,$/) == -1) {
153 products = products + ',';
154 }
155
156 products = products.replace(/\s+/, '');
157 products = products.replace(to_be_removed + ',' , '');
158 products = products.replace(/,$/, '');
159
160 return products;
161 }
162
142 exports.start = start; 163 exports.start = start;
143 exports.topupRequest = topupRequest; 164 exports.topupRequest = topupRequest;
144 exports.stockKeyword = stockKeyword; 165 exports.stockKeyword = stockKeyword;
145 exports.parseStock = parseStock; 166 exports.parseStock = parseStock;
167 exports.productsWithout = productsWithout;
146 168
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('partner', function() {
36 var partner = require('./partner-scrappingkisel'); 36 var partner = require('./partner-scrappingkisel');
37 37
38 describe("#stockKeyword", function() { 38 describe("#stockKeyword", function() {
39 it('should return 10K', function() { 39 it('should return 10K', function() {
40 assert.equal('10K', partner.stockKeyword('Telkomsel 10K')); 40 assert.equal('10K', partner.stockKeyword('Telkomsel 10K'));
41 }); 41 });
42 42
43 it('should return 20K', function() { 43 it('should return 20K', function() {
44 assert.equal('20K', partner.stockKeyword('Telkomsel 20K')); 44 assert.equal('20K', partner.stockKeyword('Telkomsel 20K'));
45 }); 45 });
46 46
47 it('should return 25K', function() { 47 it('should return 25K', function() {
48 assert.equal('25K', partner.stockKeyword('Telkomsel 25K')); 48 assert.equal('25K', partner.stockKeyword('Telkomsel 25K'));
49 }); 49 });
50 50
51 it('should return 50K', function() { 51 it('should return 50K', function() {
52 assert.equal('50K', partner.stockKeyword('Telkomsel 50K')); 52 assert.equal('50K', partner.stockKeyword('Telkomsel 50K'));
53 }); 53 });
54 54
55 it('should return 100K', function() { 55 it('should return 100K', function() {
56 assert.equal('100K', partner.stockKeyword('Telkomsel 100K')); 56 assert.equal('100K', partner.stockKeyword('Telkomsel 100K'));
57 }); 57 });
58 }); 58 });
59 59
60 describe("#parseStock", function() { 60 describe("#parseStock", function() {
61 it('should return 476', 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')); 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 }); 63 });
64 64
65 it('should return 598', function() { 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')); 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 }); 67 });
68 68
69 it('should return 0', function() { 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')); 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 }); 71 });
72 72
73 it('should return 0', function() { 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')); 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 }); 75 });
76 76
77 it('should return 0', function() { 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')); 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 }); 79 });
80 80
81 it('should return 0', function() { 81 it('should return 0', function() {
82 assert.equal(0, partner.parseStock('', 'Telkomsel 2000')); 82 assert.equal(0, partner.parseStock('', 'Telkomsel 2000'));
83 }); 83 });
84 84
85 it('should return 0', function() { 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', '')); 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 }); 87 });
88 88
89 }); 89 });
90
91 describe('#productsWithout', function () {
92
93 it('should return S10,S20,S50,S100', function() {
94 assert.equal('S10,S20,S50,S100', partner.productsWithout('S25', 'S10,S20,S25,S50,S100'));
95 });
96
97 it('should return S10,S20,S25,S50', function() {
98 assert.equal('S10,S20,S25,S50', partner.productsWithout('S100', 'S10,S20,S25,S50,S100'));
99 });
100
101 it('should return S20,S25,S50,S100', function() {
102 assert.equal('S20,S25,S50,S100', partner.productsWithout('S10', 'S10,S20,S25,S50,S100'));
103 });
104
105 it('should return S10,S20,S25,S50', function() {
106 assert.equal('S10,S20,S25,S50', partner.productsWithout('S100', 'S10,S20,S25,S50'));
107 });
108
109 });
90 }); 110 });
91 111