Commit c7ef5eaccc40a7fb5d3bc73c57137c7dda91e7ad

Authored by Adhidarma Hadiwinoto
1 parent efa2c548d6
Exists in master

Ubah 0 di depan menjadi pada nama terminal baru saat pembuatan downline baru jika phone number

Showing 3 changed files with 17 additions and 6 deletions Inline Diff

lib/command-handler/adddownline.js
1 "use strict"; 1 "use strict";
2 2
3 const common = require('../common'); 3 const common = require('../common');
4 const commandError = require('./error'); 4 const commandError = require('./error');
5 const coreapi = require('../coreapi'); 5 const coreapi = require('../coreapi');
6 6
7 const coreEndpoint = '/stores/create'; 7 const coreEndpoint = '/stores/create';
8 8
9 function help(keyword) { 9 function help(keyword) {
10 return ` 10 return `
11 Untuk membuat downline baru, ketik dgn format: 11 Untuk membuat downline baru, ketik dgn format:
12 ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<PIN> 12 ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<PIN>
13 atau 13 atau
14 ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<TERMINAL>.<PIN> 14 ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<TERMINAL>.<PIN>
15 atau 15 atau
16 ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<TERMINAL>.<MARKUP>.<PIN> 16 ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<TERMINAL>.<MARKUP>.<PIN>
17 `.replace(/^\s+/mg, '').replace(/\s+$/mg, '').trim(); 17 `.replace(/^\s+/mg, '').replace(/\s+$/mg, '').trim();
18 } 18 }
19 19
20 function execute(tokens, params, cb) { 20 function execute(tokens, params, cb) {
21 21
22 if (!tokens || tokens.length < 3) { 22 if (!tokens || tokens.length < 3) {
23 const responseParams = { 23 const responseParams = {
24 body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }` 24 body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }`
25 } 25 }
26 26
27 cb(null, null, responseParams); 27 cb(null, null, responseParams);
28 return; 28 return;
29 } 29 }
30 30
31 const coreParams = { 31 const coreParams = {
32 asker_terminal_name: params.from, 32 asker_terminal_name: params.from,
33 asker_terminal_password: null, 33 asker_terminal_password: null,
34 new_store_name: tokens[1], 34 new_store_name: tokens[1],
35 new_terminal_name: null, 35 new_terminal_name: null,
36 markup: null, 36 markup: null,
37 }; 37 };
38 38
39 if (tokens.length === 3) { 39 if (tokens.length === 3) {
40 coreParams.asker_terminal_password = tokens[2]; 40 coreParams.asker_terminal_password = tokens[2];
41 } else if (tokens.length === 4) { 41 } else if (tokens.length === 4) {
42 coreParams.new_terminal_name = tokens[2]; 42 coreParams.new_terminal_name = tokens[2];
43 coreParams.asker_terminal_password = tokens[3]; 43 coreParams.asker_terminal_password = tokens[3];
44 coreParams.generate_password = 1; 44 coreParams.generate_password = 1;
45 } else if (tokens.length === 5) { 45 } else if (tokens.length === 5) {
46 coreParams.new_terminal_name = tokens[2]; 46 coreParams.new_terminal_name = tokens[2];
47 coreParams.markup = tokens[3]; 47 coreParams.markup = tokens[3];
48 coreParams.asker_terminal_password = tokens[4]; 48 coreParams.asker_terminal_password = tokens[4];
49 coreParams.generate_password = 1; 49 coreParams.generate_password = 1;
50 } 50 }
51 51
52 /*
52 if (typeof coreParams.new_terminal_name === 'string' && coreParams.new_terminal_name.indexOf('@') < 0) { 53 if (typeof coreParams.new_terminal_name === 'string' && coreParams.new_terminal_name.indexOf('@') < 0) {
53 let suffix = ''; 54 let suffix = '';
54 if (common.isPhoneNumber(coreParams.new_terminal_name)) { 55 if (common.isPhoneNumber(coreParams.new_terminal_name)) {
55 coreParams.new_terminal_name = common.indonesiaIntlNumberToZeroPrefix(coreParams.new_terminal_name); 56 coreParams.new_terminal_name = common.indonesiaIntlNumberToZeroPrefix(coreParams.new_terminal_name);
56 suffix = '@phonenumber'; 57 suffix = '@phonenumber';
57 } else { 58 } else {
58 suffix = common.guessSuffix(coreParams.asker_terminal_name); 59 suffix = common.guessSuffix(coreParams.asker_terminal_name);
59 } 60 }
60 61
61 if (suffix) { 62 if (suffix) {
62 coreParams.new_terminal_name += suffix; 63 coreParams.new_terminal_name += suffix;
63 } 64 }
64 } 65 }
65 66
66 coreapi(coreEndpoint, coreParams, 'GET', cb); 67 coreapi(coreEndpoint, coreParams, 'GET', cb);
68 */
69
70 if (typeof coreParams.new_terminal_name === 'string' && common.isPhoneNumber(coreParams.new_terminal_name) && coreParams.new_terminal_name.indexOf('0') === 0) {
71 coreParams.new_terminal_name = coreParams.new_terminal_name.replace(/^0/, '62');
72 }
67 } 73 }
68 74
69 module.exports = execute; 75 module.exports = execute;
1 function guessSuffix(terminalName) { 1 function guessSuffix(terminalName) {
2 if (typeof terminalName !== 'string') { 2 if (typeof terminalName !== 'string') {
3 return; 3 return;
4 } 4 }
5 5
6 const items = terminalName.split('@'); 6 const items = terminalName.split('@');
7 if (!items || (items.length < 2)) return; 7 if (!items || (items.length < 2)) return;
8 8
9 return `@${items[items.length - 1]}`; 9 return `@${items[items.length - 1]}`;
10 } 10 }
11 11
12 function isPhoneNumber(terminalName) { 12 function isPhoneNumber(terminalName) {
13 if (typeof terminalName !== 'string') { 13 if (typeof terminalName !== 'string') {
14 return false; 14 return false;
15 } 15 }
16 16
17 if (!terminalName) return false; 17 if (terminalName.trim().length < 7) return false;
18 18
19 return Boolean(terminalName.replace(/^\+/, '').match(/^\d+$/)); 19 return Boolean(terminalName.replace(/^\+/, '').match(/^\d+$/));
20 } 20 }
21 21
22 function indonesiaIntlNumberToZeroPrefix(phoneNumber) { 22 function indonesiaIntlNumberToZeroPrefix(phoneNumber) {
23 if (!isPhoneNumber(phoneNumber)) { 23 if (!isPhoneNumber(phoneNumber)) {
24 return phoneNumber; 24 return phoneNumber;
25 } 25 }
26 26
27 return phoneNumber.replace(/^\+62/, '0'); 27 return phoneNumber.replace(/^\+62/, '0');
28 } 28 }
29 29
30 exports.guessSuffix = guessSuffix; 30 exports.guessSuffix = guessSuffix;
31 exports.isPhoneNumber = isPhoneNumber; 31 exports.isPhoneNumber = isPhoneNumber;
32 exports.indonesiaIntlNumberToZeroPrefix = indonesiaIntlNumberToZeroPrefix; 32 exports.indonesiaIntlNumberToZeroPrefix = indonesiaIntlNumberToZeroPrefix;
33 33
1 /* global describe it */ 1 /* global describe it */
2 2
3 const should = require('should'); 3 const should = require('should');
4 const common = require('../lib/common'); 4 const common = require('../lib/common');
5 5
6 describe('#common', () => { 6 describe('#common', () => {
7 describe('#guessSuffix', () => { 7 describe('#guessSuffix', () => {
8 it('should handle missing suffix', () => { 8 it('should handle missing suffix', () => {
9 should.not.exist(common.guessSuffix('xxx')); 9 should.not.exist(common.guessSuffix('xxx'));
10 }); 10 });
11 11
12 it('should return correct suffix', () => { 12 it('should return correct suffix', () => {
13 common.guessSuffix('xxx@yyy').should.equal('@yyy'); 13 common.guessSuffix('xxx@yyy').should.equal('@yyy');
14 common.guessSuffix('xxx@yyy@zzz').should.equal('@zzz'); 14 common.guessSuffix('xxx@yyy@zzz').should.equal('@zzz');
15 }) 15 })
16 }); 16 });
17 17
18 describe('#isPhoneNumber', () => { 18 describe('#isPhoneNumber', () => {
19 it('should detect correctly', () => { 19 it('should detect correctly', () => {
20 common.isPhoneNumber('0818').should.be.ok(); 20 common.isPhoneNumber('0818').should.not.be.ok();
21 common.isPhoneNumber('62818').should.be.ok(); 21 common.isPhoneNumber('62818').should.not.be.ok();
22 common.isPhoneNumber('+62818').should.be.ok(); 22 common.isPhoneNumber('+62818').should.not.be.ok();
23
24 common.isPhoneNumber('081808180').should.be.ok();
25 common.isPhoneNumber('62818818').should.be.ok();
26 common.isPhoneNumber('+62818818').should.be.ok();
27
23 common.isPhoneNumber('ada').should.not.be.ok(); 28 common.isPhoneNumber('ada').should.not.be.ok();
24 }); 29 });
25 }); 30 });
26 31
27 describe('#indonesiaIntlNumberToZeroPrefix', () => { 32 describe('#indonesiaIntlNumberToZeroPrefix', () => {
28 it('should transform correctly', () => { 33 it('should transform correctly', () => {
29 common.indonesiaIntlNumberToZeroPrefix('+62818').should.equal('0818'); 34 common.indonesiaIntlNumberToZeroPrefix('+6281812341234').should.equal('081812341234', '+6281812341234');
30 common.indonesiaIntlNumberToZeroPrefix('0818').should.equal('0818'); 35 common.indonesiaIntlNumberToZeroPrefix('081808180').should.equal('081808180', '081808180');
31 common.indonesiaIntlNumberToZeroPrefix('ada').should.equal('ada'); 36 common.indonesiaIntlNumberToZeroPrefix('ada').should.equal('ada');
32 }); 37 });
33 }) 38 })
34 }); 39 });