Switch to side-by-side view

--- a/backend/notificationManager.js
+++ b/backend/notificationManager.js
@@ -2,134 +2,858 @@
 
 //Notification Manager to check notifications with rules
 
-var mysql = require('../controllers/mysql');
-var email = require('../controllers/emailer');
+var mysql  = require('../controllers/mysql');
+var email  = require('../controllers/emailer');
+var logger = require('../config/logger.js');
 
 module.exports.notificationsCheck= function(body, callback) {
+
+	var handler = "backend.controllers.notificationManager";
+
+	//Date Variables
 	var today = new Date();
 	var endoftwoweeks = new Date();
 	var endofweek = new Date();
 	var endofthreedays = new Date();
 	var lastDayOfMonth = new Date(today.getFullYear(), today.getMonth() + 1, 0);
 
+	//Auxiliary Variables
+	var AllData= [{}];
+	var flag = false;
+	var aux = 0;
+	var cnt = 1;
+	var keys = [];
+	var values = [];
+
 	endoftwoweeks.setDate(endoftwoweeks.getDate() + 14);
 	endofweek.setDate(endofweek.getDate() + 7);
 	endofthreedays.setDate(endofthreedays.getDate() + 3);
-	
-
-	strSubject = body.subject.replace(/\s/g, '');
-	var newstring = strSubject.split("=");
-	var parameter = newstring[0];
-	var valueNotification = parseFloat(newstring[1]);
-	
-
-	mysql.getRulesListByToken(body.token, function(isOK, ruleslist){
-		if(!isOK){
-			callback(false);
-		}else if(ruleslist){
-			for(i = 0; i < ruleslist.length; i++){
-				var valueRules = parseFloat(ruleslist[i].controlValue);
-				if(ruleslist[i].conditionValue == ">"){
-					if((valueNotification > valueRules) && (parameter == ruleslist[i].parameter)){
-						if(ruleslist[i].notificationType == 1){
-							cronjob(body.emailTo, body.subject, lastDayOfMonth.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 2){
-							cronjob(body.emailTo, body.subject, endoftwoweeks.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 3){
-							cronjob(body.emailTo, body.subject, endofweek.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 4){
-							cronjob(body.emailTo, body.subject, endofthreedays.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 5){
-							cronjob(body.emailTo, body.subject, today.getDate());
-							callback(true);
+
+
+	for(var k in body){ 
+		keys.push(k);
+		values.push(body[k]);
+	}
+
+	for(var i = keys.length - 1; i >= 0; i--) {
+		if(keys[i] === "emailTo" || keys[i] === "token") {
+			keys.splice(i, 1);
+			values.splice(i, 1);
+		}
+	}
+
+	for(var check = 0; check < keys.length; check = check + 2){
+		var stringSubject = "subject_" + cnt;
+		var stringSubjectValue = "subjectValue_" + cnt;
+		if(keys[check] != stringSubject || keys[check + 1] != stringSubjectValue){
+			flag = true;
+			break;	
+		}
+		cnt++;
+	}
+	if(flag == false){
+		mysql.getRulesListByToken(body.token, function(isOK, ruleslist){
+			if(!isOK){
+				callback(false);
+			}else if(ruleslist){
+				for(j = 0; j <= values.length / 2; j = j + 2){
+					var parameter = values[j];
+					var valueNotification = parseFloat(values[j + 1]);
+					for(i = 0; i < ruleslist.length; i++){
+						var valueRules = parseFloat(ruleslist[i].controlValue);
+						if(ruleslist[i].conditionValue == ">"){
+							if((valueNotification > valueRules) && (parameter == ruleslist[i].parameter)){
+								if(ruleslist[i].notificationType == 1){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, lastDayOfMonth.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 2){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endoftwoweeks.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 3){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofweek.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 4){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofthreedays.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 5){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, today.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalN = ruleslist[i].totalNotifications + 1;
+									var totalApplyRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalN;
+									var percentage = (totalApplyRules * 100 ) /  totalN;
+									mysql.updateValues(totalN, totalApplyRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}
+							}else if(valueNotification > valueRules || (parameter != ruleslist[i].parameter)){
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Parameter is not valid";
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+							else{
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Value within the limits of the Rules";
+								var totalN = ruleslist[i].totalNotifications + 1;
+								var totalApplyRules = ruleslist[i].totalNotificationsApplyByRules;
+								var totalValue = valueNotification + ruleslist[i].totalValue;
+								var averageValue = (totalValue / totalN).toFixed(2);
+								var percentage = ((totalApplyRules * 100 ) /  totalN).toFixed(2);
+								mysql.updateValues(totalN, totalApplyRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+						} else if(ruleslist[i].conditionValue == ">="){
+							if((valueNotification >= valueRules) && (parameter == ruleslist[i].parameter)){
+								if(ruleslist[i].notificationType == 1){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, lastDayOfMonth.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 2){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endoftwoweeks.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 3){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofweek.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 4){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofthreedays.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 5){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, today.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}
+							}else if(valueNotification >= valueRules || (parameter != ruleslist[i].parameter)){
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Parameter is not valid";
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+							else{
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Value within the limits of the Rules";
+								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules;
+								var totalValue = valueNotification + ruleslist[i].totalValue;
+								var averageValue = totalValue / totalNotifications;
+								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+						} else if(ruleslist[i].conditionValue == "<"){
+							if((valueNotification < valueRules) && (parameter == ruleslist[i].parameter)){
+								if(ruleslist[i].notificationType == 1){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, lastDayOfMonth.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 2){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endoftwoweeks.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 3){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofweek.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 4){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofthreedays.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 5){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, today.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}
+							}else if(valueNotification < valueRules || (parameter != ruleslist[i].parameter)){
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Parameter is not valid";
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+							else{
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Value within the limits of the Rules";
+								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules;
+								var totalValue = valueNotification + ruleslist[i].totalValue;
+								var averageValue = totalValue / totalNotifications;
+								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+						}	else{
+							if((valueNotification <= valueRules) && (parameter == ruleslist[i].parameter)){
+								if(ruleslist[i].notificationType == 1){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, lastDayOfMonth.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 2){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endoftwoweeks.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 3){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofweek.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 4){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, endofthreedays.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}else if(ruleslist[i].notificationType == 5){
+									strSubject = parameter + " = " + valueNotification;
+									cronjob(body.emailTo, strSubject, today.getDate());
+									var data = {
+										emailTo: body.emailTo,
+										subject: parameter,
+										subjectValue: valueNotification,
+										token: body.token
+									};
+									flag = true;
+									AllData[aux] = strSubject + " : Success";
+									var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+									var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules + 1;
+									var totalValue = valueNotification + ruleslist[i].totalValue;
+									var averageValue = totalValue / totalNotifications;
+									var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+									mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+									mysql.insertNotification(data, function(isOK, data){
+										if(!isOK) {
+											logger.error(handler + " InsertNotification",data);
+										}else {
+											logger.info(handler + " InsertNotification",data);
+										}
+									});
+
+									if(j == values.length / 2){
+										if(flag == true){
+											callback(true, AllData);
+										}
+										else{
+											callback(false, AllData);
+										}
+									}
+								}
+							}else if(valueNotification <= valueRules || (parameter != ruleslist[i].parameter)){
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Parameter is not valid";
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
+							else{
+								strSubject = parameter + " = " + valueNotification;
+								AllData[aux] = strSubject + " : Value within the limits of the Rules";
+								var totalNotifications = ruleslist[i].totalNotificationsReceived + 1;
+								var totalApplyByRules = ruleslist[i].totalNotificationsApplyByRules;
+								var totalValue = valueNotification + ruleslist[i].totalValue;
+								var averageValue = totalValue / totalNotifications;
+								var percentage = (totalApplyByRules * 100 ) /  totalNotifications;
+								mysql.updateValues(totalNotifications, totalApplyByRules, totalValue, averageValue, percentage, ruleslist[i].rulesID);
+								if(j == values.length / 2){
+									if(flag == true){
+										callback(true, AllData);
+									}
+									else{
+										callback(false, AllData);
+									}
+								}
+							}
 						}
-					}else if(valueNotification > valueRules){
-						callback(false, "Parameter is not valid");
 					}
-					else{
-						callback(false, "Value within the limits of the Rules");
-					}
-				} else if(ruleslist[i].conditionValue == ">="){
-					if((valueNotification >= valueRules) && (parameter == ruleslist[i].parameter)){
-						if(ruleslist[i].notificationType == 1){
-							cronjob(body.emailTo, body.subject, lastDayOfMonth.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 2){
-							cronjob(body.emailTo, body.subject, endoftwoweeks.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 3){
-							cronjob(body.emailTo, body.subject, endofweek.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 4){
-							cronjob(body.emailTo, body.subject, endofthreedays.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 5){
-							cronjob(body.emailTo, body.subject, today.getDate());
-							callback(true);
-						}
-					}else if(valueNotification >= valueRules){
-						callback(false, "Parameter is not valid");
-					}
-					else{
-						callback(false, "Value within the limits of the Rules");
-					}
-				} else if(ruleslist[i].conditionValue == "<"){
-					if((valueNotification < valueRules) && (parameter == ruleslist[i].parameter)){
-						if(ruleslist[i].notificationType == 1){
-							cronjob(body.emailTo, body.subject, lastDayOfMonth.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 2){
-							cronjob(body.emailTo, body.subject, endoftwoweeks.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 3){
-							cronjob(body.emailTo, body.subject, endofweek.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 4){
-							cronjob(body.emailTo, body.subject, endofthreedays.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 5){
-							cronjob(body.emailTo, body.subject, today.getDate());
-							callback(true);
-						}
-					}else if(valueNotification < valueRules){
-						callback(false, "Parameter is not valid");
-					}
-					else{
-						callback(false, "Value within the limits of the Rules");
-					}
-				}	else{
-					if((valueNotification <= valueRules) && (parameter == ruleslist[i].parameter)){
-						if(ruleslist[i].notificationType == 1){
-							cronjob(body.emailTo, body.subject, lastDayOfMonth.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 2){
-							cronjob(body.emailTo, body.subject, endoftwoweeks.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 3){
-							cronjob(body.emailTo, body.subject, endofweek.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 4){
-							cronjob(body.emailTo, body.subject, endofthreedays.getDate());
-							callback(true);
-						}else if(ruleslist[i].notificationType == 5){
-							cronjob(body.emailTo, body.subject, today.getDate());
-							callback(true);
-						}
-					}else if(valueNotification <= valueRules){
-						callback(false, "Parameter is not valid");
-					}
-					else{
-						callback(false, "Value within the limits of the Rules");
-					}
+					aux++;
 				}
 			}
-		}
-
-	});
+
+		});
+	}else{
+		callback(false, "JSON Key with wrong format");
+	}
 }
 
 function cronjob(emailTo, subject, day){