index.js 1.3 KB
const MODULE_NAME = 'TRANSLATE-RC';

const fs = require('fs');
const path = require('path');

const logger = require('../logger');

const dictDefault = require('./rcs');

const dictCustomFilename = path.join(process.cwd(), 'rc-local.json');
const dictCustomFileExists = fs.existsSync(dictCustomFilename);

// eslint-disable-next-line import/no-dynamic-require
const dictCustom = dictCustomFileExists ? require(dictCustomFilename) : null;

if (dictCustomFileExists) {
    logger.verbose(`${MODULE_NAME} 14F61E23: Custom RC dictionary found`, {
        fileName: dictCustomFilename,
        content: dictCustom,
    });
}

module.exports = (xid, rcFromPartner) => {
    if (!rcFromPartner) {
        logger.verbose(`${MODULE_NAME} 030CFC8F: Unknown rcFromPartner`, { xid });
        return '68';
    }

    logger.verbose(`${MODULE_NAME} 9EDC60A9: Translating RC from partner`, { xid, rcFromPartner });

    if (dictCustom) {
        logger.verbose(`${MODULE_NAME} 3A495EC2: Using rc-local.json`, { xid });
        return dictCustom[rcFromPartner] || '40';
    }

    if (dictDefault[rcFromPartner]) {
        logger.verbose(`${MODULE_NAME} D1B588D9: Found on default dict`, { xid });
        return dictDefault[rcFromPartner];
    }

    logger.verbose(`${MODULE_NAME} 61A6ADCE: Using default rc (40)`, { xid });
    return '40';
};