on-received.js 2.15 KB
const DEFAULT_MAX_MESSAGE_AGE_IN_SECS = 300;

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

const messagingCenter = require('komodo-center-messaging-client-lib');
const modems = require('../../modems');

const blacklistMsgPatterns = [
    /^TRANSAKSI KE NOMOR/,
    /^Kesalahan autentikasi/,
    /^Saldo anda/,
    /^Perintah salah/,
];

const maxMessageAgeInSeconds = config.max_message_age_in_secs
        || DEFAULT_MAX_MESSAGE_AGE_IN_SECS;

module.exports = (req, res) => {
    res.end('OK');

    const {
        msg, msisdn, smsc, ts,
    } = req.query;

    if (!(msg || '').trim()) {
        logger.verbose('ON-RECEIVED B09186E3: Skip empty message', {
            qs: req.query,
        });

        return;
    }

    const msgUpperCased = (msg || '').trim().toUpperCase();
    if (
        (msisdn.search(/\+*62/) === 0)
        && blacklistMsgPatterns.find((pattern) => msgUpperCased.search(pattern) >= 0)
    ) {
        logger.verbose('ON-RECEIVED 5486D373: Skip invalid message', {
            msg,
            msisdn,
            qs: req.query,
        });

        return;
    }

    const sentTime = moment(ts, 'YYYY-MM-DD HH:mm:ss');
    const messageAgeInSeconds = Math.round(Number(new Date() - sentTime) / 1000);

    if (messageAgeInSeconds > maxMessageAgeInSeconds) {
        logger.warn('ON-RECEIVED 2247F406: Got expired message from EVO-CP', {
            qs: req.query,
            messageAgeInSeconds,
            maxMessageAgeInSeconds,
        });

        return;
    }

    logger.info('ON-RECEIVED D3747E3C: Got message from EVO-CP', {
        messageAgeInSeconds,
        qs: req.query,
    });

    const modem = modems.modemsDictionary[smsc] || {};

    const doNotForwardToCore = (msisdn.search(/(\+)*62/) !== 0) || (msisdn.length <= 8);

    messagingCenter.onIncomingMessage({
        me: modem.imsi || smsc,
        partner: `${msisdn}${config.number_suffix || ''}`,
        partner_raw: msisdn,
        msg,
        origin_label: modem.imsi || smsc,
        origin_transport: 'SMS',
        origin_partner: msisdn,
        do_not_forward_to_core: doNotForwardToCore,
    });
};