neoxmlinutil.js
2.48 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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
var oracledb = require('oracledb');
var LRU = require('lru-cache');
var config = require("./config.json");
var logger = console;
var oraCon;
var reverseUrlCache = LRU({max: 20, maxAge: 60 * 1000});
function init(options) {
if (options && options.logger) {
logger = options.logger;
}
}
function connectToOracle(callback) {
oracledb.getConnection(config.oracle, function(err, connection) {
if (err) {
logger.warn('Can not connect to oracle db: ' + err);
process.exit(1);
}
oraCon = connection;
logger.info("Oracle db connected")
if (callback) {
callback(null, oraCon);
}
})
}
function getTransactionIdFromMessage(message) {
try {
var matches = message.match(/ID=(\d+)/);
return matches[1];
}
catch(err) {
return '0';
}
}
function getReverseUrlFromDb(msisdn, callback) {
oraCon.execute(
"SELECT trim(A.MSISDN) MSISDN, trim(B.REVERSE_URL) REVERSE_URL FROM T_STORE_USER_MSISDN A, T_STORE_USER B WHERE A.STORE_ID = B.STORE_ID AND A.USER_NAME = B.USER_NAME AND B.REVERSE_URL IS NOT NULL AND A.MSISDN = :msisdn",
[msisdn],
function(err, result) {
if (err) {
logger.warn('Error retrieving reverse url from oracle: ' + err);
callback(err);
return;
}
var rowCount = result.rows.length;
var retval = [];
for (var i = 0; i < rowCount; i++) {
retval.push(result.rows[i][1]);
}
if (retval.length) {
reverseUrlCache.set(msisdn, retval);
}
callback(null, retval);
}
);
}
function getReverseUrl(msisdn, callback) {
var urls = reverseUrlCache.get(msisdn);
if (!urls) {
logger.verbose('Cache Miss on reverse url for ' + msisdn, '. Trying to get from DB.');
getReverseUrlFromDb(msisdn, callback);
} else {
//logger.verbose('Got cache hit on reverse url for ' + msisdn);
callback(null, urls);
}
}
function getRequestIdFromResponseMessage(message) {
try {
var tokens = message.split('.');
return tokens[4];
}
catch(err) {
return;
}
}
exports.init = init;
exports.connectToOracle = connectToOracle;
exports.getReverseUrl = getReverseUrl;
exports.getTransactionIdFromMessage = getTransactionIdFromMessage;
exports.getRequestIdFromResponseMessage = getRequestIdFromResponseMessage;