index.js
2.21 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
76
77
78
79
const MODULE_NAME = 'HTTP-SERVER.ROUTER.UPDATES';
const express = require('express');
const axios = require('axios').default;
const logger = require('tektrans-logger');
const config = require('../../../config');
const router = express.Router();
module.exports = router;
const client = axios.create({
baseURL: config.core.url,
timeout: config.core.request_timeout_ms,
headers: {
'x-access-token': config.core.access_token,
},
});
const pageUpdate = async (req, res) => {
const { xid } = res.locals;
let data = req.query;
if (req.method.toUpperCase() !== 'GET') {
data = req.body;
}
try {
logger.verbose(`${MODULE_NAME} 9E5C70C8: update from sds`, { xid, data });
const requestIds = data.trxid.split('-');
let trxId = '';
if (Array.isArray(requestIds) && requestIds.length > 0) {
trxId = requestIds[requestIds.length - 1];
}
let rc = '68';
if (data.status === 'S') {
rc = '00';
} else if (data.status === 'F') {
rc = '40';
} else if (data.status === 'R') {
rc = '47';
}
const params = {
id: trxId,
rc,
message: data.message,
sn: data.sn || null,
related_data: JSON.stringify(data),
};
client.post('/transactions/gateway-update', params).then((result) => {
logger.verbose(`${MODULE_NAME} A8DA0D04: response from core2`, {
xid,
response: result.data,
});
}).catch((err) => {
logger.warn(`${MODULE_NAME} 32EB485C: Exception on request to core2`, {
xid,
eMessage: err.message,
eCode: err.code,
});
});
res.json({ error: false, message: 'OK' });
} catch (e) {
logger.warn(`${MODULE_NAME} B12B4A2C: Exception.`, {
xid, eMessage: e.message, eCode: e.code,
});
res.status(500).json({
error: true,
error_code: e.code,
message: e.message,
});
}
};
router.get('/', pageUpdate);
router.post('/', [express.urlencoded({ extended: true }), express.json()], pageUpdate);