Compare View
Commits (7)
Changes
Showing 13 changed files Side-by-side Diff
index.js
... | ... | @@ -4,8 +4,12 @@ process.chdir(__dirname); |
4 | 4 | const fs = require('fs'); |
5 | 5 | fs.writeFileSync('pid.txt', process.pid); |
6 | 6 | |
7 | +const matrix = require('komodo-sdk/matrix'); | |
8 | +matrix.NODE_ENV = process.env.NODE_ENV; | |
9 | + | |
7 | 10 | const logger = require('komodo-sdk/logger'); |
8 | 11 | const coreUrl = require('komodo-sdk/core-url'); |
9 | 12 | logger.verbose('CORE URL: ' + coreUrl); |
10 | 13 | |
11 | 14 | require('./lib/http-listener'); |
15 | + |
lib/command-handler/error.js
... | ... | @@ -6,4 +6,5 @@ exports.ERR_INVALID_FORMAT = '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'; |
9 | -exports.ERR_NOT_IMPLEMENTED = 'Perintah belum dapat diproses'; | |
10 | 9 | \ No newline at end of file |
10 | +exports.ERR_NOT_IMPLEMENTED = 'Perintah belum dapat diproses'; | |
11 | +exports.ERR_CORE_VERSION_NOT_SUFFICIENT = 'Versi CORE tidak mencukupi'; | |
11 | 12 | \ No newline at end of file |
lib/command-handler/index.js
... | ... | @@ -6,10 +6,11 @@ const commandParser = require('../command-parser'); |
6 | 6 | |
7 | 7 | const commandError = require('./error'); |
8 | 8 | |
9 | +const handlerHelp = require('./help'); | |
9 | 10 | const handlerBuy = require('./buy'); |
10 | 11 | const handlerBalance = require('./balance'); |
11 | 12 | const handlerPrice = require('./price'); |
12 | -const handlerHelp = require('./help'); | |
13 | +const handlerListDownline = require('./listdownline'); | |
13 | 14 | |
14 | 15 | function execute(msg, params, cb) { |
15 | 16 | |
... | ... | @@ -31,6 +32,9 @@ function execute(msg, params, cb) { |
31 | 32 | else if (commandGroup === 'price') { |
32 | 33 | handlerPrice(tokens, params, cb); |
33 | 34 | } |
35 | + else if (commandGroup === 'listdownline') { | |
36 | + handlerListDownline(tokens, params, cb); | |
37 | + } | |
34 | 38 | else if (commandGroup === 'help') { |
35 | 39 | handlerHelp(cb) |
36 | 40 | } |
lib/command-handler/listdownline.js
... | ... | @@ -0,0 +1,31 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const commandError = require('./error'); | |
4 | +const coreapi = require('../coreapi'); | |
5 | + | |
6 | +const coreEndpoint = '/stores/downlines'; | |
7 | + | |
8 | +function help(keyword) { | |
9 | + return `Untuk list downline anda, ketik perintah dengan format: ${ keyword.toUpperCase() }.<PIN>`; | |
10 | +} | |
11 | + | |
12 | +function execute(tokens, params, cb) { | |
13 | + | |
14 | + if (!tokens || tokens.length < 2) { | |
15 | + const responseParams = { | |
16 | + body: `${ commandError.ERR_INVALID_FORMAT }. ${ help(tokens[0]) }` | |
17 | + } | |
18 | + | |
19 | + cb(null, null, responseParams); | |
20 | + return; | |
21 | + } | |
22 | + | |
23 | + const coreParams = { | |
24 | + terminal_name: params.from, | |
25 | + password: tokens[1], | |
26 | + }; | |
27 | + | |
28 | + coreapi(coreEndpoint, coreParams, 'GET', cb); | |
29 | +} | |
30 | + | |
31 | +module.exports = execute; | |
0 | 32 | \ No newline at end of file |
lib/coreapi/core-version.js
... | ... | @@ -0,0 +1,47 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const util = require('util'); | |
4 | +const naturalCompare = require('natural-compare-lite'); | |
5 | + | |
6 | +const logger = require('komodo-sdk/logger'); | |
7 | +const matrix = require('komodo-sdk/matrix'); | |
8 | + | |
9 | +const coreMatrixRequest = util.promisify(require('./matrix')); | |
10 | + | |
11 | +const MINIMUM_VERSION = 'v1.33.1-3-g2ce5525'; | |
12 | + | |
13 | +let _isSufficient = false; | |
14 | +matrix.core_version_is_sufficient = false; | |
15 | +matrix.core_version_requirement = MINIMUM_VERSION; | |
16 | + | |
17 | +function logIfDev(...args) { | |
18 | + matrix.NODE_ENV !== 'production' && logger.verbose(...args); | |
19 | +} | |
20 | + | |
21 | +async function getCoreVersion() { | |
22 | + | |
23 | + logIfDev('CORE-VERSION: checking core version'); | |
24 | + | |
25 | + const coreMatrix = await coreMatrixRequest(); | |
26 | + if (!coreMatrix || !coreMatrix.version_active) return; | |
27 | + | |
28 | + _isSufficient = naturalCompare(MINIMUM_VERSION, coreMatrix.version_active) <= 0; | |
29 | + matrix.core_version_is_sufficient = _isSufficient; | |
30 | + logger.info('CORE-VERSION', {version_active: coreMatrix.version_active, minimum_version: MINIMUM_VERSION, sufficient: _isSufficient}); | |
31 | + | |
32 | + if (!_isSufficient) { | |
33 | + setTimeout( | |
34 | + function() { | |
35 | + getCoreVersion(); | |
36 | + }, | |
37 | + 5000 | |
38 | + ) | |
39 | + } | |
40 | +} | |
41 | +getCoreVersion(); | |
42 | + | |
43 | +function isSufficient() { | |
44 | + return _isSufficient; | |
45 | +} | |
46 | + | |
47 | +module.exports = isSufficient(); | |
0 | 48 | \ No newline at end of file |
lib/coreapi/index.js
... | ... | @@ -0,0 +1,18 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const matrix = require('komodo-sdk/matrix'); | |
4 | +const logger = require('komodo-sdk/logger'); | |
5 | + | |
6 | +const coreApiRequest = require('./request'); | |
7 | +require('./core-version'); | |
8 | + | |
9 | + | |
10 | +function _coreApiRequest(...args) { | |
11 | + if (!matrix.core_version_is_sufficient) { | |
12 | + logger.info('Blocking request to CORE because of insufficient CORE version'); | |
13 | + return; | |
14 | + } | |
15 | + coreApiRequest(...args); | |
16 | +} | |
17 | + | |
18 | +module.exports = _coreApiRequest; | |
0 | 19 | \ No newline at end of file |
lib/coreapi/matrix.js
... | ... | @@ -0,0 +1,17 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const coreapiRequest = require('./request'); | |
4 | + | |
5 | +function get(cb) { | |
6 | + coreapiRequest('/services/matrix', null, 'GET', function(err, responseObject) { | |
7 | + if (err) { | |
8 | + cb(err); | |
9 | + return; | |
10 | + } | |
11 | + | |
12 | + cb(null, responseObject); | |
13 | + }); | |
14 | + | |
15 | +} | |
16 | + | |
17 | +module.exports = get; | |
0 | 18 | \ No newline at end of file |
lib/coreapi/request.js
... | ... | @@ -0,0 +1,57 @@ |
1 | +"use strict"; | |
2 | + | |
3 | +const request = require('request'); | |
4 | + | |
5 | +const coreUrl = require('komodo-sdk/core-url'); | |
6 | +const logger = require('komodo-sdk/logger'); | |
7 | +const commandError = require('../command-handler/error'); | |
8 | + | |
9 | +function execute(coreEndpoint, params, httpMethod, cb) { | |
10 | + | |
11 | + const requestOptions = { | |
12 | + url: coreUrl + coreEndpoint, | |
13 | + method: httpMethod || 'GET' | |
14 | + } | |
15 | + | |
16 | + if (requestOptions.method === 'GET' && params) { | |
17 | + requestOptions.qs = params; | |
18 | + } | |
19 | + else if (requestOptions.method === 'POST' && params) { | |
20 | + requestOptions.data = params; | |
21 | + } | |
22 | + | |
23 | + logger.verbose('COREAPI-REQUEST: Requesting to core', {url: requestOptions.url, http_method: httpMethod, params: params}); | |
24 | + | |
25 | + request(requestOptions, function(err, res, body) { | |
26 | + const responseParams = { | |
27 | + directResponse: true, | |
28 | + httpStatusCode: res ? res.statusCode : null, | |
29 | + body: body | |
30 | + } | |
31 | + | |
32 | + if (err) { | |
33 | + cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); | |
34 | + return; | |
35 | + } | |
36 | + | |
37 | + if (res.statusCode !== 200) { | |
38 | + cb(commandError.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE, null, responseParams); | |
39 | + return; | |
40 | + } | |
41 | + | |
42 | + try { | |
43 | + var coreResponseObject = JSON.parse(body); | |
44 | + } | |
45 | + catch(e) { | |
46 | + logger.verbose(commandError.ERR_INVALID_CORE_RESPONSE, {body: body}) | |
47 | + cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); | |
48 | + return; | |
49 | + } | |
50 | + | |
51 | + | |
52 | + cb(err, coreResponseObject, responseParams); | |
53 | + }) | |
54 | + | |
55 | +} | |
56 | + | |
57 | +module.exports = execute; | |
0 | 58 | \ No newline at end of file |
lib/default-command.js
... | ... | @@ -13,7 +13,7 @@ module.exports = { |
13 | 13 | "beli", |
14 | 14 | "i" |
15 | 15 | ], |
16 | - statuscheck: [ | |
16 | + _statuscheck: [ | |
17 | 17 | "status", |
18 | 18 | "cekstatus", |
19 | 19 | "checkstatus" |
... | ... | @@ -27,11 +27,11 @@ module.exports = { |
27 | 27 | "ceksaldo", |
28 | 28 | "checksaldo" |
29 | 29 | ], |
30 | - transferbalance: [ | |
30 | + _transferbalance: [ | |
31 | 31 | "transfer", |
32 | 32 | "trf" |
33 | 33 | ], |
34 | - addbalance: [ | |
34 | + _addbalance: [ | |
35 | 35 | "addbalance", |
36 | 36 | "addbal", |
37 | 37 | "tambahsaldo" |
... | ... | @@ -40,12 +40,39 @@ module.exports = { |
40 | 40 | "listdownline", |
41 | 41 | "listdl", |
42 | 42 | "downlinelist", |
43 | - "dllist" | |
43 | + "dllist", | |
44 | + "ldl" | |
44 | 45 | ], |
45 | - downlineinfo: [ | |
46 | - "downline" | |
46 | + _downlineinfo: [ | |
47 | + "downline", | |
48 | + "sd", | |
49 | + "saldodownline", | |
50 | + "mitra", | |
51 | + "saldomitra" | |
47 | 52 | ], |
48 | - daysummary: [ | |
53 | + _disabledownline: [ | |
54 | + "disabledownline", | |
55 | + "disabledl", | |
56 | + "disablemitra", | |
57 | + "nonaktifdownline", | |
58 | + "nonaktifdl", | |
59 | + "nonaktifmitra", | |
60 | + "nonaktifkandownline", | |
61 | + "nonaktifkandl", | |
62 | + "nonaktifkanmitra" | |
63 | + ], | |
64 | + _enabledownline: [ | |
65 | + "enabledownline", | |
66 | + "enabledl", | |
67 | + "enablemitra", | |
68 | + "aktivasidownline", | |
69 | + "aktivasidl", | |
70 | + "aktivasimitra", | |
71 | + "aktifkandownline", | |
72 | + "aktifkandl", | |
73 | + "aktifkanmitra" | |
74 | + ], | |
75 | + _daysummary: [ | |
49 | 76 | "rekap", |
50 | 77 | "laporan", |
51 | 78 | "lap" |
... | ... | @@ -60,21 +87,21 @@ module.exports = { |
60 | 87 | "hargaproduk", |
61 | 88 | "hargaproduct" |
62 | 89 | ], |
63 | - changepin: [ | |
90 | + _changepin: [ | |
64 | 91 | "pin", |
65 | 92 | "changepin", |
66 | 93 | "ubahpin", |
67 | 94 | "setpin", |
68 | 95 | "gantipin" |
69 | 96 | ], |
70 | - depositticket: [ | |
97 | + _depositticket: [ | |
71 | 98 | 'tiket', |
72 | 99 | 'tiketdeposit', |
73 | 100 | 'depo', |
74 | 101 | 'depoticket', |
75 | 102 | 'depositticket' |
76 | 103 | ], |
77 | - listterminal: [ | |
104 | + _listterminal: [ | |
78 | 105 | 'listterminal', |
79 | 106 | 'terminallist', |
80 | 107 | 'lsterminal', |
... | ... | @@ -87,7 +114,7 @@ module.exports = { |
87 | 114 | 'lsmsisdn', |
88 | 115 | 'msisdnls' |
89 | 116 | ], |
90 | - addterminal: [ | |
117 | + _addterminal: [ | |
91 | 118 | 'addterminal', |
92 | 119 | 'terminaladd', |
93 | 120 | 'tambahterminal', |
... | ... | @@ -100,7 +127,7 @@ module.exports = { |
100 | 127 | 'newmsisdn', |
101 | 128 | 'regmsisdn' |
102 | 129 | ], |
103 | - listdownlineterminal: [ | |
130 | + _listdownlineterminal: [ | |
104 | 131 | 'listdownlineterminal', |
105 | 132 | 'listdlterminal', |
106 | 133 | 'listterminaldownline', |
... | ... | @@ -112,7 +139,7 @@ module.exports = { |
112 | 139 | 'listmsisdndl', |
113 | 140 | 'listdlmsisdn' |
114 | 141 | ], |
115 | - adddownlineterminal: [ | |
142 | + _adddownlineterminal: [ | |
116 | 143 | 'adddownlineterminal', |
117 | 144 | 'adddlterminal', |
118 | 145 | 'addpartnerterminal', |
lib/http-listener.js
... | ... | @@ -58,5 +58,5 @@ app.get('/', mainHandler); |
58 | 58 | app.post('/', bodyParser.urlencoded({extended: true}), mainHandler); |
59 | 59 | |
60 | 60 | app.listen(port, function() { |
61 | - logger.info('HTTP listener started', {port: port, app_env: app.get('env')}); | |
61 | + logger.info('HTTP-LISTENER: started', {port: port, app_env: app.get('env')}); | |
62 | 62 | }); |
63 | 63 | \ No newline at end of file |
package-lock.json
1 | 1 | { |
2 | 2 | "name": "komodo-center-messaging", |
3 | - "version": "0.9.3", | |
3 | + "version": "0.9.4", | |
4 | 4 | "lockfileVersion": 1, |
5 | 5 | "requires": true, |
6 | 6 | "dependencies": { |
... | ... | @@ -2624,6 +2624,11 @@ |
2624 | 2624 | "integrity": "sha1-Sr6/7tdUHywnrPspvbvRXI1bpPc=", |
2625 | 2625 | "dev": true |
2626 | 2626 | }, |
2627 | + "natural-compare-lite": { | |
2628 | + "version": "1.4.0", | |
2629 | + "resolved": "https://registry.npmjs.org/natural-compare-lite/-/natural-compare-lite-1.4.0.tgz", | |
2630 | + "integrity": "sha1-F7CVgZiJef3a/gIB6TG6kzyWy7Q=" | |
2631 | + }, | |
2627 | 2632 | "negotiator": { |
2628 | 2633 | "version": "0.6.2", |
2629 | 2634 | "resolved": "https://registry.npmjs.org/negotiator/-/negotiator-0.6.2.tgz", |
package.json
1 | 1 | { |
2 | 2 | "name": "komodo-center-messaging", |
3 | - "version": "0.9.3", | |
3 | + "version": "0.9.4", | |
4 | 4 | "description": "Komodo Common Messaging Center", |
5 | 5 | "main": "index.js", |
6 | 6 | "scripts": { |
... | ... | @@ -23,6 +23,7 @@ |
23 | 23 | "express": "^4.17.1", |
24 | 24 | "express-ipfilter": "^1.0.1", |
25 | 25 | "komodo-sdk": "git+http://gitlab.kodesumber.com/komodo/komodo-sdk.git", |
26 | + "natural-compare-lite": "^1.4.0", | |
26 | 27 | "request": "^2.88.0", |
27 | 28 | "yargs": "^13.2.4" |
28 | 29 | }, |