Commit 7a59681851b24861116e647f53503bdb49db9439

Authored by Adhidarma Hadiwinoto
1 parent 53246e000f
Exists in master

destinationCorrector

Showing 3 changed files with 31 additions and 2 deletions Side-by-side Diff

lib/command-handler/buy.js
... ... @@ -6,6 +6,7 @@ const logger = require('komodo-sdk/logger');
6 6  
7 7 const commands = require('../command-group');
8 8 const commandError = require('./error');
  9 +const destinationCorrector = require('../destination-corrector');
9 10 const coreapi = require('../coreapi');
10 11  
11 12 const coreEndpoint = '/prepaid/buy';
... ... @@ -33,15 +34,17 @@ function execute(tokens, params, cb) {
33 34 logger.verbose('Rearrange tokens', {tokens: tokens});
34 35 }
35 36  
  37 + const destination = destinationCorrector((tokens[2] || '').trim());
  38 +
36 39 const coreParams = {
37 40 origin: params.origin,
38 41 report_ip: params.report_ip,
39 42 report_port: params.report_port,
40 43 terminal_name: params.from,
41 44 product_name: (tokens[1] || '').trim().toUpperCase(),
42   - destination: (tokens[2] || '').trim().replace(/^\+62/, '0'),
  45 + destination,
43 46 password: tokens[3],
44   - request_id: `${generateRequestId(tokens[1], tokens[2])}${Number(tokens[4]) ? '_req' + Number(tokens[4]) : ''}`,
  47 + request_id: `${generateRequestId(tokens[1], destination)}${Number(tokens[4]) ? '_req' + Number(tokens[4]) : ''}`,
45 48 postpaid: 0
46 49 };
47 50  
lib/destination-corrector/index.js
... ... @@ -0,0 +1,25 @@
  1 +const util = require('./util');
  2 +
  3 +module.exports = (val, opts) => {
  4 + let result = val;
  5 + if (typeof result === 'number') {
  6 + result = result.toString();
  7 + }
  8 +
  9 + if (typeof result !== 'string') return val;
  10 +
  11 + result = result.trim();
  12 +
  13 + if (util.shouldNotProcessed(result)) return result;
  14 +
  15 + result = result
  16 + .replace(/\s+/g, '')
  17 + .replace(/-+/g, '')
  18 + .replace(/[();]/g, '');
  19 +
  20 + if (!opts || !opts.do_not_change_intl_code) {
  21 + result = result.replace(/^\+62/, '0');
  22 + }
  23 +
  24 + return result;
  25 +};
lib/destination-corrector/util.js
... ... @@ -0,0 +1 @@
  1 +exports.shouldNotProcessed = (val) => val.search(/[^0-9\s-()+;]/) >= 0;