Commit 1aee6daf6a555574e51ac5ee6d0abee10dcc44d5

Authored by Adhidarma Hadiwinoto
1 parent 478db8b4ca
Exists in master

LISTTERMINAL multiple formats

Showing 1 changed file with 30 additions and 5 deletions Inline Diff

lib/command-handler/listterminal.js
1 const CORE_ENDPOINT = '/terminals'; 1 const CORE_ENDPOINT = '/terminals';
2 2
3 const commandError = require('./error'); 3 const commandError = require('./error');
4 const coreapi = require('../coreapi'); 4 const coreapi = require('../coreapi');
5 5
6 function help(keyword) { 6 function help(keyword) {
7 return `Untuk list terminal anda, ketik perintah dengan format: ${ keyword.toUpperCase() }.<PIN> atau ${ keyword.toUpperCase() }.<IDDOWNLINE>.<PIN>`; 7 return `
8 Untuk list terminal anda, ketik perintah dengan format:
9
10 ${ keyword.toUpperCase() }.<PIN>
11 atau
12 ${ keyword.toUpperCase() }.#<IDDOWNLINE>.<PIN>
13 atau
14 ${ keyword.toUpperCase() }.<TERMINALDOWNLINE>.<PIN>
15 `.trim();
8 } 16 }
9 17
10 function execute(tokens, params, cb) { 18 function execute(tokens, params, cb) {
11 19
12 if (!tokens || tokens.length < 2) { 20 if (!tokens || tokens.length < 2) {
13 const responseParams = { 21 const responseParams = {
14 body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }` 22 body: `
23 ${ commandError.ERR_INVALID_FORMAT }: ${params.msg}
24
25 ${ help(tokens[0]) }
26 `.trim(),
15 } 27 }
16 28
17 cb(null, null, responseParams); 29 cb(null, null, responseParams);
18 return; 30 return;
19 } 31 }
20 32
33 const idxDownline = (tokens.length <= 2) ? null : 1;
21 const idxPin = (tokens.length <= 2) ? 1 : 2; 34 const idxPin = (tokens.length <= 2) ? 1 : 2;
22 35
36 let downline;
37 let downlineStoreId;
38 let downlineTerminalName;
39 if (idxDownline) {
40 downline = tokens[idxDownline];
41 if (downline.indexOf('#') === 0) {
42 downlineStoreId = downline.replace(/^#/, '');
43 } else {
44 downlineTerminalName = downline;
45 }
46 }
47
23 const coreParams = { 48 const coreParams = {
24 asker_terminal_name: params.from, 49 asker_terminal_name: params.from,
25 asker_terminal_password: tokens[ idxPin ], 50 asker_terminal_password: tokens[ idxPin ],
26 store_id: tokens[1].replace(/^#/, ''), 51 store_id: downlineStoreId,
27 52 terminal_name: downlineTerminalName,
28 }; 53 };
29 54
30 coreapi(CORE_ENDPOINT, coreParams, 'GET', cb); 55 coreapi(CORE_ENDPOINT, coreParams, 'GET', cb);
31 } 56 }
32 57
33 module.exports = execute; 58 module.exports = execute;