sms-handler.js 2.27 KB
"use strict";

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

const patternMatcher = require('./pattern-rule-matcher');
const patternValue = require('./pattern-rule-value');

function isAllowedSender(sender) {
    if (!config || !config.sms_parser || !config.sms_parser.allowed_sender || !Array.isArray(config.sms_parser.allowed_sender)) return;
    return config.sms_parser.allowed_sender.indexOf(sender) >= 0;
}

function getDestination(msg) {
    const result = patternMatcher(msg, config.sms_parser.destination);
    if (result && typeof result === 'string') {
        return result.replace(/^62/, '0').replace(/^8/, '08');
    }
}

function getProduct(msg) {
    const product_on_msg = patternMatcher(msg, config.sms_parser.product);
    return product_on_msg && config.remote_product_alias ? config.remote_product_alias[product_on_msg] || product_on_msg : product_on_msg || null;
}

function getTrxDate(msg) {
    return patternMatcher(msg, config.sms_parser.trx_date);
}

function getSn(msg) {
    return patternMatcher(msg, config.sms_parser.sn);
}

function getRc(msg) {
    return patternValue(msg, config.sms_parser.rc) || '68';
}

function getStockBalance(msg) {
    return {
        name: patternMatcher(msg, config.sms_parser.stock.product),
        balance: patternMatcher(msg, config.sms_parser.stock.balance)
    }
}

function getMultiStockBalance(msg) {
    if (!config || !config.sms_parser || !config.sms_parser.stock || !config.sms_parser.stock.multistock) return;

    if (config.sms_parser.stock.multistock.must_have_pattern) {
        const re = new RegExp(config.sms_parser.stock.multistock.must_have_pattern, config.sms_parser.stock.multistock.must_have_pattern_flags);
        if (msg.search(re) < 0) { return; }
    }

    const re = new RegExp(config.sms_parser.stock.multistock.pattern, config.sms_parser.stock.multistock.flags);
    const stocks = msg.match(re);
    logger.verbose('SMS-HANDLER: Got multistock', {stocks: stocks});
    return stocks;
}

exports.isAllowedSender = isAllowedSender;
exports.getDestination = getDestination;
exports.getProduct = getProduct;
exports.getTrxDate = getTrxDate;
exports.getSn = getSn;
exports.getRc = getRc;
exports.getStockBalance = getStockBalance;
exports.getMultiStockBalance = getMultiStockBalance;