Switch to side-by-side view

--- a/backend/notificationManager.js
+++ b/backend/notificationManager.js
@@ -1,16 +1,17 @@
 /*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
 
-//Notification Manager to check notifications with rules
+//notificationManager to check notifications with rules
 
 var mysql  = require('../controllers/mysql');
 var email  = require('../controllers/emailer');
+var httprequest  = require('../controllers/httpRequest');
 var logger = require('../config/logger.js');
 
 //Handle the notifications request
-module.exports.notificationHandler = function(body, destinations, appToken, cb){
+module.exports.notificationHandler = function(body, 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));
+	logger.info("backend.controllers.notificationHandler with payload: "+JSON.stringify(body) + ";" + JSON.stringify(appToken));
 	//prepare the http response
 	var response = [];
 	var jobsdone = 0;
@@ -28,7 +29,7 @@
 				response.push({subject: eachBody.subject, subjectValue: eachBody.subjectValue, total: 0, results:[]});
 				//iterate the rules list
 				rules.forEach(function(eachRule, ruleIndex){
-					applyRule(destinations, eachBody, eachRule, appToken, function(ruleApplied, reason){
+					applyRule(eachBody, eachRule, appToken, function(ruleApplied, reason){
 						if(ruleApplied){
 							if(!reason){
 								//rule applied
@@ -93,7 +94,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
 output: response concatenated
- */
+*/
 function concatenateResponse (response, body, rule, success, comment, cb){
 	response.forEach(function(eachResponse){
 		if(eachResponse.subject == body.subject && eachResponse.subjectValue == body.subjectValue){
@@ -114,14 +115,13 @@
 //	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){
+function applyRule(source, rule, token, cb){
 	var handler = "applyRule";
 	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,
@@ -130,21 +130,21 @@
 		}
 		switch (rule.conditionValue){
 			case ">":
-				ruleApplied = ( parseFloat(source.subjectValue) > parseFloat(rule.controlValue) ) ? true : false
-				break;
+			ruleApplied = ( parseFloat(source.subjectValue) > parseFloat(rule.controlValue) ) ? true : false
+			break;
 			case "<":
-				ruleApplied = ( parseFloat(source.subjectValue) < parseFloat(rule.controlValue) ) ? true : false
-				break;
+			ruleApplied = ( parseFloat(source.subjectValue) < parseFloat(rule.controlValue) ) ? true : false
+			break;
 			case ">=":
-				ruleApplied = ( parseFloat(source.subjectValue) >= parseFloat(rule.controlValue) ) ? true : false
-				break;
+			ruleApplied = ( parseFloat(source.subjectValue) >= parseFloat(rule.controlValue) ) ? true : false
+			break;
 			case "<=":
-				ruleApplied = ( parseFloat(source.subjectValue) <= parseFloat(rule.controlValue) ) ? true : false
-				break;
+			ruleApplied = ( parseFloat(source.subjectValue) <= parseFloat(rule.controlValue) ) ? true : false
+			break;
 			default:
 				//already covered at >= / <=
-		}
-		if(ruleApplied){
+			}
+			if(ruleApplied){
 			//rule is applied
 			logger.info(handler, "rule " + rule.rulesID + " match");
 
@@ -195,21 +195,21 @@
 	})	
 }
 //self explainatory 
-//function cronDispatcher(appToken, input, schedule, emailTo, subject, cb){
-module.exports.cronDispatcher = function(emailTo, 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 = [
+//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:[],
@@ -231,16 +231,25 @@
 			list:[],
 			schedule: today.getDate()
 		}
-	];
-
-
-	
-	reason.forEach(function(eachElem, reasonidx){
-		eachElem.results.forEach(function(eachResult, resultidx){
-			if(eachResult.success){
-				thiscron[eachResult.notType-1].proceed = true;
-				thiscron[eachResult.notType-1].list.push({subject: eachElem.subject, value: eachElem.subjectValue,rule:eachResult.ruleid})
-			}
+		];
+
+		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.emailTo == null){
+								httprequest.sendHttpRequest(ruleslist.hostname, ruleslist.port, ruleslist.path, ruleslist.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})
+							}
+						}
+					})
+				}
 			//detect end loop
 			if(reason.length-1 == reasonidx && eachElem.results.length-1 == resultidx){
 
@@ -250,11 +259,16 @@
 					
 					thiscron.forEach(function(eachCron){
 						if(eachCron.proceed){
-							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);
+							mysql.getRulesListByRulesID(eachCron.list.rule, function(isOK, ruleslist){
+								if(isOK){
+									emailTo = ruleslist.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);
+								}
+							})
 						}
 						
 					})
@@ -263,5 +277,5 @@
 				}
 			}
 		})
-	})
-}+		})
+	}