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