Parent: [98b6e7] (diff)

Child: [489239] (diff)

Download this file

controllerV0.js    232 lines (219 with data), 7.3 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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
/*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');
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.createApp= function (req, res) {
logger.info("Request type: ",req.method, "Request URL: ", req.originalUrl);
var handler = "backend.controllers.createApp";
logger.info("Starting: ",handler);
if(Object.keys(req.body).length != 0){
database.insertVappByDeveloperid(req.body, function(isOK, data){
if(!isOK) {
logger.info(handler,data);
sendResponse(500, {success:false, data:data},"Create Vapp: "+data, res);
}else {
logger.info(handler,data);
sendResponse(200, {success: true, data:data},"Create Vapp: Success", res);
}
});
}else{
logger.error(handler);
res.end();
}
};
/*************************
Description: Handler to receive new notification
output type: N/A
JSON body payload structure
{
"emailTo": [
"string",
"string"
],
"token": "string",
"body": [
{
"subject" : "stirng",
"subjectValue" : "string"
},{
"subject" : "string",
"subjectValue" : "string"
},{
"subject" : "stirng",
"subjectValue" : "string"
}
]
}
*/
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.notificationsCheck(req.body.body, req.body.emailTo, req.body.token, function(isOK, reason){
if(isOK) {
sendResponse(200, {success: true, reason:reason},"Notification: Success", res);
}else {
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);
if(Object.keys(req.body).length != 0){
database.insertNotificationRules(req.body, function(isOK, data){
if(!isOK) {
logger.info(handler,data);
sendResponse(500, {success:false, data:data},"Create Rules: "+data, res);
}else {
logger.info(handler,data);
sendResponse(200, {success: true, data:data},"Create Rules: Success", 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(500, {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);
ruleid = req.params.ruleid;
database.retrieveStatisticsList(ruleid, 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, data:data},"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, data:data},"Delete 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();
}