sms-handler.js
2.27 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
"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;