data.js 2.85 KB
const MODULE_NAME = 'CONFIG.DATA';

const fs = require('fs/promises');
const logger = require('tektrans-logger');

let config = {};

/**
 * Get all config
 *
 * @param {string} xid
 *
 * @returns <object>
 */
async function all(xid) {
    try {
        if (Object.keys(config).length === 0) {
            const configString = await fs.readFile('config.json');
            config = JSON.parse(configString);
        }
    } catch (e) {
        logger.warn(`${MODULE_NAME} ED8D390C: Exception`, {
            xid,
            eMessage: e.message,
            eCode: e.code,
        });
    }
    return config;
}

/**
 * Get active product prepaid config
 *
 * @param {string} xid
 *
 * @returns <Array>
 */
async function getActiveProductArray(xid) {
    try {
        if (Object.keys(config).length === 0) {
            const configString = await fs.readFile('config.json');
            config = JSON.parse(configString);
        }
        const allProductsArray = Object.values(config.products);
        if (Array.isArray(allProductsArray)) {
            const activeProductArray = allProductsArray.filter((val) => val.active === 1);
            const activeProductNameArray = activeProductArray.map((val) => val.name);
            return activeProductNameArray;
        }
    } catch (e) {
        logger.warn(`${MODULE_NAME} 2D49D035: Exception`, {
            xid,
            eMessage: e.message,
            eCode: e.code,
        });
    }
    return [];
}

/**
 * Get active product postpaid config
 *
 * @param {string} xid
 *
 * @returns <Array>
 */
async function getActiveProductPostpaidArray(xid) {
    try {
        if (Object.keys(config).length === 0) {
            const configString = await fs.readFile('config.json');
            config = JSON.parse(configString);
        }
        const allProductsArray = Object.values(config.postpaid_products);
        if (Array.isArray(allProductsArray)) {
            const activeProductArray = allProductsArray.filter((val) => val.active === 1);
            const activeProductNameArray = activeProductArray.map((val) => val.name);
            return activeProductNameArray;
        }
    } catch (e) {
        logger.warn(`${MODULE_NAME} B5414DB0: Exception`, {
            xid,
            eMessage: e.message,
            eCode: e.code,
        });
    }
    return [];
}

/**
 * Reload config
 *
 * @param {string} xid
 *
 * @returns <object>
 */
async function reload(xid) {
    try {
        const configString = await fs.readFile('config.json');
        config = JSON.parse(configString);
    } catch (e) {
        logger.warn(`${MODULE_NAME} D005AED3: Exception`, {
            xid,
            eMessage: e.message,
            eCode: e.code,
        });
    }
    return config;
}

exports.all = all;
exports.reload = reload;
exports.getActiveProductArray = getActiveProductArray;
exports.getActiveProductPostpaidArray = getActiveProductPostpaidArray;