index.js 2.21 KB
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);