forked from sclorg/nodejs-ex
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathnotificationModule.js
93 lines (63 loc) · 1.97 KB
/
notificationModule.js
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
function notificationModule() {
var that = this;
var database = require('./database');
/**
* Retrieves a list of all notifications for a user from the database and executes the callback function on it
*/
that.loadNotifications = function(req, callback){
query = "SELECT message FROM notifications WHERE username = ?";
database.connection.query(
query,
[req.params.username],function(err, rows, field){
if(!err){
var completeList = [];
var listEntry;
for(var i=0; i<rows.length; i++){
listEntry = {};
listEntry.message = rows[i].message;
completeList.push(listEntry);
}
console.log(completeList);
callback(completeList);
//remove the notifications once they are collected by the phone
query = "DELETE FROM notifications WHERE username = ?";
database.connection.query(
query,
[req.params.username],function(err, rows, field){
if (err){
console.log(err);
console.log(err.code)
}
});
return;
} else {
console.log("Error querying DB for notifications");
}
callback(null);
});
};
/**
* Handles GET notifications requests
* Sends all notifications for a user to the mobile application
* req.params must contain 'username'
*/
that.getNotifications = function(req, res, next){
if(!req.params.username){
res.json({'error': 'Insufficient Parameters'});
} else {
that.loadNotifications(req, function(completeList){
if(completeList != null) {
if(completeList.length > 0) {
res.send(200, {completeList: completeList, error: "false"});
} else {
res.send(404, {completeList: [], error: "No notifications found"});
}
} else {
res.send(500, {completeList: [], error: "There was an error"});
}
});
return next();
}
};
};
module.exports = new notificationModule();