Commit d2a493f07b5d0e1cc7671014a19884c20b54e4c1

Authored by Adhidarma Hadiwinoto
1 parent 64bb83404f
Exists in master

pushResponseToMongoDb with st24 rc

Showing 1 changed file with 40 additions and 37 deletions Side-by-side Diff

... ... @@ -22,11 +22,11 @@ var mongodb;
22 22 function start(_config, _callbackReport, options) {
23 23 config = _config;
24 24 callbackReport = _callbackReport
25   -
  25 +
26 26 if (options && options.aaa) {
27 27 aaa = options.aaa;
28 28 }
29   -
  29 +
30 30 if (options && options.logger) {
31 31 logger = options.logger;
32 32 } else {
... ... @@ -36,7 +36,7 @@ function start(_config, _callbackReport, options) {
36 36 ]
37 37 });
38 38 }
39   -
  39 +
40 40 createXMLRPCServer();
41 41 initMongoClient();
42 42 }
... ... @@ -55,25 +55,26 @@ function createXMLRPCServer() {
55 55 });
56 56  
57 57 server.on('topUpReport', function (err, params, callback) {
58   -
  58 +
59 59 logger.info('Got XMLRPC topUpReport request from partner', {xmlrpc_method: 'topUpReport', xmlrpc_params: params});
60   -
  60 +
61 61 var paramscount = params.length;
62 62 for (var i = 0; i < paramscount; i++) {
63 63 var value = params[i];
64   -
  64 +
65 65 var responseTs = strftime('%Y-%m-%d %H:%M:%S', new Date());
66 66 var dummyTask = { requestId: value.REQUESTID };
67   - pushResponseToMongoDb(dummyTask, {ts: responseTs, supplier: config.globals.gateway_name, raw: JSON.stringify(value), parsed: value});
68   -
  67 +
69 68 if (value['RESPONSECODE'] == '00' && config.h2h_out.parse_sn == 'YES') {
70 69 value['MESSAGE'] = 'SN=' + parseSN(value['MESSAGE']) + '; ' + value['MESSAGE'];
71 70 }
72   -
  71 +
73 72 if (value['RESPONSECODE'] != '00' && value['RESPONSECODE'] != '68') {
74 73 value['RESPONSECODE'] = '40';
75 74 }
76   -
  75 +
  76 + pushResponseToMongoDb(dummyTask, {ts: responseTs, supplier: config.globals.gateway_name, raw: JSON.stringify(value), parsed: value}, value['RESPONSECODE']);
  77 +
77 78 callbackReport(value['REQUESTID'], value['RESPONSECODE'], value['MESSAGE']);
78 79 }
79 80  
... ... @@ -82,7 +83,7 @@ function createXMLRPCServer() {
82 83 }
83 84  
84 85 function topupRequestHttpGet(task, retry) {
85   -
  86 +
86 87 var options = {
87 88 method: 'GET',
88 89 url: config.h2h_out.partner,
... ... @@ -96,7 +97,7 @@ function topupRequestHttpGet(task, retry) {
96 97 idtrx: task.requestId
97 98 }
98 99 }
99   -
  100 +
100 101 request(options, function(err, res, body) {
101 102 });
102 103 }
... ... @@ -116,7 +117,7 @@ function topupRequestXMLRPC(task, retry) {
116 117 } else {
117 118 client = xmlrpc.createClient(clientOptions);
118 119 }
119   -
  120 +
120 121 var params = {
121 122 MSISDN: config.h2h_out.userid,
122 123 REQUESTID: task.requestId,
... ... @@ -124,42 +125,43 @@ function topupRequestXMLRPC(task, retry) {
124 125 NOHP: task.destination,
125 126 NOM: task.remoteProduct
126 127 };
127   -
  128 +
128 129 var methodName = 'topUpRequest';
129 130 logger.info('Preparing XMLRPC client method', {methodname: methodName, params: params});
130   -
131   - client.methodCall(methodName, [ params ], function (error, value) {
  131 +
  132 + client.methodCall(methodName, [ params ], function (error, value) {
132 133 // Results of the method response
133 134 if (error) {
134   -
  135 +
135 136 logger.warn('XMLRPC Client Error', {requestId: task['requestId'], errorMessage: error});
136   -
  137 +
137 138 if (retry) {
138   -
  139 +
139 140 logger.info('Retrying topUpRequest (' + retry + ')');
140 141 setTimeout(function() {
141 142 topupRequest(task, retry - 1);
142 143 }, sleep_before_retry);
143   -
  144 +
144 145 } else {
145 146 callbackReport(task['requestId'], '68', 'Silahkan resend. Gangguan koneksi ke suplier: ' + error);
146 147 }
147 148 return;
148 149 }
149   -
  150 +
150 151 logger.info('Got XMLRPC response from partner for', {response_method: methodName, response_message: value});
151   -
  152 +
152 153 var responseTs = strftime('%Y-%m-%d %H:%M:%S', new Date());
153   - pushResponseToMongoDb(task, {ts: responseTs, supplier: config.globals.gateway_name, raw: JSON.stringify(value), parsed: value});
154   -
  154 +
155 155 if (value['RESPONSECODE'] == '00' && config.h2h_out.parse_sn == 'YES') {
156 156 value['MESSAGE'] = 'SN=' + parseSN(value['MESSAGE']) + '; ' + value['MESSAGE'];
157 157 }
158   -
  158 +
159 159 if (value['RESPONSECODE'] != '00' && value['RESPONSECODE'] != '68') {
160 160 value['RESPONSECODE'] = '40';
161 161 }
162   -
  162 +
  163 + pushResponseToMongoDb(task, {ts: responseTs, supplier: config.globals.gateway_name, raw: JSON.stringify(value), parsed: value}, value['RESPONSECODE']);
  164 +
163 165 callbackReport(value['REQUESTID'], value['RESPONSECODE'], value['MESSAGE']);
164 166 });
165 167 }
... ... @@ -168,10 +170,10 @@ function topupRequest(task, retry) {
168 170 if (retry === undefined) {
169 171 task.ts = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD HH:mm:ss');
170 172 task.ts_date = moment(task.timestamp, 'YYYYMMDDHHmmss').format('YYYY-MM-DD');
171   -
  173 +
172 174 insertTaskToMongoDb(task);
173 175 }
174   -
  176 +
175 177 topupRequestXMLRPC(task, retry);
176 178 }
177 179  
... ... @@ -179,10 +181,10 @@ function initMongoClient() {
179 181 if (!config.mongodb || !config.mongodb.url) {
180 182 return;
181 183 }
182   -
  184 +
183 185 try {
184 186 var url = config.mongodb.url;
185   -
  187 +
186 188 mongoClient.connect(url, function(err, db) {
187 189 if (err) {
188 190 logger.warn('Failed to connect to mongodb', {err: err});
... ... @@ -194,22 +196,22 @@ function initMongoClient() {
194 196 }
195 197 catch(err) {
196 198 logger.warn('Exception when connecting to mongodb', {err: err, url: url});
197   - }
  199 + }
198 200 }
199 201  
200 202 function isMongoReady() {
201 203 if (!config.mongodb) { return; }
202 204 if (!config.mongodb.collection) { return; }
203 205 if (!mongodb) { return; }
204   -
  206 +
205 207 return true;
206 208 }
207 209  
208 210 function insertTaskToMongoDb(task) {
209 211 if (!isMongoReady()) { return; }
210   -
  212 +
211 213 task.supplier = config.globals.gateway_name;
212   -
  214 +
213 215 try {
214 216 mongodb.collection(config.mongodb.collection).insertOne(task);
215 217 }
... ... @@ -218,16 +220,17 @@ function insertTaskToMongoDb(task) {
218 220 }
219 221 }
220 222  
221   -function pushResponseToMongoDb(task, response) {
  223 +function pushResponseToMongoDb(task, response, rc) {
222 224 if (!isMongoReady()) { return; }
223   -
  225 +
224 226 try {
225 227 mongodb.collection(config.mongodb.collection).updateOne(
226 228 {requestId: task.requestId},
227 229 {
228 230 $set: {
229 231 lastResponse: response,
230   - supplier: config.globals.gateway_name
  232 + supplier: config.globals.gateway_name,
  233 + rc: rc
231 234 },
232 235 $push: {
233 236 responses: response