/*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, emailTo, token, 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;
//Define Day for Cronjob
endoftwoweeks.setDate(endoftwoweeks.getDate() + 14);
endofweek.setDate(endofweek.getDate() + 7);
endofthreedays.setDate(endofthreedays.getDate() + 3);
//Get Rules by Token
mysql.getRulesListByToken(token, function(isOK, ruleslist){
if(!isOK){
callback(false);
}else if(ruleslist){
//Iterate http request body
for(j = 0; j < body.length; j++){
//Iterate Rules List
for(i = 0; i < ruleslist.length; i++){
//Check the condition Value
if(ruleslist[i].conditionValue == ">"){
if((parseFloat(body[j].subjectValue) > parseFloat(ruleslist[i].controlValue)) && (body[j].subject == ruleslist[i].parameter)){
//Check Priority of Notification
if(ruleslist[i].notificationType == 1){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 2){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endoftwoweeks.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 3){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofweek.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 4){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofthreedays.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
else if(ruleslist[i].notificationType == 5){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, today.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
}else if( body[j].subject != ruleslist[i].parameter){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Parameter is not valid";
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}else if (parseFloat(body[j].subjectValue) <= parseFloat(ruleslist[i].controlValue)){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Value within the limits of the Rules";
mysql.insertStatistics(false, parseFloat(body[j].subjectValue), body[j].subject, ruleslist[i].rulesID, function(isOK){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success");
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
}else if(ruleslist[i].conditionValue == ">="){
if((parseFloat(body[j].subjectValue) >= parseFloat(ruleslist[i].controlValue)) && (body[j].subject == ruleslist[i].parameter)){
//Check Priority of Notification
if(ruleslist[i].notificationType == 1){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 2){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endoftwoweeks.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 3){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofweek.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 4){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofthreedays.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
else if(ruleslist[i].notificationType == 5){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, today.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
}else if( body[j].subject != ruleslist[i].parameter){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Parameter is not valid";
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}else if (parseFloat(body[j].subjectValue) < parseFloat(ruleslist[i].controlValue)){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Value within the limits of the Rules";
mysql.insertStatistics(false, parseFloat(body[j].subjectValue), body[j].subject, ruleslist[i].rulesID, function(isOK){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success");
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
}else if(ruleslist[i].conditionValue == "<"){
if((parseFloat(body[j].subjectValue) < parseFloat(ruleslist[i].controlValue)) && (body[j].subject == ruleslist[i].parameter)){
//Check Priority of Notification
if(ruleslist[i].notificationType == 1){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 2){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endoftwoweeks.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 3){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofweek.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 4){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofthreedays.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
else if(ruleslist[i].notificationType == 5){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, today.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
}else if(body[j].subject != ruleslist[i].parameter){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Parameter is not valid";
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}else if (parseFloat(body[j].subjectValue) >= parseFloat(ruleslist[i].controlValue)){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Value within the limits of the Rules";
mysql.insertStatistics(false, parseFloat(body[j].subjectValue), body[j].subject, ruleslist[i].rulesID, function(isOK){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success");
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
}else if(ruleslist[i].conditionValue == "<="){
if((parseFloat(body[j].subjectValue) <= parseFloat(ruleslist[i].controlValue)) && (body[j].subject == ruleslist[i].parameter)){
//Check Priority of Notification
if(ruleslist[i].notificationType == 1){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, lastDayOfMonth.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 2){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endoftwoweeks.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 3){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofweek.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}else if(ruleslist[i].notificationType == 4){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, endofthreedays.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
else if(ruleslist[i].notificationType == 5){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
cronjob(emailTo, strSubject, today.getDate());
AllData[j] = strSubject + " : Success";
flag = true;
mysql.insertNotification({emailTo: emailTo, subject: body[j].subject, subjectValue: parseFloat(body[j].subjectValue), rulesID: ruleslist[i].rulesID, token: token}, function(isOK, resdata){
if(!isOK) {
logger.error(handler + " InsertNotification ",resdata);
}else {
logger.info(handler + " InsertNotification ",resdata);
mysql.insertStatistics(true, resdata.subjectValue, resdata.subject, resdata.rulesID, function(isOK, resubject){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success for: " + resubject);
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
});
}
}else if(body[j].subject != ruleslist[i].parameter){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Parameter is not valid";
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}else if(parseFloat(body[j].subjectValue) > parseFloat(ruleslist[i].controlValue)){
strSubject = body[j].subject + " = " + parseFloat(body[j].subjectValue);
AllData[j] = strSubject + " : Value within the limits of the Rules";
mysql.insertStatistics(false, parseFloat(body[j].subjectValue), body[j].subject, ruleslist[i].rulesID, function(isOK){
if(!isOK) {
logger.error(handler + " InsertStatistics Failed");
}else {
logger.info(handler + " InsertStatistics Success");
if(j == body.length - 1 && i == ruleslist.length - 1){
if(flag == true){
callback(true, AllData);
}else{
callback(false, AllData);
}
}
}
});
}
}
}
}
}
});
}
function cronjob(emailTo, subject, day){
var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * "+day+" * *", function(){
for(var i = 0; i < emailTo.length; i++){
email.sendEmail(emailTo[i], subject);
}
}, null, null, false);
}
function checkOut(j, length, flag){
if(j == length - 1){
if(flag == true){
return 1;
}else{
return 2;
}
}else{
return 0;
}
}