Commit 046fb821ea8bf2680341f06885955f26c0c50504
1 parent
20edbc9666
Exists in
master
update split
Showing 1 changed file with 1 additions and 1 deletions Inline Diff
lib/actions/buy.js
1 | const MODULE_NAME = 'ACTIONS.BUY'; | 1 | const MODULE_NAME = 'ACTIONS.BUY'; |
2 | 2 | ||
3 | const logger = require('tektrans-logger'); | 3 | const logger = require('tektrans-logger'); |
4 | const axios = require('axios').default; | 4 | const axios = require('axios').default; |
5 | 5 | ||
6 | const config = require('../config'); | 6 | const config = require('../config'); |
7 | const configData = require('../config/data'); | 7 | const configData = require('../config/data'); |
8 | 8 | ||
9 | const buyToSDS = require('./buy-to-sds'); | 9 | const buyToSDS = require('./buy-to-sds'); |
10 | 10 | ||
11 | const client = axios.create({ | 11 | const client = axios.create({ |
12 | baseURL: config.core.url, | 12 | baseURL: config.core.url, |
13 | timeout: config.core.request_timeout_ms, | 13 | timeout: config.core.request_timeout_ms, |
14 | headers: { | 14 | headers: { |
15 | 'x-access-token': config.core.access_token, | 15 | 'x-access-token': config.core.access_token, |
16 | }, | 16 | }, |
17 | }); | 17 | }); |
18 | 18 | ||
19 | /** | 19 | /** |
20 | * Buy a product from supplier sds | 20 | * Buy a product from supplier sds |
21 | * | 21 | * |
22 | * @param {string} xid | 22 | * @param {string} xid |
23 | * @param {object} transaction | 23 | * @param {object} transaction |
24 | * | 24 | * |
25 | */ | 25 | */ |
26 | module.exports = async (xid, transaction) => { | 26 | module.exports = async (xid, transaction) => { |
27 | try { | 27 | try { |
28 | logger.verbose(`${MODULE_NAME} 4B139379: Buy product to sds`, { | 28 | logger.verbose(`${MODULE_NAME} 4B139379: Buy product to sds`, { |
29 | xid, | 29 | xid, |
30 | transaction, | 30 | transaction, |
31 | }); | 31 | }); |
32 | const iConfig = await configData.all(); | 32 | const iConfig = await configData.all(); |
33 | 33 | ||
34 | let productName = transaction.product_name; | 34 | let productName = transaction.product_name; |
35 | if ( | 35 | if ( |
36 | iConfig.products[transaction.product_name] | 36 | iConfig.products[transaction.product_name] |
37 | && iConfig.products[transaction.product_name].remote | 37 | && iConfig.products[transaction.product_name].remote |
38 | ) { | 38 | ) { |
39 | productName = iConfig.products[transaction.product_name].remote; | 39 | productName = iConfig.products[transaction.product_name].remote; |
40 | } | 40 | } |
41 | 41 | ||
42 | const productNames = productName.split(','); | 42 | const productNames = productName.trim().split(/\s*,\s*/); |
43 | // eslint-disable-next-line prefer-destructuring | 43 | // eslint-disable-next-line prefer-destructuring |
44 | let quantity = transaction.quantity; | 44 | let quantity = transaction.quantity; |
45 | 45 | ||
46 | if (productNames.length >= 2) { | 46 | if (productNames.length >= 2) { |
47 | [productName, quantity] = productNames; | 47 | [productName, quantity] = productNames; |
48 | } | 48 | } |
49 | 49 | ||
50 | // const callbackUrl = `${iConfig.url}:${iConfig.port}/apikey/${iConfig.apikey}/updates`; | 50 | // const callbackUrl = `${iConfig.url}:${iConfig.port}/apikey/${iConfig.apikey}/updates`; |
51 | const result = await buyToSDS( | 51 | const result = await buyToSDS( |
52 | xid, | 52 | xid, |
53 | `${transaction.request_id}-${transaction.id}`, | 53 | `${transaction.request_id}-${transaction.id}`, |
54 | transaction.destination, | 54 | transaction.destination, |
55 | productName, | 55 | productName, |
56 | quantity, | 56 | quantity, |
57 | ); | 57 | ); |
58 | logger.verbose(`${MODULE_NAME} 5BDFAF41: result from sds`, { | 58 | logger.verbose(`${MODULE_NAME} 5BDFAF41: result from sds`, { |
59 | xid, | 59 | xid, |
60 | trxId: transaction.id, | 60 | trxId: transaction.id, |
61 | result, | 61 | result, |
62 | }); | 62 | }); |
63 | let rc = '40'; | 63 | let rc = '40'; |
64 | if (result.code === '200') { | 64 | if (result.code === '200') { |
65 | rc = '68'; | 65 | rc = '68'; |
66 | } | 66 | } |
67 | const params = { | 67 | const params = { |
68 | id: transaction.id, | 68 | id: transaction.id, |
69 | rc, | 69 | rc, |
70 | amount: result.amount || null, | 70 | amount: result.amount || null, |
71 | message: result.text || '', | 71 | message: result.text || '', |
72 | sn: result.sn || null, | 72 | sn: result.sn || null, |
73 | }; | 73 | }; |
74 | 74 | ||
75 | await client.post('/transactions/gateway-update', params); | 75 | await client.post('/transactions/gateway-update', params); |
76 | } catch (e) { | 76 | } catch (e) { |
77 | logger.warn(`${MODULE_NAME} E9887C98: Exception`, { | 77 | logger.warn(`${MODULE_NAME} E9887C98: Exception`, { |
78 | xid, | 78 | xid, |
79 | message: e.message, | 79 | message: e.message, |
80 | code: e.code, | 80 | code: e.code, |
81 | }); | 81 | }); |
82 | } | 82 | } |
83 | }; | 83 | }; |
84 | 84 |