/*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');
module.exports.notificationsCheck= function(body, callback) {
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);
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);
}
}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");
}
}
}
}
});
}
function cronjob(emailTo, subject, day){
var crontab = require('node-crontab');
var jobId = crontab.scheduleJob("* * * "+day+" * *", function(){
email.sendEmail(emailTo, subject);
}, null, null, false);
}