/*made by Miguel Rodrigues @ KBZ miguel.rodrigues@knowledgebiz.pt*/
//controllerV0 - First Version
var database = require('./mysql');
var controllerNotification = require('../backend/notificationManager');
module.exports.registerVapp= function (req, res) {
if(Object.keys(req.body).length != 0){
database.insertVapp(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Register Vapp: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Register Vapp: Success", res);
}
});
}else{
res.end();
}
};
module.exports.createApp= function (req, res) {
if(Object.keys(req.body).length != 0){
database.insertVappByDeveloperid(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Create Vapp: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Create Vapp: Success", res);
}
});
}else{
res.end();
}
};
module.exports.createNotifications= function (req, res) {
if(Object.keys(req.body).length != 0){
database.checkIfTokenExists(req.body, function(isOK, reason){
if(isOK) {
controllerNotification.notificationsCheck(req.body, function(isOK, reason){
if(isOK) {
database.insertNotification(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Insert Notification: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Insert Notification: Success", res);
}
});
}else {
sendResponse(500, {success: false, reason: reason},"Notifications Check: "+reason, res);
}
});
}else {
sendResponse(500, {success: false, reason: reason},"Check Token: "+reason, res);
}
});
}else{
res.end();
}
};
module.exports.createNotificationsRules= function (req, res) {
if(Object.keys(req.body).length != 0){
database.insertNotificationRules(req.body, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Create Rules: "+data, res);
}else {
sendResponse(200, {success: true, data:data},"Create Rules: Success", res);
}
});
}else{
res.end();
}
};
module.exports.getNotifications= function (req, res) {
appid = req.params.appid;
database.retrieveNotificationList(appid, function(isOK, rows){
if(!isOK) {
sendResponse(500, {success:false, data:rows},"Get Notifications: "+rows, res);
}else {
sendResponse(200, {success: true, data:rows},"Get Notifications: Success",res);
}
});
};
module.exports.getApps= function (req, res) {
developerid = req.params.developerid;
database.retrieveApp(developerid, function(isOK, rows){
if(!isOK) {
sendResponse(500, {success:false, data:rows},"Get Vapps: "+rows,res);
}else {
sendResponse(200, {success: true, data:rows},"Get Vapps: Success",res);
}
});
};
/*output:
{
success: true/false,
data: <>
}*/
module.exports.getRules= function (req, res) {
appid = req.params.appid;
database.retrieveRulesList(appid, function(isOK, rows){
if(!isOK) {
sendResponse(500, {success:false, data:rows},"Get Rules: "+rows,res);
}else {
sendResponse(200, {success: true, data:rows},"Get Rules: Success",res);
}
});
};
module.exports.deleteVapp= function (req, res) {
appid = req.params.appid;
database.deleteVapp(appid, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Delete Vapp: "+data,res);
}else {
sendResponse(200, {success: true, data:data},"Delete Vapp: Success",res);
}
});
};
module.exports.deleteRule= function (req, res) {
rulesid = req.params.rulesid;
database.deleteRule(rulesid, function(isOK, data){
if(!isOK) {
sendResponse(500, {success:false, data:data},"Delete Rule: "+data,res);
}else {
sendResponse(200, {success: true, data:data},"Delete Rule: Success",res);
}
});
};
/*
INPUT:
statusCode: number 200/400/500 etc
JSONbody: the content of response
*/
function sendResponse(statusCode, JSONbody, type, res){
//console.log("Sending response to " + type);
console.log(type);
res.writeHead(statusCode, {'Content-type': 'application/json'});
res.write(JSON.stringify(JSONbody));
res.end();
}