/*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');
//Handle the notifications request
module.exports.notificationHandler = function(body, destinations, appToken, cb){
var handler = "backend.controllers.notificationHandler";
//log the handler name and inputarguments
logger.info("backend.controllers.notificationHandler with payload: "+JSON.stringify(body) + ";" +JSON.stringify(destinations) + ";" + JSON.stringify(appToken));
//prepare the http response
var response = [];
//grab the app rules list
mysql.getRulesListByToken(appToken, function(success, rules){
if(!success){
logger.info(handler,"Executing callback");
cb(false);
}else{
//iterate the body
body.forEach(function(eachBody, bodyIndex){
//prepare the response with some structure
response.push({subject: eachBody.subject, subjectValue: eachBody.subjectValue, results:[]});
//iterate the rules list
rules.forEach(function(eachRule, ruleIndex){
applyRule(destinations, eachBody, eachRule, appToken, function(ruleApplied, reason){
if(ruleApplied){
if(!reason){
//rule applied
logger.debug({subject: eachBody.subject, value: eachBody.subjectValue, ruleid: eachRule.rulesID, success: true, comment:"Rule applied"})
response.forEach(function(eachResponse){
if(eachResponse.subject == eachBody.subject && eachResponse.subjectValue == eachBody.subjectValue){
eachResponse.results.push({ruleid: eachRule.rulesID, success: true, comment:"Rule applied"})
}
})
}else{
//rule not applied because of threshold was not exceed
logger.debug({subject: eachBody.subject, value: eachBody.subjectValue, ruleid: eachRule.rulesID, success: false, comment:"Rule's threshold not exceed"});
response.forEach(function(eachResponse){
if(eachResponse.subject == eachBody.subject && eachResponse.subjectValue == eachBody.subjectValue){
eachResponse.results.push({ruleid: eachRule.rulesID, success: false, comment:"Rule's threshold not exceed"})
}
})
}
}else{
//rule not applied
logger.debug({subject: eachBody.subject, value: eachBody.subjectValue, ruleid: eachRule.rulesID, success: false, comment:"Rule not applied"})
response.forEach(function(eachResponse){
if(eachResponse.subject == eachBody.subject && eachResponse.subjectValue == eachBody.subjectValue){
eachResponse.results.push({ruleid: eachRule.rulesID, success: false, comment:"Rule not applied"})
}
})
}
//check the loop end
logger.debug("bodyIndex",bodyIndex,"body length",body.length,"ruleIndex",ruleIndex,"rules length",rules.length);
if(bodyIndex == body.length-1 && ruleIndex == rules.length-1){
logger.debug("Finished!");
cb(true, response)
}
})
})
})
}
})
}
//destinations: the emailTo array
//source: the incoming "subject" and "subjectValue"
//rule: the rules which we are trying to match
//token: its the app id on the DB...
//callback arguments:
// true - rule applied
// false, err - rule not applied because of error (err msg)
// true, reason - rule not applied because values are lower/higher then controlValue defined
function applyRule(destinations, source, rule, token, cb){
var handler = "applyRule";
logger.debug(handler,"is starting...");
var ruleApplied = false;
//check the rule parameter: if not exists, discard
if(source.subject == rule.parameter){
//prepare insertNotification input arguments
notificationInput = {
emailTo: destinations,
subject: source.subject,
subjectValue: parseFloat(source.subjectValue),
rulesID: rule.rulesID,
token: token
}
switch (rule.conditionValue){
case ">":
ruleApplied = ( parseFloat(source.subjectValue) > parseFloat(rule.controlValue) ) ? true : false
break;
case "<":
ruleApplied = ( parseFloat(source.subjectValue) < parseFloat(rule.controlValue) ) ? true : false
break;
case ">=":
ruleApplied = ( parseFloat(source.subjectValue) >= parseFloat(rule.controlValue) ) ? true : false
break;
case "<=":
ruleApplied = ( parseFloat(source.subjectValue) <= parseFloat(rule.controlValue) ) ? true : false
break;
default:
//already covered at >= / <=
}
if(ruleApplied){
//rule is applied
logger.info(handler, "rule " + rule.rulesID + " match");
mysql.insertNotification(notificationInput, function(notificationCreated, err){
if(notificationCreated){
logger.info(handler, "Notification created successfully");
statsDispatcher(true,source,rule, function(statsCreated, err){
if(statsCreated){
logger.info(handler, "Stats created successfully");
cronDispatcher(rule.notificationType, destinations, source.subject + " = " + source.subjectValue, function(cronCreated, err){
if(cronCreated){
logger.info(handler, "Cron created successfully");
cb(true);
}else{
logger.error(handler,err);
cb(false, err);
}
})
}else{
logger.error(handler, err);
cb(false, err);
}
})
}else{
logger.error(handler, err);
cb(false, err);
}
})
}else{
//rule not applied because controlValue not checked
logger.info(handler, "rule " + rule.rulesID + " does not match");
statsDispatcher(false, source, rule, function(statsCreated, err){
if(statsCreated){
logger.info(handler, "Stats created Successfully");
cb(true, "Threshold was not exceed");
}else{
logger.error(handler, err);
cb(false, err);
}
});
}
}else{
cb(false,"Parameter does not match")
}
}
//ruleApplied: TRUE if it was applied properly, FALSE if source is out of value scope
function statsDispatcher(ruleApplied, source, rule, cb){
mysql.insertStatistics(ruleApplied, source.subjectValue, source.subject, rule.rulesID, function(statsCreated, err){
if(statsCreated){
logger.info("statsDispatcher: Stats have been created");
cb(true);
}else{
logger.error("statsDispatcher: Stats have NOT been created", err);
cb(false, err);
}
})
}
//self explainatory
//TODO THIS CRON IS WRONG!
function cronDispatcher(schedule, emailTo, subject, cb){
var handler = "cronDispatcher";
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);
endoftwoweeks.setDate(endoftwoweeks.getDate() + 14);
endofweek.setDate(endofweek.getDate() + 7);
endofthreedays.setDate(endofthreedays.getDate() + 3);
var toCron = "";
switch (schedule){
case '1':
toCron = lastDayOfMonth.getDate();
break;
case '2':
toCron = endoftwoweeks.getDate();
break;
case '3':
toCron = endofweek.getDate();
break;
case '4':
toCron = endofthreedays.getDate();
break;
default:
//trigger immediately
toCron = today.getDate();
}
logger.debug(handler,"email feature is not active");
//TODO uncomment this
/*
var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * "+toCron+" * *", function(){
for(var i = 0; i < emailTo.length; i++){
email.sendEmail(emailTo[i], subject);
}
}, null, null, false);
*/
cb(true)
}
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;
}
}