diff --git a/lib/command-handler/buy.js b/lib/command-handler/buy.js index 9ae6042..dd9c8c3 100644 --- a/lib/command-handler/buy.js +++ b/lib/command-handler/buy.js @@ -6,6 +6,7 @@ const logger = require('komodo-sdk/logger'); const commands = require('../command-group'); const commandError = require('./error'); +const destinationCorrector = require('../destination-corrector'); const coreapi = require('../coreapi'); const coreEndpoint = '/prepaid/buy'; @@ -33,15 +34,17 @@ function execute(tokens, params, cb) { logger.verbose('Rearrange tokens', {tokens: tokens}); } + const destination = destinationCorrector((tokens[2] || '').trim()); + const coreParams = { origin: params.origin, report_ip: params.report_ip, report_port: params.report_port, terminal_name: params.from, product_name: (tokens[1] || '').trim().toUpperCase(), - destination: (tokens[2] || '').trim().replace(/^\+62/, '0'), + destination, password: tokens[3], - request_id: `${generateRequestId(tokens[1], tokens[2])}${Number(tokens[4]) ? '_req' + Number(tokens[4]) : ''}`, + request_id: `${generateRequestId(tokens[1], destination)}${Number(tokens[4]) ? '_req' + Number(tokens[4]) : ''}`, postpaid: 0 }; diff --git a/lib/destination-corrector/index.js b/lib/destination-corrector/index.js new file mode 100644 index 0000000..e3292c5 --- /dev/null +++ b/lib/destination-corrector/index.js @@ -0,0 +1,25 @@ +const util = require('./util'); + +module.exports = (val, opts) => { + let result = val; + if (typeof result === 'number') { + result = result.toString(); + } + + if (typeof result !== 'string') return val; + + result = result.trim(); + + if (util.shouldNotProcessed(result)) return result; + + result = result + .replace(/\s+/g, '') + .replace(/-+/g, '') + .replace(/[();]/g, ''); + + if (!opts || !opts.do_not_change_intl_code) { + result = result.replace(/^\+62/, '0'); + } + + return result; +}; diff --git a/lib/destination-corrector/util.js b/lib/destination-corrector/util.js new file mode 100644 index 0000000..013bf09 --- /dev/null +++ b/lib/destination-corrector/util.js @@ -0,0 +1 @@ +exports.shouldNotProcessed = (val) => val.search(/[^0-9\s-()+;]/) >= 0;