router-config-senders.js 6.14 KB
'use strict';

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

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

const locks = require('locks');
const mutexSave = locks.createMutex();

const router = express.Router();
module.exports = router;

function saveConfig() {
    return new Promise((resolve) => {
        mutexSave.lock(() => {
            fs.writeFile(global.KOMODO_SDK_CONFIG_FILENAME, JSON.stringify(config, null, 4), (err) => {
                if (err) {
                    logger.warn(`APISERVER: ERROR writing config file. ${err.toString()}`);
                }
                mutexSave.unlock();
                resolve();
            });
        });
    });
}

function initConfig(req, res, next) {
    if (typeof config.senders_imsi !== 'object') {
        config.senders_imsi = {
            prefix_names: {},
            unknown_prefix: [],
            default: [],
        };
    }

    if (typeof config.senders_imsi.prefix_names !== 'object') {
        config.senders_imsi.prefix_names = {};
    }

    if (!Array.isArray(config.senders_imsi.unknown_prefix)) {
        config.senders_imsi.unknown_prefix = [];
    }

    if (!Array.isArray(config.senders_imsi.default)) {
        config.senders_imsi.default = [];
    }

    next();
}

function pageIndex(req, res) {
    res.json(config.senders_imsi);
}

async function delImsi(data, imsi, cleanup) {
    const idx = data.indexOf(imsi);
    if (idx < 0) return;

    data.splice(idx, 1);

    if (cleanup) {
        Object.keys(config.senders_imsi.prefix_names).forEach((key) => {
            if (!config.senders_imsi.prefix_names[key]) return;
    
            if (!Array.isArray(config.senders_imsi.prefix_names[key])) return;
    
            if (config.senders_imsi.prefix_names[key].length === 0) {
                delete config.senders_imsi.prefix_names[key];
            }
        });
    }

    await saveConfig();
}

async function addImsi(data, imsi) {
    if (data.indexOf(imsi) >= 0) {
        return false;
    }

    data.push(imsi);
    await saveConfig();

    return true;
}

function addImsiResponse(req, res) {
    if (res.locals.add_imsi_success) {
        res.json({
            status: 'OK',
            message: 'IMSI berhasil didaftarkan',
        });
    } else  {
        res.json({
            status: 'NOT OK',
            message: 'IMSI sudah terdaftar',
        });
    }
}

/**
 * Menambahkan IMSI pada list sender by prefix
 *
 * @param  {object} req - Express request object
 * @param  {object} req.query - query string object
 * @param  {string} req.query.prefix - nama prefix
 * @param  {string} req.query.imsi - nomor imsi
 * @param  {object} res - Express result object
 */
async function pageAddForPrefix(req, res, next) {
    if (!req.query.prefix) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined prefix',
        });

        return;
    }
    
    if (!req.query.imsi) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined IMSI',
        });

        return;
    }

    req.query.prefix = req.query.prefix.toUpperCase();
    
    if (!config.senders_imsi.prefix_names[req.query.prefix]) {
        config.senders_imsi.prefix_names[req.query.prefix] = [];
    }

    res.locals.add_imsi_success = await addImsi(config.senders_imsi.prefix_names[req.query.prefix], req.query.imsi);
    next();
}

async function pageDelForPrefix(req, res) {
    if (!req.query.prefix) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined prefix',
        });

        return;
    }
    
    if (!req.query.imsi) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined IMSI',
        });

        return;
    }

    await delImsi(config.senders_imsi.prefix_names[req.query.prefix], req.query.imsi, true);
    
    res.json({
        status: 'OK',
        message: 'IMSI berhasil dihapus',
    });
}

/**
 * Menambahkan IMSI pada list sender untuk prefix tidak dikenal
 *
 * @param  {object} req - Express request object
 * @param  {object} req.query - query string object
 * @param  {string} req.query.imsi - nomor imsi
 * @param  {object} res - Express result object
 */
async function pageAddForUnknownPrefix(req, res, next) {
    if (!req.query.imsi) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined IMSI',
        });

        return;
    }

    res.locals.add_imsi_success = await addImsi(config.senders_imsi.unknown_prefix, req.query.imsi);
    next();
}

async function pageDelForUnknownPrefix(req, res) {
    if (!req.query.imsi) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined IMSI',
        });

        return;
    }

    await delImsi(config.senders_imsi.unknown_prefix, req.query.imsi);
    res.json({
        status: 'OK',
        message: 'IMSI berhasil dihapus',
    });
}

/**
 * Menambahkan IMSI pada list sender default
 *
 * @param  {object} req - Express request object
 * @param  {object} req.query - query string object
 * @param  {string} req.query.imsi - nomor imsi
 * @param  {object} res - Express result object
 */
async function pageAddForDefault(req, res, next) {
    if (!req.query.imsi) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined IMSI',
        });

        return;
    }

    res.locals.add_imsi_success = await addImsi(config.senders_imsi.default, req.query.imsi);
    next();
}

async function pageDelForDefault(req, res) {
    if (!req.query.imsi) {
        res.json({
            status: 'NOT-OK',
            message: 'Undefined IMSI',
        });

        return;
    }

    await delImsi(config.senders_imsi.default, req.query.imsi);
    res.json({
        status: 'OK',
        message: 'IMSI berhasil dihapus',
    });
}

router.use(initConfig);
router.get('/', initConfig, pageIndex);

router.get('/add-for-prefix', pageAddForPrefix, addImsiResponse);
router.get('/add-for-unknown-prefix', pageAddForUnknownPrefix, addImsiResponse);
router.get('/add-for-default', pageAddForDefault, addImsiResponse)

router.get('/del-for-prefix', pageDelForPrefix);
router.get('/del-for-unknown-prefix', pageDelForUnknownPrefix);
router.get('/del-for-default', pageDelForDefault);