Commit 7a59681851b24861116e647f53503bdb49db9439

Authored by Adhidarma Hadiwinoto
1 parent 53246e000f
Exists in master

destinationCorrector

Showing 3 changed files with 31 additions and 2 deletions Inline Diff

lib/command-handler/buy.js
1 "use strict"; 1 "use strict";
2 2
3 const moment = require('moment'); 3 const moment = require('moment');
4 4
5 const logger = require('komodo-sdk/logger'); 5 const logger = require('komodo-sdk/logger');
6 6
7 const commands = require('../command-group'); 7 const commands = require('../command-group');
8 const commandError = require('./error'); 8 const commandError = require('./error');
9 const destinationCorrector = require('../destination-corrector');
9 const coreapi = require('../coreapi'); 10 const coreapi = require('../coreapi');
10 11
11 const coreEndpoint = '/prepaid/buy'; 12 const coreEndpoint = '/prepaid/buy';
12 13
13 function generateRequestId(product, destination) { 14 function generateRequestId(product, destination) {
14 return `AUTO_${ product.toUpperCase() }_${ destination }_${ moment().format('YYYYMMDD') }`; 15 return `AUTO_${ product.toUpperCase() }_${ destination }_${ moment().format('YYYYMMDD') }`;
15 } 16 }
16 17
17 function help() { 18 function help() {
18 return `Untuk pembelian, ketik perintah dengan format: <KODEPRODUK>.<NOMORTUJUAN>.<PIN>`; 19 return `Untuk pembelian, ketik perintah dengan format: <KODEPRODUK>.<NOMORTUJUAN>.<PIN>`;
19 } 20 }
20 21
21 function execute(tokens, params, cb) { 22 function execute(tokens, params, cb) {
22 if (!tokens || tokens.length < 3) { 23 if (!tokens || tokens.length < 3) {
23 const responseParams = { 24 const responseParams = {
24 body: `${ commandError.ERR_INVALID_FORMAT }. ${ help() }` 25 body: `${ commandError.ERR_INVALID_FORMAT }. ${ help() }`
25 } 26 }
26 27
27 cb(null, null, responseParams); 28 cb(null, null, responseParams);
28 return; 29 return;
29 } 30 }
30 31
31 if (commands[ tokens[0] ] !== 'buy') { 32 if (commands[ tokens[0] ] !== 'buy') {
32 tokens.unshift('buy'); 33 tokens.unshift('buy');
33 logger.verbose('Rearrange tokens', {tokens: tokens}); 34 logger.verbose('Rearrange tokens', {tokens: tokens});
34 } 35 }
35 36
37 const destination = destinationCorrector((tokens[2] || '').trim());
38
36 const coreParams = { 39 const coreParams = {
37 origin: params.origin, 40 origin: params.origin,
38 report_ip: params.report_ip, 41 report_ip: params.report_ip,
39 report_port: params.report_port, 42 report_port: params.report_port,
40 terminal_name: params.from, 43 terminal_name: params.from,
41 product_name: (tokens[1] || '').trim().toUpperCase(), 44 product_name: (tokens[1] || '').trim().toUpperCase(),
42 destination: (tokens[2] || '').trim().replace(/^\+62/, '0'), 45 destination,
43 password: tokens[3], 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 postpaid: 0 48 postpaid: 0
46 }; 49 };
47 50
48 coreapi(coreEndpoint, coreParams, 'GET', cb); 51 coreapi(coreEndpoint, coreParams, 'GET', cb);
49 } 52 }
50 53
51 module.exports = execute; 54 module.exports = execute;
lib/destination-corrector/index.js
File was created 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 };
26
lib/destination-corrector/util.js
File was created 1 exports.shouldNotProcessed = (val) => val.search(/[^0-9\s-()+;]/) >= 0;
2