Parent: [0765ef] (diff)

Child: [18557b] (diff)

Download this file

mysql.js    377 lines (352 with data), 9.1 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
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
var mysql = require('mysql');
var db = require ('../config/db');
var token = require ('../backend/token/tokenGenerator');
var dbpool = require ('../backend/server');
var email = require('./emailer');
var logger = require('../config/logger.js');
function handlerDB (query, cb){
dbpool.getConnection(function(err, conn){
if(err){
logger.error("handlerDB",err);
cb(true, err);
}
logger.info("handlerDB","connected as id ", conn.threadId);
conn.query(query,function(err, rows){
conn.release();
if(!err){
cb(false, rows)
}
})
conn.on('error', function(err){
logger.error("handlerDB","error on database connection");
})
})
}
/*
Insert vApp
*/
module.exports.insertVapp= function(body, callback){
var sqlQuery = "SELECT appID FROM registration where appID = '"+body.appID+"'";
handlerDB(sqlQuery, function(err, result){
if (err){
callback(false,err);
}
else{
if(result.length == 0 && body.appID != "null"){
var newToken = token.token_generator();
var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+body.appID+"','"+body.developerID+"')";
handlerDB(sqlQuery, function(err, result){
if(err){
callback(false, result)
}else{
email.sendEmailToken(body.developerID, newToken);
callback(true,{token: newToken});
}
})
}
else{
callback(false,"App is already registered or AppID is Null");
}
}
})
}
/*
Insert Notifications
*/
module.exports.insertNotification= function(data, callback){
strSubject = data.subject + " = " + data.subjectValue;
var sqlQuery = "INSERT INTO notificationlist ( emailTo, subject, token, ntype) VALUES ('"+data.emailTo+"','"+strSubject+"', '"+data.token+"', "+parseInt(data.ntype)+")";
handlerDB(sqlQuery, function(err, result){
if (err){
callback(false,err);
}
else{
callback(true, result);
}
})
}
/*
Insert Rules
*/
module.exports.insertNotificationRules= function(body, callback){
var sql = "SELECT token FROM registration where appID = '"+body.appID+"'";
handlerDB(sql, function(err, res){
if (err){
callback(false,err);
}
else{
var sqlQuery = "INSERT INTO ruleslist ( description, parameter, conditionValue, controlValue, notificationType, token) VALUES ('"+body.description+"','"+body.parameter+"','"+body.conditionValue+"','"+body.controlValue+"','"+body.notificationType+"','"+res[0].token+"')";
handlerDB(sqlQuery, function(err, result){
if (err){
callback(false,err);
}
else {
callback(true);
}
});
}
})
}
/*
Insert Statistics
*/
module.exports.insertStatistics = function(result, subjectValue, subject, rulesid, callback){
var sqlQuery = "INSERT INTO statisticslist ( result, subjectValue, subject, rulesid) VALUES ('"+result+"', '"+subjectValue+"', '"+subject+"', '"+rulesid+"')";
handlerDB(sqlQuery, function(err, result){
if (err) {
logger.info("Update Values for Statistics: " + err);
callback(false,err);
}
else{
logger.info("Update Values for Statistics: Success");
callback(true, subject);
}
})
}
/*
Retrieve vApp
*/
module.exports.retrieveApp= function(developerid,callback){
var sqlQuery = "SELECT * FROM registration where developerID = '"+developerid+"'";
handlerDB(sqlQuery, function(err,result){
if (err){
callback(false,err);
}
else {
if(result.length == 0){
callback(false,"DeveloperID does not exist");
}
else{
callback(true,result);
}
}
})
}
module.exports.getNotificationsByListId = function(IDArray, cb){
handlerDB("select * from notificationlist where notificationID IN "+IDArray,function(err,res){
if(err){
cb(true, err)
}else{
cb(false, res)
}
})
}
/*
Retrieve Notifications by appid
*/
module.exports.retrieveNotificationList= function(appid,callback){
var sql = "SELECT token FROM registration where appID = '"+appid+"'";
handlerDB(sql, function(err, res){
if (err){
callback(false,err);
}
else{
if(res.length == 0){
callback(false,"AppID incorrect");
}
else{
var sqlQuery = "SELECT * FROM notificationlist where token = '"+res[0].token+"'";
handlerDB(sqlQuery, function(err, rows){
if (err){
callback(false,err);
}
else {
callback(true,rows);
}
})
}
}
})
}
/*
Retrieve Rules
*/
module.exports.retrieveRulesList= function(appid,callback){
var sql = "SELECT token FROM registration where appID = '"+appid+"'";
handlerDB(sql, function(err, res){
if (err){
callback(false,err);
}
else{
if(res.length == 0){
callback(false,"AppID incorrect");
}
else{
var sqlQuery = "SELECT * FROM ruleslist where token = '"+res[0].token+"'";
handlerDB(sqlQuery, function(err, rows){
if (err){
callback(false,err);
}
else{
callback(true,rows);
}
})
}
}
})
}
/*
Retrieve Statistics
*/
module.exports.retrieveStatisticsList = function(ruleid, callback){
var handler = "retrieveStatisticsList";
var totalNotifications = 0;
var totalNotificationsApplyByRules = 0;
var totalValue = 0;
var averageValue = null;
var percentage = null;
var sqlQuery = "SELECT * FROM statisticslist where rulesID = '"+ruleid+"'";
handlerDB(sqlQuery, function(err, rows){
if (err) {
logger.error(handler,err);
callback(false,err);
}
else{
totalNotifications = rows.length;
if(totalNotifications == 0){
totalNotifications = 0;
totalValue = 0;
totalNotificationsApplyByRules = 0;
averageValue = 0.0;
percentage = 0.0;
}else{
for (var i = 0; i < totalNotifications; i++){
if(rows[i].result == "true"){
totalNotificationsApplyByRules++;
totalValue = parseFloat(totalValue + parseFloat(rows[i].subjectValue));
}
else{
totalValue = parseFloat(totalValue + parseFloat(rows[i].subjectValue));
}
}
averageValue = (totalValue / totalNotifications).toFixed(2);
percentage = ((parseInt(totalNotificationsApplyByRules) * 100 ) / parseInt(totalNotifications)).toFixed(2);
}
callback(true, rows, {totalNotifications: totalNotifications, totalNotificationsApplyByRules: totalNotificationsApplyByRules, averageValue: averageValue, percentage: percentage});
}
})
}
/*
Get Rules by Token
*/
module.exports.getRulesListByToken = function(token, callback){
var sqlQuery = "SELECT * FROM ruleslist where token = '"+token+"'";
handlerDB(sqlQuery, function(err, ruleslist){
if (err) {
callback(false,err);
}
else{
callback(true, ruleslist);
}
})
}
/*
Check If Token Exists
*/
module.exports.checkIfTokenExists = function(token, callback){
var sql = "SELECT token FROM registration where token = '"+token+"'";
handlerDB(sql, function(err, res){
if (err){
callback(false);
}
else{
if(res.length == 1){
callback(true, res);
}
else{
callback(false, "Invalid Token");
}
}
})
}
/*
Delete vApp and Rules
*/
module.exports.deleteVapp= function(appid, callback){
var sql = "SELECT token FROM registration where appID = '"+appid+"'";
handlerDB(sql, function(err, res){
if (err){
callback(false,err);
}
else{
if(res.length == 0){
callback(false,"AppID incorrect");
}
else{
var sqlNotifcations = "DELETE FROM notificationlist where token = '"+res[0].token+"'";
handlerDB(sqlNotifcations, function(err, rows){
if (err) {
callback(false,err);
}
else{
var sqlRules = "DELETE FROM ruleslist where token = '"+res[0].token+"'";
handlerDB(sqlRules, function(err, rows){
if (err) {
callback(false,err);
}
else{
var sqlQuery = "DELETE FROM registration where token = '"+res[0].token+"'";
handlerDB(sqlQuery, function (err, rows) {
if (err){
callback(false,err);
}
else{
callback(true, rows);
}
});
}
})
}
})
}
}
})
}
/*
Delete Rule
*/
module.exports.deleteRule= function(rulesid, callback){
var sqlQuery = "DELETE FROM ruleslist where rulesID = '"+rulesid+"'";
handlerDB(sqlQuery, function(err, rows){
if (err) {
callback(false,err);
}
else{
if(rows.length == 0){
callback(false,"RuleID incorrect");
}
else{
callback(true,rows);
}
}
})
}
/*
Update Rule
*/
module.exports.updateRule= function(data, callback){
var sqlQuery = "UPDATE ruleslist SET description = '"+data.description+"', parameter = '"+data.parameter+"', conditionValue = '"+data.conditionValue+"', controlValue = '"+data.controlValue+"', notificationType = '"+data.notificationType+"' where rulesID = '"+data.rulesid+"'";
handlerDB(sqlQuery, function(err, result){
if (err){
callback(false,err);
}
else{
callback(true, result);
}
})
}
/*
Get All Parameters on Specific Rule
*/
module.exports.retrieveAllRule= function(RuleID, callback){
var sqlQuery = "SELECT * FROM ruleslist where rulesID = '"+RuleID+"'";
handlerDB(sqlQuery, function(err, result){
if (err){
callback(false,err);
}
else{
callback(true, result);
}
})
}