Child: [b33563] (diff)

Download this file

notificationManager.js    140 lines (130 with data), 5.4 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
/*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);
}