modems.js 3.37 KB
/* eslint-disable no-console */
const DEBUG = !!process.env.DEBUG_KOMODO_CENTER_CP_EVO_MODEMS;

const config = require('komodo-sdk/config');

exports.modemsDictionary = (arrayOfModem) => {
    const modems = arrayOfModem || config.modems || [];
    const retval = {};
    modems.forEach((modem) => {
        retval[modem.name] = modem;
    });
    return retval;
};

/**
 * deprecated
 */
exports.enabledModems = (arrayOfModem) => {
    const modems = arrayOfModem || config.modems || [];
    return modems.filter((modem) => !modem.disabled);
};

exports.outgoingModems = (arrayOfModem) => {
    const modems = arrayOfModem || config.modems || [];
    return modems.filter((modem) => modem.outgoing);
};

exports.OUTGOING_MODEMS = this.outgoingModems();

exports.prefixes = (arrayOfModem) => {
    const prefixModemsObject = {};
    const prefixModemsArray = [];

    const modems = this.outgoingModems(arrayOfModem);
    modems.forEach((modem) => {
        const modemPrefixes = modem.prefix || [];
        modemPrefixes.forEach((prefix) => {
            const normalizedPrefix = prefix.trim().replace(/^0/, '62');

            if (!prefixModemsObject[normalizedPrefix]) {
                prefixModemsObject[normalizedPrefix] = [modem.name];
            } else {
                prefixModemsObject[normalizedPrefix].push(modem.name);
            }
        });
    });

    // eslint-disable-next-line no-restricted-syntax
    for (const [prefix, value] of Object.entries(prefixModemsObject)) {
        prefixModemsArray.push({ prefix, modems: value });
    }

    const retval = prefixModemsArray.sort((a, b) => {
        if (a.prefix.length < b.prefix.length) return 1;
        if (a.prefix.length > b.prefix.length) return -1;

        if (a < b) return -1;
        if (a > b) return 1;
        return 0;
    });

    retval.forEach((item) => {
        // eslint-disable-next-line no-param-reassign
        item.modems = item.modems.sort((a, b) => {
            if (a < b) return -1;
            if (a > b) return 1;
            return 0;
        });
    });

    return retval;
};

exports.randomModem = (arrayOfModem) => {
    const modems = this.outgoingModems(arrayOfModem);

    if (DEBUG) {
        console.log('DEBUG_MODEMS: Get random modems', {
            haystack: modems,
        });
    }
    const modemCount = modems.length;

    if (!modemCount) return null;

    const idx = Math.floor(Math.random() * modemCount);
    return modems[idx];
};

exports.findPrefixForNumber = (number, prefixes) => prefixes.find(
    (item) => number.replace(/^0/, '62').indexOf(item.prefix) === 0,
);

exports.isFreePrefixModem = (modem) => !modem.prefix || !modem.prefix.length;

exports.randomModemByPrefix = (number, arrayOfModem) => {
    const modems = this.outgoingModems(arrayOfModem);
    const prefixes = this.prefixes(arrayOfModem);
    const prefix = this.findPrefixForNumber(number, prefixes);
    const modemNamesForPrefix = (prefix || {}).modems;

    if (modemNamesForPrefix && modemNamesForPrefix.length) {
        const count = modemNamesForPrefix.length;
        const idx = Math.floor(Math.random() * count);
        const modemName = modemNamesForPrefix[idx];

        return modems.find((modem) => modem.name === modemName);
    }

    const freeModems = modems.filter((modem) => this.isFreePrefixModem(modem));
    const count = freeModems.length;
    const idx = Math.floor(Math.random() * count);
    return freeModems[idx];
};