Compare View
Commits (2)
Changes
Showing 4 changed files Side-by-side Diff
lib/command-handler/buy.js
1 | 1 | // const moment = require('moment'); |
2 | 2 | |
3 | +const config = require('komodo-sdk/config'); | |
3 | 4 | const logger = require('komodo-sdk/logger'); |
4 | 5 | |
5 | 6 | const commands = require('../command-group'); |
... | ... | @@ -7,7 +8,8 @@ const commandError = require('./error'); |
7 | 8 | const destinationCorrector = require('../destination-corrector'); |
8 | 9 | const coreapi = require('../coreapi'); |
9 | 10 | |
10 | -const coreEndpoint = '/prepaid/buy'; | |
11 | +const coreEndpointAuto = '/buy-or-pay'; | |
12 | +const coreEndpointBuy = '/prepaid/buy'; | |
11 | 13 | |
12 | 14 | /* |
13 | 15 | function generateRequestId(product, destination) { |
... | ... | @@ -49,6 +51,7 @@ function execute(tokens, params, cb) { |
49 | 51 | postpaid: 0 |
50 | 52 | }; |
51 | 53 | |
54 | + const coreEndpoint = config.buy_only_prepaid ? coreEndpointBuy : coreEndpointAuto; | |
52 | 55 | coreapi(coreEndpoint, coreParams, 'GET', cb); |
53 | 56 | } |
54 | 57 |
lib/coreapi/request.js
1 | 1 | // const PRODUCTION = process.env.NODE_ENV === 'production'; |
2 | 2 | |
3 | 3 | const request = require('request'); |
4 | +const uniqid = require('uniqid'); | |
4 | 5 | |
5 | 6 | const coreUrl = require('komodo-sdk/core-url'); |
6 | 7 | const logger = require('komodo-sdk/logger'); |
7 | 8 | const commandError = require('../command-handler/error'); |
8 | 9 | |
9 | 10 | function execute(coreEndpoint, params, httpMethod, cb) { |
11 | + const xid = uniqid(); | |
10 | 12 | |
11 | 13 | const requestOptions = { |
12 | 14 | url: coreUrl + coreEndpoint, |
... | ... | @@ -20,7 +22,9 @@ function execute(coreEndpoint, params, httpMethod, cb) { |
20 | 22 | requestOptions.data = params; |
21 | 23 | } |
22 | 24 | |
23 | - logger.verbose('COREAPI-REQUEST: Requesting to core', {url: requestOptions.url, http_method: httpMethod, params: params}); | |
25 | + logger.verbose('COREAPI-REQUEST: Requesting to core', { | |
26 | + xid, url: requestOptions.url, http_method: httpMethod, params: params, | |
27 | + }); | |
24 | 28 | |
25 | 29 | request(requestOptions, function(err, res, body) { |
26 | 30 | const responseParams = { |
... | ... | @@ -30,11 +34,18 @@ function execute(coreEndpoint, params, httpMethod, cb) { |
30 | 34 | } |
31 | 35 | |
32 | 36 | if (err) { |
37 | + logger.warn('ERROR on requesting to CORE', { | |
38 | + xid, eCode: err.code, eMessage: err.message, | |
39 | + }); | |
33 | 40 | cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); |
34 | 41 | return; |
35 | 42 | } |
36 | 43 | |
37 | 44 | if (res.statusCode !== 200) { |
45 | + logger.warn('Invalid HTTP status from CORE', { | |
46 | + xid, httpStatus: res.statusCode, | |
47 | + }); | |
48 | + | |
38 | 49 | cb(commandError.ERR_INVALID_CORE_HTTP_STATUS_RESPONSE, null, responseParams); |
39 | 50 | return; |
40 | 51 | } |
... | ... | @@ -43,14 +54,20 @@ function execute(coreEndpoint, params, httpMethod, cb) { |
43 | 54 | var coreResponseObject = JSON.parse(body); |
44 | 55 | } |
45 | 56 | catch(e) { |
46 | - logger.verbose(commandError.ERR_INVALID_CORE_RESPONSE, {body: body}) | |
57 | + logger.warn(commandError.ERR_INVALID_CORE_RESPONSE, { | |
58 | + xid, eCode: e.code, eMessage: e.message, body: body | |
59 | + }); | |
47 | 60 | cb(commandError.ERR_INVALID_CORE_RESPONSE, null, responseParams); |
48 | 61 | return; |
49 | 62 | } |
50 | 63 | |
64 | + logger.verbose('Got CORE response', { | |
65 | + xid, coreResponseObject, | |
66 | + }); | |
67 | + | |
51 | 68 | cb(err, coreResponseObject, responseParams); |
52 | 69 | }) |
53 | - | |
70 | + | |
54 | 71 | } |
55 | 72 | |
56 | 73 | module.exports = execute; |
57 | 74 | \ No newline at end of file |
package-lock.json