/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
//Notification Manager to check notifications with rules
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);
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);
}
}
}
}
}
aux++;
}
}
});
}else{
callback(false, "JSON Key with wrong format");
}
}
function cronjob(emailTo, subject, day){
var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * "+day+" * *", function(){
email.sendEmail(emailTo, subject);
}, null, null, false);
}