Switch to side-by-side view

--- a/backend/notificationManager.js
+++ b/backend/notificationManager.js
@@ -90,6 +90,7 @@
 		}
 	})
 }
+
 /*
 routine to concatenate the notificationHandler response
 input: response, request body, internal rule applied, success type (boolean), the comment for the rule agains the resquested body
@@ -107,7 +108,7 @@
 		}
 	})
 }
-//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...
@@ -182,6 +183,7 @@
 		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){
@@ -194,89 +196,96 @@
 		}
 	})	
 }
-//self explainatory 
-//function cronDispatcher(reason){
-	module.exports.cronDispatcher = function(reason){
-		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 thiscron = [
-		{
-			type: 1,
-			list:[],
-			schedule: lastDayOfMonth.getDate(),
-		},{
-			type: 2,
-			list:[],
-			schedule: endoftwoweeks.getDate(),
-		},{
-			type: 3,
-			list:[],
-			schedule: endofweek.getDate(),
-		},{
-			type: 4,
-			list:[],
-			schedule: endofthreedays.getDate(),
-		},{
-			type: 5,
-			list:[],
-			schedule: today.getDate()
-		}
-		];
-
-		reason.forEach(function(eachElem, reasonidx){
-			eachElem.results.forEach(function(eachResult, resultidx){
-				if(eachResult.success){
-					//Get Rules by RulesID
-					mysql.getRulesListByRulesID(eachResult.ruleid, function(isOK, ruleslist){
-						if(!isOK){
-							logger.error("getRulesListByRulesID Failed: " + ruleslist);
+
+//function cronDispatcher(reason)
+module.exports.cronDispatcher = function(reason){
+	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 thiscron = [
+	{
+		type: 1,
+		list:[],
+		schedule: lastDayOfMonth.getDate(),
+	},{
+		type: 2,
+		list:[],
+		schedule: endoftwoweeks.getDate(),
+	},{
+		type: 3,
+		list:[],
+		schedule: endofweek.getDate(),
+	},{
+		type: 4,
+		list:[],
+		schedule: endofthreedays.getDate(),
+	},{
+		type: 5,
+		list:[],
+		schedule: today.getDate()
+	}
+	];
+
+	reason.forEach(function(eachElem, reasonidx){
+		eachElem.results.forEach(function(eachResult, resultidx){
+			if(eachResult.success){
+				mysql.getRulesListByRulesID(eachResult.ruleid, function(isOK, ruleslist){
+					if(!isOK){
+						logger.error("getRulesListByRulesID Failed: " + ruleslist);
+					}else{
+						logger.info("getRulesListByRulesID: Success");
+						if(ruleslist[0].notificationType == 0){
+							httprequest.sendHttpRequest(ruleslist[0].hostname, ruleslist[0].port, ruleslist[0].path, ruleslist[0].method, {subject: eachElem.subject, value: eachElem.subjectValue,rule:eachResult.ruleid});
+							//Detect the End of Loop
+							if(reason.length-1 == reasonidx && eachElem.results.length-1 == resultidx){
+								ToSendEmail(thiscron);
+							}
 						}else{
-							logger.info("getRulesListByRulesID: Success");
-							if(ruleslist[0].notificationType == 0){
-								httprequest.sendHttpRequest(ruleslist[0].hostname, ruleslist[0].port, ruleslist[0].path, ruleslist[0].method, {subject: eachElem.subject, value: eachElem.subjectValue,rule:eachResult.ruleid});
-							}else{
-								thiscron[eachResult.notType-1].proceed = true;
-								thiscron[eachResult.notType-1].list.push({subject: eachElem.subject, value: eachElem.subjectValue,rule:eachResult.ruleid})
+							thiscron[eachResult.notType-1].proceed = true;
+							thiscron[eachResult.notType-1].list.push({subject: eachElem.subject, value: eachElem.subjectValue,rule:eachResult.ruleid});
+							//Detect the End of Loop
+							if(reason.length-1 == reasonidx && eachElem.results.length-1 == resultidx){
+								ToSendEmail(thiscron);
 							}
 						}
-					})
-				}
-			//detect end loop
-			if(reason.length-1 == reasonidx && eachElem.results.length-1 == resultidx){
-
-				if(process.env.EMAILON){
-					logger.info(handler,"Email feature is activated");
-					var crontab = require('node-crontab');
-					
-					thiscron.forEach(function(eachCron){
-						if(eachCron.proceed){
-							mysql.getRulesListByRulesID(eachCron.list.rule, function(isOK, ruleslist){
-								if(isOK){
-									emailTo = ruleslist[0].emailTo;
-									logger.info(handler,"Creating scheduled job for notification type",eachCron.type,"Destination list",emailTo,"Email Content",eachCron.list,"Scheduled for day",eachCron.schedule);
-									var jobid=crontab.scheduleJob("* * * "+eachCron.schedule+" * *", function(){
-										email.sendEmail(emailTo, JSON.stringify(eachCron.list));
-									},null,null,false);
-									logger.info(handler,"Scheduled job created with id",jobid);
-								}
-							})
-						}
-						
-					})
-				}else{
-					logger.info(handler, "Email feature is NOT activated");
-				}
+					}
+				})
 			}
 		})
+	})
+}
+
+//function ToSendEmail(thiscron)
+function ToSendEmail(thiscron){
+	if(process.env.EMAILON){
+		logger.info(handler,"Email feature is activated");
+		var crontab = require('node-crontab');
+
+		thiscron.forEach(function(eachCron){
+			if(eachCron.proceed){
+				mysql.getRulesListByRulesID(eachCron.list.rule, function(isOK, ruleslist){
+					if(isOK){
+						emailTo = ruleslist[0].emailTo;
+						logger.info(handler,"Creating scheduled job for notification type",eachCron.type,"Destination list",emailTo,"Email Content",eachCron.list,"Scheduled for day",eachCron.schedule);
+						var jobid=crontab.scheduleJob("* * * "+eachCron.schedule+" * *", function(){
+							email.sendEmail(emailTo, JSON.stringify(eachCron.list));
+						},null,null,false);
+						logger.info(handler,"Scheduled job created with id",jobid);
+					}
+				})
+			}
+
 		})
-	}+	}else{
+		logger.info(handler, "Email feature is NOT activated");
+	}
+}