Compare View
Commits (2)
Changes
Showing 6 changed files Side-by-side Diff
lib/command-handler/adddownline.js
1 | 1 | "use strict"; |
2 | 2 | |
3 | +const common = require('../common'); | |
3 | 4 | const commandError = require('./error'); |
4 | 5 | const coreapi = require('../coreapi'); |
5 | 6 | |
6 | 7 | const coreEndpoint = '/stores/create'; |
7 | 8 | |
8 | 9 | function help(keyword) { |
9 | - return `Untuk membuat downline baru, ketik perintah dengan format: ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<PIN>`; | |
10 | + return ` | |
11 | + Untuk membuat downline baru, ketik dgn format: | |
12 | + ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<PIN> | |
13 | + atau | |
14 | + ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<TERMINAL>.<PIN> | |
15 | + atau | |
16 | + ${ keyword.toUpperCase() }.<NAMADOWNLINE>.<TERMINAL>.<MARKUP>.<PIN> | |
17 | + `.replace(/^\s+/mg, '').replace(/\s+$/mg, '').trim(); | |
10 | 18 | } |
11 | 19 | |
12 | 20 | function execute(tokens, params, cb) { |
13 | - | |
21 | + | |
14 | 22 | if (!tokens || tokens.length < 3) { |
15 | 23 | const responseParams = { |
16 | 24 | body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }` |
... | ... | @@ -22,10 +30,39 @@ function execute(tokens, params, cb) { |
22 | 30 | |
23 | 31 | const coreParams = { |
24 | 32 | asker_terminal_name: params.from, |
33 | + asker_terminal_password: null, | |
25 | 34 | new_store_name: tokens[1], |
26 | - asker_terminal_password: tokens[2], | |
35 | + new_terminal_name: null, | |
36 | + markup: null, | |
27 | 37 | }; |
28 | 38 | |
39 | + if (tokens.length === 3) { | |
40 | + coreParams.asker_terminal_password = tokens[2]; | |
41 | + } else if (tokens.length === 4) { | |
42 | + coreParams.new_terminal_name = tokens[2]; | |
43 | + coreParams.asker_terminal_password = tokens[3]; | |
44 | + coreParams.generate_password = 1; | |
45 | + } else if (tokens.length === 5) { | |
46 | + coreParams.new_terminal_name = tokens[2]; | |
47 | + coreParams.markup = tokens[3]; | |
48 | + coreParams.asker_terminal_password = tokens[4]; | |
49 | + coreParams.generate_password = 1; | |
50 | + } | |
51 | + | |
52 | + if (typeof coreParams.new_terminal_name === 'string' && coreParams.new_terminal_name.indexOf('@') < 0) { | |
53 | + let suffix = ''; | |
54 | + if (common.isPhoneNumber(coreParams.new_terminal_name)) { | |
55 | + coreParams.new_terminal_name = common.indonesiaIntlNumberToZeroPrefix(coreParams.new_terminal_name); | |
56 | + suffix = '@phonenumber'; | |
57 | + } else { | |
58 | + suffix = common.guessSuffix(coreParams.asker_terminal_name); | |
59 | + } | |
60 | + | |
61 | + if (suffix) { | |
62 | + coreParams.new_terminal_name += suffix; | |
63 | + } | |
64 | + } | |
65 | + | |
29 | 66 | coreapi(coreEndpoint, coreParams, 'GET', cb); |
30 | 67 | } |
31 | 68 |
lib/command-handler/error.js
... | ... | @@ -2,7 +2,7 @@ |
2 | 2 | |
3 | 3 | exports.ERR_EMPTY_MESSAGE = 'Tidak dapat memproses pesan kosong'; |
4 | 4 | exports.ERR_UNKNOWN_COMMAND_GROUP = 'Perintah tidak dikenal'; |
5 | -exports.ERR_INVALID_FORMAT = 'Format perintah salah'; | |
5 | +exports.ERR_INVALID_FORMAT = 'Perintah salah'; | |
6 | 6 | exports.ERR_INVALID_CREDENTIAL = 'Kesalahan kredensial. User atau PIN tidak sesuai'; |
7 | 7 | exports.ERR_INVALID_CORE_RESPONSE = 'Respon CORE tidak valid'; |
8 | 8 | exports.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE = 'CORE tidak merespon dengan HTTP status 200'; |
lib/common.js
... | ... | @@ -0,0 +1,32 @@ |
1 | +function guessSuffix(terminalName) { | |
2 | + if (typeof terminalName !== 'string') { | |
3 | + return; | |
4 | + } | |
5 | + | |
6 | + const items = terminalName.split('@'); | |
7 | + if (!items || (items.length < 2)) return; | |
8 | + | |
9 | + return `@${items[items.length - 1]}`; | |
10 | +} | |
11 | + | |
12 | +function isPhoneNumber(terminalName) { | |
13 | + if (typeof terminalName !== 'string') { | |
14 | + return false; | |
15 | + } | |
16 | + | |
17 | + if (!terminalName) return false; | |
18 | + | |
19 | + return Boolean(terminalName.replace(/^\+/, '').match(/^\d+$/)); | |
20 | +} | |
21 | + | |
22 | +function indonesiaIntlNumberToZeroPrefix(phoneNumber) { | |
23 | + if (!isPhoneNumber(phoneNumber)) { | |
24 | + return phoneNumber; | |
25 | + } | |
26 | + | |
27 | + return phoneNumber.replace(/^\+62/, '0'); | |
28 | +} | |
29 | + | |
30 | +exports.guessSuffix = guessSuffix; | |
31 | +exports.isPhoneNumber = isPhoneNumber; | |
32 | +exports.indonesiaIntlNumberToZeroPrefix = indonesiaIntlNumberToZeroPrefix; |
package-lock.json
package.json
test/common.js
... | ... | @@ -0,0 +1,34 @@ |
1 | +/* global describe it */ | |
2 | + | |
3 | +const should = require('should'); | |
4 | +const common = require('../lib/common'); | |
5 | + | |
6 | +describe('#common', () => { | |
7 | + describe('#guessSuffix', () => { | |
8 | + it('should handle missing suffix', () => { | |
9 | + should.not.exist(common.guessSuffix('xxx')); | |
10 | + }); | |
11 | + | |
12 | + it('should return correct suffix', () => { | |
13 | + common.guessSuffix('xxx@yyy').should.equal('@yyy'); | |
14 | + common.guessSuffix('xxx@yyy@zzz').should.equal('@zzz'); | |
15 | + }) | |
16 | + }); | |
17 | + | |
18 | + describe('#isPhoneNumber', () => { | |
19 | + it('should detect correctly', () => { | |
20 | + common.isPhoneNumber('0818').should.be.ok(); | |
21 | + common.isPhoneNumber('62818').should.be.ok(); | |
22 | + common.isPhoneNumber('+62818').should.be.ok(); | |
23 | + common.isPhoneNumber('ada').should.not.be.ok(); | |
24 | + }); | |
25 | + }); | |
26 | + | |
27 | + describe('#indonesiaIntlNumberToZeroPrefix', () => { | |
28 | + it('should transform correctly', () => { | |
29 | + common.indonesiaIntlNumberToZeroPrefix('+62818').should.equal('0818'); | |
30 | + common.indonesiaIntlNumberToZeroPrefix('0818').should.equal('0818'); | |
31 | + common.indonesiaIntlNumberToZeroPrefix('ada').should.equal('ada'); | |
32 | + }); | |
33 | + }) | |
34 | +}); | |
0 | 35 | \ No newline at end of file |