/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
//controllerV0 - First Version
var database = require('./mysql');
var logger = require('../config/logger.js');
var controllerNotification = require('../backend/notificationManager');
var smartSystem = require('../backend/smartRuleSystem');
module.exports.registerVapp= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.registerVapp";
logger.info("Starting: ",handler);
if(Object.keys(req.body).length != 0){
database.insertVapp(req.body, function(isOK, data){
if(!isOK) {
logger.error(handler,data);
sendResponse(500, {success:false, data:data},"Register Vapp: "+data, res);
}else {
logger.info(handler,data);
sendResponse(200, {success: true, data:data},"Register Vapp: Success", res);
}
});
}else{
logger.error(handler);
res.end();
}
}
module.exports.createNotifications= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.createNotifications";
logger.info("Starting: ",handler);
//Check if request body is not empty
if(Object.keys(req.body).length != 0){
//Check if Token Exists
database.checkIfTokenExists(req.body.token, function(isOK, reason){
if(isOK) {
controllerNotification.notificationHandler(req.body.body, req.body.token, function(ifOK, reason, notificationsCreated){
if(isOK) {
//execute email/http request based on the reason
controllerNotification.cronDispatcher(reason);
smartSystem.smartRulesHandler();
console.log("200 - reason: " + reason);
sendResponse(200, {success: true, reason:reason},"Notification: Success", res);
}else {
console.log("500 - reason: " + reason);
sendResponse(500, {success: false, reason: reason},"Notification: "+reason, res);
}
});
}else{
logger.error(handler);
sendResponse(500, {success: false, reason: reason},"Check Token: "+reason, res);
res.end();
}
});
}
}
module.exports.createNotificationsRules= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.createNotificationsRules";
logger.info("Starting: ",handler);
var response = [];
if(Object.keys(req.body).length != 0){
req.body.forEach(function(eachBody, index){
database.insertNotificationRules(eachBody, function(isOK, data){
if(!isOK) {
logger.info(handler,data);
response.push({description: eachBody.description, success: true })
}else {
logger.info(handler,data);
response.push({description: eachBody.description, success: false })
}
});
if(req.body.length-1 == index){
sendResponse(200, {success:true},"Create Rules: "+response, res);
}
})
}else{
logger.error(handler);
res.end();
}
}
module.exports.getNotifications= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.getNotifications";
logger.info("Starting: ",handler);
appid = req.params.appid;
database.retrieveNotificationList(appid, function(isOK, rows){
if(!isOK) {
logger.info(handler,rows);
sendResponse(500, {success:false, data:rows},"Get Notifications: "+rows, res);
}else {
logger.info(handler,rows);
sendResponse(200, {success: true, data:rows},"Get Notifications: Success",res);
}
});
}
module.exports.getApps= function (req, res) {
logger.info("Request type:",req.method, "Request URL:", req.originalUrl);
var handler = "backend.controllers.getApps";
logger.info("Starting: ",handler);
developerid = req.params.developerid;
database.retrieveApp(developerid, function(isOK, rows){
if(!isOK) {
logger.info(handler,rows);
sendResponse(404, {success:false, data:rows},"Get Vapps: "+rows,res);
}else {
logger.info(handler,rows);
sendResponse(200, {success: true, data:rows},"Get Vapps: Success",res);
}
});
}
/*output:
{
success: true/false,
data: <>
}*/
module.exports.getRules= function (req, res) {
logger.info("Request type:",req.method, "Request URL:", req.originalUrl);
var handler = "backend.controllers.getRules";
logger.info("Starting: ",handler);
appid = req.params.appid;
database.retrieveRulesList(appid, function(isOK, rows){
if(!isOK) {
logger.info(handler,rows);
sendResponse(500, {success:false, data:rows},"Get Rules: "+rows,res);
}else {
logger.info(handler,rows);
sendResponse(200, {success: true, data:rows},"Get Rules: Success",res);
}
});
}
module.exports.getStatistics= function (req, res) {
logger.info("Request type:",req.method, "Request URL:", req.originalUrl);
var handler = "backend.controllers.getStatistics";
logger.info("Starting: ",handler);
rulesid = req.params.rulesid;
database.retrieveStatisticsList(rulesid, function(isOK, rows, total){
if(!isOK) {
logger.info(handler,rows);
sendResponse(500, {success:false, data:rows,total},"Get Statistics: "+rows,res);
}else {
logger.info(handler,rows);
sendResponse(200, {success: true, data:rows,total},"Get Statistics: Success",res);
}
});
}
module.exports.deleteVapp= function (req, res) {
logger.info("Request type:",req.method, "Request URL:", req.originalUrl);
var handler = "backend.controllers.deleteVapp";
logger.info("Starting: ",handler);
appid = req.params.appid;
database.deleteVapp(appid, function(isOK, data){
if(!isOK) {
logger.info(handler,data);
sendResponse(500, {success:false, data:data},"Delete Vapp: "+data,res);
}else {
logger.info(handler,data);
sendResponse(200, {success: true},"Delete Vapp: Success",res);
}
});
}
module.exports.deleteRule= function (req, res) {
logger.info("Request type:",req.method, "Request URL:", req.originalUrl);
var handler = "backend.controllers.deleteRule";
logger.info("Starting: ",handler);
rulesid = req.params.rulesid;
database.deleteRule(rulesid, function(isOK, data){
if(!isOK) {
logger.info(handler,data);
sendResponse(500, {success:false, data:data},"Delete Rule: "+data,res);
}else {
logger.info(handler,data);
sendResponse(200, {success: true},"Delete Rule: Success",res);
}
});
}
module.exports.editRule= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.editRule";
logger.info("Starting: ",handler);
database.updateRule(req.body, function(isOK, data){
if(!isOK) {
logger.info(handler,data);
sendResponse(500, {success:false, data:rows},"Update Rule: "+data, res);
}else {
logger.info(handler,data);
sendResponse(200, {success: true, data:data},"Update Rule: Success",res);
}
});
}
module.exports.getAllRule= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.getNotifications";
logger.info("Starting: ",handler);
rulesid = req.params.rulesid;
database.retrieveAllRule(rulesid, function(isOK, rows){
if(!isOK) {
logger.info(handler,rows);
sendResponse(500, {success:false, data:rows},"Get All Specific Rule: "+rows, res);
}else {
logger.info(handler,rows);
sendResponse(200, {success: true, data:rows},"Get All Specific Rule: Success",res);
}
});
}
/*
INPUT:
statusCode: number 200/400/500 etc
JSONbody: the content of response
*/
function sendResponse(statusCode, JSONbody, type, res){
var handler = "backend.controllers.sendResponse";
logger.info(type);
//console.log("Sending response to " + type);
//console.log(type);
res.writeHead(statusCode, {'Content-type': 'application/json'});
res.write(JSON.stringify(JSONbody));
res.end();
}