Commit 333bf512180a7b137562cfaa2b658483681c61d3
1 parent
4cdaea2ef4
Exists in
master
must_have_pattern_flags
Showing 1 changed file with 2 additions and 4 deletions Inline Diff
lib/sms-handler.js
1 | "use strict"; | 1 | "use strict"; |
2 | 2 | ||
3 | const config = require('komodo-sdk/config'); | 3 | const config = require('komodo-sdk/config'); |
4 | const logger = require('komodo-sdk/logger'); | 4 | const logger = require('komodo-sdk/logger'); |
5 | 5 | ||
6 | const patternMatcher = require('./pattern-rule-matcher'); | 6 | const patternMatcher = require('./pattern-rule-matcher'); |
7 | const patternValue = require('./pattern-rule-value'); | 7 | const patternValue = require('./pattern-rule-value'); |
8 | 8 | ||
9 | function isAllowedSender(sender) { | 9 | function isAllowedSender(sender) { |
10 | if (!config || !config.sms_parser || !config.sms_parser.allowed_sender || !Array.isArray(config.sms_parser.allowed_sender)) return; | 10 | if (!config || !config.sms_parser || !config.sms_parser.allowed_sender || !Array.isArray(config.sms_parser.allowed_sender)) return; |
11 | return config.sms_parser.allowed_sender.indexOf(sender) >= 0; | 11 | return config.sms_parser.allowed_sender.indexOf(sender) >= 0; |
12 | } | 12 | } |
13 | 13 | ||
14 | function getDestination(msg) { | 14 | function getDestination(msg) { |
15 | const result = patternMatcher(msg, config.sms_parser.destination); | 15 | const result = patternMatcher(msg, config.sms_parser.destination); |
16 | if (result && typeof result === 'string') { | 16 | if (result && typeof result === 'string') { |
17 | return result.replace(/^62/, '0').replace(/^8/, '08'); | 17 | return result.replace(/^62/, '0').replace(/^8/, '08'); |
18 | } | 18 | } |
19 | } | 19 | } |
20 | 20 | ||
21 | function getProduct(msg) { | 21 | function getProduct(msg) { |
22 | const product_on_msg = patternMatcher(msg, config.sms_parser.product); | 22 | const product_on_msg = patternMatcher(msg, config.sms_parser.product); |
23 | return product_on_msg && config.remote_product_alias ? config.remote_product_alias[product_on_msg] || product_on_msg : product_on_msg || null; | 23 | return product_on_msg && config.remote_product_alias ? config.remote_product_alias[product_on_msg] || product_on_msg : product_on_msg || null; |
24 | } | 24 | } |
25 | 25 | ||
26 | function getTrxDate(msg) { | 26 | function getTrxDate(msg) { |
27 | return patternMatcher(msg, config.sms_parser.trx_date); | 27 | return patternMatcher(msg, config.sms_parser.trx_date); |
28 | } | 28 | } |
29 | 29 | ||
30 | function getSn(msg) { | 30 | function getSn(msg) { |
31 | return patternMatcher(msg, config.sms_parser.sn); | 31 | return patternMatcher(msg, config.sms_parser.sn); |
32 | } | 32 | } |
33 | 33 | ||
34 | function getRc(msg) { | 34 | function getRc(msg) { |
35 | return patternValue(msg, config.sms_parser.rc) || '68'; | 35 | return patternValue(msg, config.sms_parser.rc) || '68'; |
36 | } | 36 | } |
37 | 37 | ||
38 | function getStockBalance(msg) { | 38 | function getStockBalance(msg) { |
39 | return { | 39 | return { |
40 | name: patternMatcher(msg, config.sms_parser.stock.product), | 40 | name: patternMatcher(msg, config.sms_parser.stock.product), |
41 | balance: patternMatcher(msg, config.sms_parser.stock.balance) | 41 | balance: patternMatcher(msg, config.sms_parser.stock.balance) |
42 | } | 42 | } |
43 | } | 43 | } |
44 | 44 | ||
45 | function getMultiStockBalance(msg) { | 45 | function getMultiStockBalance(msg) { |
46 | if (!config || !config.sms_parser || !config.sms_parser.stock || !config.sms_parser.stock.multistock) return; | 46 | if (!config || !config.sms_parser || !config.sms_parser.stock || !config.sms_parser.stock.multistock) return; |
47 | 47 | ||
48 | if (config.sms_parser.stock.multistock.must_have_pattern) { | 48 | if (config.sms_parser.stock.multistock.must_have_pattern) { |
49 | const re = new RegExp(config.sms_parser.stock.multistock.must_have_pattern) | 49 | const re = new RegExp(config.sms_parser.stock.multistock.must_have_pattern, config.sms_parser.stock.multistock.must_have_pattern_flags); |
50 | if (msg.search(re) < 0) { | 50 | if (msg.search(re) < 0) { return; } |
51 | return; | ||
52 | } | ||
53 | } | 51 | } |
54 | 52 | ||
55 | const re = new RegExp(config.sms_parser.stock.multistock.pattern, config.sms_parser.stock.multistock.flags); | 53 | const re = new RegExp(config.sms_parser.stock.multistock.pattern, config.sms_parser.stock.multistock.flags); |
56 | const stocks = msg.match(re); | 54 | const stocks = msg.match(re); |
57 | logger.verbose('SMS-HANDLER: Got multistock', {stocks: stocks}); | 55 | logger.verbose('SMS-HANDLER: Got multistock', {stocks: stocks}); |
58 | return stocks; | 56 | return stocks; |
59 | } | 57 | } |
60 | 58 | ||
61 | exports.isAllowedSender = isAllowedSender; | 59 | exports.isAllowedSender = isAllowedSender; |
62 | exports.getDestination = getDestination; | 60 | exports.getDestination = getDestination; |
63 | exports.getProduct = getProduct; | 61 | exports.getProduct = getProduct; |
64 | exports.getTrxDate = getTrxDate; | 62 | exports.getTrxDate = getTrxDate; |
65 | exports.getSn = getSn; | 63 | exports.getSn = getSn; |
66 | exports.getRc = getRc; | 64 | exports.getRc = getRc; |
67 | exports.getStockBalance = getStockBalance; | 65 | exports.getStockBalance = getStockBalance; |
68 | exports.getMultiStockBalance = getMultiStockBalance; | 66 | exports.getMultiStockBalance = getMultiStockBalance; |
69 | 67 |