Commit 4a6f9dce6ace8cb6396635c183a8e9fd73423da2
1 parent
4923ddcd30
Exists in
master
Ubah 0 di depan pada terminal baru di addterminal jika phone number
Showing 1 changed file with 5 additions and 0 deletions Inline Diff
lib/command-handler/addterminal.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const common = require('../common'); | ||
3 | const commandError = require('./error'); | 4 | const commandError = require('./error'); |
4 | const coreapi = require('../coreapi'); | 5 | const coreapi = require('../coreapi'); |
5 | 6 | ||
6 | const coreEndpoint = '/terminals/create'; | 7 | const coreEndpoint = '/terminals/create'; |
7 | 8 | ||
8 | function help(keyword) { | 9 | function help(keyword) { |
9 | return `Untuk membuat terminal, ketik perintah dengan format: ${ keyword.toUpperCase() }.<NAMATERMINALBARU>.<PINTERMINALBARU>.<PIN> atau ${ keyword.toUpperCase() }.<IDDOWNLINE>.<NAMATERMINALBARU>.<PINTERMINALBARU>.<PIN>`; | 10 | return `Untuk membuat terminal, ketik perintah dengan format: ${ keyword.toUpperCase() }.<NAMATERMINALBARU>.<PINTERMINALBARU>.<PIN> atau ${ keyword.toUpperCase() }.<IDDOWNLINE>.<NAMATERMINALBARU>.<PINTERMINALBARU>.<PIN>`; |
10 | } | 11 | } |
11 | 12 | ||
12 | function execute(tokens, params, cb) { | 13 | function execute(tokens, params, cb) { |
13 | 14 | ||
14 | if (!tokens || tokens.length < 4) { | 15 | if (!tokens || tokens.length < 4) { |
15 | const responseParams = { | 16 | const responseParams = { |
16 | body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }` | 17 | body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }` |
17 | } | 18 | } |
18 | 19 | ||
19 | cb(null, null, responseParams); | 20 | cb(null, null, responseParams); |
20 | return; | 21 | return; |
21 | } | 22 | } |
22 | 23 | ||
23 | const idxPin = (tokens.length < 5) ? 3 : 4; | 24 | const idxPin = (tokens.length < 5) ? 3 : 4; |
24 | 25 | ||
25 | const coreParams = { | 26 | const coreParams = { |
26 | asker_terminal_name: params.from, | 27 | asker_terminal_name: params.from, |
27 | asker_terminal_password: tokens[ idxPin ], | 28 | asker_terminal_password: tokens[ idxPin ], |
28 | store_id: tokens.length > 4 ? tokens[1] : null, | 29 | store_id: tokens.length > 4 ? tokens[1] : null, |
29 | terminal_name: tokens.length < 5 ? tokens[1] : tokens[2], | 30 | terminal_name: tokens.length < 5 ? tokens[1] : tokens[2], |
30 | password: tokens.length < 5 ? tokens[2] : tokens[3] | 31 | password: tokens.length < 5 ? tokens[2] : tokens[3] |
31 | }; | 32 | }; |
32 | 33 | ||
34 | if (typeof coreParams.terminal_name === 'string' && common.isPhoneNumber(coreParams.terminal_name) && coreParams.indexOf('0') === 0) { | ||
35 | coreParams.terminal_name = coreParams.terminal_name.replace(/^0/, '62'); | ||
36 | } | ||
37 | |||
33 | coreapi(coreEndpoint, coreParams, 'GET', cb); | 38 | coreapi(coreEndpoint, coreParams, 'GET', cb); |
34 | } | 39 | } |
35 | 40 | ||
36 | module.exports = execute; | 41 | module.exports = execute; |