--- a/controllers/mysql.js
+++ b/controllers/mysql.js
@@ -26,33 +26,48 @@
})
})
}
+
/*
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});
- }
- })
+ var response = [];
+ var bool = false;
+ body.appID.forEach(function(eachBody, bodyIndex){
+ var sqlQuery = "SELECT appID FROM registration where appID = '"+eachBody+"'";
+ handlerDB(sqlQuery, function(err, result){
+ if (err){
+ callback(false, err);
}
else{
- callback(false,"App is already registered or AppID is Null");
- }
- }
+ if(result.length == 0 && eachBody != "null"){
+ var newToken = token.token_generator();
+ var sqlQuery = "INSERT INTO registration ( token, appID, developerID) VALUES ('"+newToken+"','"+eachBody+"','"+body.developerID[bodyIndex]+"')";
+ handlerDB(sqlQuery, function(err, result){
+ if(err){
+ callback(false, err);
+ }else{
+ email.sendEmailToken(body.developerID[bodyIndex], newToken);
+ response.push({token: newToken});
+ bool = true;
+ if(bodyIndex == body.appID.length - 1){
+ callback(true,response);
+ }
+ }
+ })
+ }
+ else{
+ response.push({token: "App is already registered or AppID is Null"});
+ if(bodyIndex == body.appID.length - 1){
+ if(bool == true){
+ callback(true,response);
+ }else{
+ callback(false,response);
+ }
+ }
+ }
+ }
+ })
})
}
@@ -61,46 +76,72 @@
*/
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)+")";
+ var sqlQuery = "INSERT INTO notificationlist ( subject, token, rulesID) VALUES ('"+strSubject+"', '"+data.token+"', '"+data.rulesID+"')";
+ handlerDB(sqlQuery, function(err, result){
+ if (err){
+ callback(false,err);
+ }
+ else{
+ callback(true, result);
+ }
+ })
+}
+
+/*
+Insert Rules
+*/
+module.exports.insertNotificationRules= function(body, callback){
+ console.log("body.emailTo.length: " + JSON.stringify(body));
+ console.log("body.emailTo: " + body.emailTo[0]);
+ var destinations ="";
- 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);
+ for(i = 0; i < body.emailTo.length; i++){
+ if(body.emailTo[i] == null && body.emailTo.length == 1){
+ destinations = null;
+ }else{
+ if(body.emailTo[i] != null){
+ destinations += body.emailTo[i];
+ if(i < body.emailTo.length - 1 && body.emailTo.length != 1){
+ destinations += ", "
}
- else {
- callback(true);
- }
- });
- }
- })
+ }
+ }
+ }
+ if(body.hasOwnProperty('token')){
+ var sqlQuery = "INSERT INTO ruleslist ( description, parameter, conditionValue, controlValue, threshold, notifyType, emailTo, notificationType, hostname, port, path, method, token) VALUES ('"+body.description+"','"+body.parameter+"','"+body.conditionValue+"','"+body.controlValue+"','"+body.threshold+"','"+body.notifyType+"','"+destinations+"','"+body.notificationType+"','"+body.hostname+"','"+body.port+"','"+body.path+"','"+body.method+"','"+body.token+"')";
+ handlerDB(sqlQuery, function(err, result){
+ if (err){
+ callback(false,err);
+ }else {
+ callback(true);
+ }
+ });
+ }else{
+ var sql = "SELECT token FROM registration where appID = '"+body.appID+"'";
+ handlerDB(sql, function(err, res){
+ if (err){
+ callback(false,err);
+ }else{
+ var sqlQuery2 = "INSERT INTO ruleslist ( description, parameter, conditionValue, controlValue, threshold, notifyType, emailTo, notificationType, hostname, port, path, method, token) VALUES ('"+body.description+"','"+body.parameter+"','"+body.conditionValue+"','"+body.controlValue+"','"+body.threshold+"','"+body.notifyType+"','"+destinations+"','"+body.notificationType+"','"+body.hostname+"','"+body.port+"','"+body.path+"','"+body.method+"','"+res[0].token+"')";
+ console.log(sqlQuery2);
+ handlerDB(sqlQuery2, 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+"')";
+ 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);
@@ -143,6 +184,7 @@
}
})
}
+
/*
Retrieve Notifications by appid
*/
@@ -199,11 +241,9 @@
})
}
-
/*
Retrieve Statistics
*/
-
module.exports.retrieveStatisticsList = function(ruleid, callback){
var handler = "retrieveStatisticsList";
var totalNotifications = 0;
@@ -261,6 +301,51 @@
}
/*
+Get Rules by Token
+*/
+module.exports.getEmailTovAppByToken = function(token, callback){
+ var sqlQuery = "SELECT developerID FROM registration where token = '"+token+"'";
+ handlerDB(sqlQuery, function(err, developer){
+ if (err) {
+ callback(false,err);
+ }
+ else{
+ callback(true, developer);
+ }
+ })
+}
+
+/*
+Get Rules by RulesID
+*/
+module.exports.getRulesListByRulesID = function(rulesID, callback){
+ var sqlQuery = "SELECT * FROM ruleslist where rulesID = '"+rulesID+"'";
+ handlerDB(sqlQuery, function(err, ruleslist){
+ if (err) {
+ callback(false,err);
+ }
+ else{
+ callback(true, ruleslist);
+ }
+ })
+}
+
+/*
+Get All Rules
+*/
+module.exports.getRulesList = function(callback){
+ var sqlQuery = "SELECT * FROM ruleslist";
+ handlerDB(sqlQuery, function(err, ruleslist){
+ if (err) {
+ callback(false,err);
+ }
+ else{
+ callback(true, ruleslist);
+ }
+ })
+}
+
+/*
Check If Token Exists
*/
module.exports.checkIfTokenExists = function(token, callback){
@@ -276,6 +361,31 @@
else{
callback(false, "Invalid Token");
}
+ }
+ })
+}
+
+/*
+Check If Rule Have Automatic Rule Created by System
+*/
+module.exports.checkIfRuleAutomaticExists = function(rule, callback){
+ var aux = false;
+ var sql = "SELECT description FROM ruleslist";
+ handlerDB(sql, function(err, res){
+ if (err){
+ callback(false);
+ }
+ else{
+ for(var i = 0; i < res.length; i++){
+ if(res[i].description.search("System Create Automatically New rule: " + rule.description) > -1){
+ aux = true;
+ }
+ }
+ }
+ if(aux == false){
+ callback(false);
+ }else{
+ callback(true);
}
})
}
@@ -312,7 +422,7 @@
callback(false,err);
}
else{
- callback(true, rows);
+ callback(true);
}
});
}
@@ -328,17 +438,32 @@
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){
+ var sql = "SELECT rulesID FROM ruleslist where rulesID = '"+rulesid+"'";
+ handlerDB(sql, function(err, res){
+ if (err){
+ callback(false,err);
+ }
+ else{
+ if(res.length == 0){
callback(false,"RuleID incorrect");
- }
- else{
- callback(true,rows);
+ }else{
+ var sqlRules = "DELETE FROM statisticslist where rulesID = '"+rulesid+"'";
+ handlerDB(sqlRules, function(err, res){
+ if (err) {
+ callback(false,err);
+ }
+ else{
+ var sqlQuery = "DELETE FROM ruleslist where rulesID = '"+rulesid+"'";
+ handlerDB(sqlQuery, function(err, rows){
+ if (err) {
+ callback(false,err);
+ }
+ else{
+ callback(true);
+ }
+ });
+ }
+ })
}
}
})
@@ -348,7 +473,7 @@
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+"'";
+ var sqlQuery = "UPDATE ruleslist SET description = '"+data.description+"', parameter = '"+data.parameter+"', conditionValue = '"+data.conditionValue+"', controlValue = '"+data.controlValue+"', threshold = '"+data.threshold+"', notificationType = '"+data.notificationType+"' where rulesID = '"+data.rulesid+"'";
handlerDB(sqlQuery, function(err, result){
if (err){
callback(false,err);