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 Side-by-side Diff

partner-scrappingkisel.js
... ... @@ -91,6 +91,11 @@ function topupRequest(task) {
91 91 var stocks = result.trx_response.stock.join(' ').trim();
92 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 99 response_code = '00';
95 100  
96 101 message = 'SN=' + kode_voucher + '; ' + product + ' ' + destination + ' ' + harga + ' ref_num: ' + ref_num + ' kode_voucher: ' + kode_voucher + ' sisa stock: ' + stock + ' unit';
... ... @@ -139,7 +144,24 @@ function parseStock(stocks, product_desc) {
139 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 163 exports.start = start;
143 164 exports.topupRequest = topupRequest;
144 165 exports.stockKeyword = stockKeyword;
145 166 exports.parseStock = parseStock;
  167 +exports.productsWithout = productsWithout;
... ... @@ -32,7 +32,7 @@ describe('aaa', function() {
32 32 });
33 33 });
34 34  
35   -describe('aaa', function() {
  35 +describe('partner', function() {
36 36 var partner = require('./partner-scrappingkisel');
37 37  
38 38 describe("#stockKeyword", function() {
... ... @@ -87,4 +87,24 @@ describe('aaa', function() {
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 });