|
a/backend/webserver/api/c2net/db/user.js |
|
b/backend/webserver/api/c2net/db/user.js |
|
... |
|
... |
15 |
status: {type: String, required: true, trim: true},
|
15 |
status: {type: String, required: true, trim: true},
|
16 |
type: {type: String, required: true, trim: true},
|
16 |
type: {type: String, required: true, trim: true},
|
17 |
timestamp: {type: Date, default: Date.now}
|
17 |
timestamp: {type: Date, default: Date.now}
|
18 |
});
|
18 |
});
|
19 |
|
19 |
|
20 |
var UserSchema = new mongoose.Schema({
|
20 |
var User = new mongoose.Schema({
|
21 |
_id: {type: String, required: true, trim: true},
|
21 |
_id: {type: String, required: true, trim: true},
|
22 |
devices: {type: [Device]},
|
22 |
devices: {type: [Device]},
|
23 |
notifications: {type: [Notification]}
|
23 |
notifications: {type: [Notification]}
|
24 |
});
|
24 |
});
|
25 |
|
25 |
|
26 |
var User = mongoose.model('c2net_tut', UserSchema);
|
26 |
var User = mongoose.model('c2net_tut', User);
|
27 |
|
27 |
|
28 |
module.exports = {
|
28 |
module.exports = {
|
29 |
|
29 |
|
30 |
createUser: function (data) {
|
30 |
createUser: function(data){
|
31 |
User.create({
|
31 |
User.create({
|
32 |
_id: data.user,
|
32 |
_id: data.user,
|
33 |
devices: [{ deviceID: data.deviceID, name: data.deviceName, status: 'connected' }]
|
33 |
devices: [{ deviceID: data.deviceID, name: data.deviceName, status: 'connected' }]
|
34 |
}, function (err, user) {
|
34 |
}, function (err, user) {
|
35 |
if (err) {
|
35 |
if (err){
|
36 |
if (err.code === 11000) {
|
36 |
if(err.code == 11000){
|
37 |
console.error('The user is already exist in db');
|
37 |
console.error('The user is already exist in db');
|
38 |
} else {
|
38 |
}else{
|
39 |
console.error(err);
|
39 |
console.error(err);
|
|
|
40 |
}
|
|
|
41 |
}else{
|
|
|
42 |
//console.log("User %s is saved to db.", data.user);
|
|
|
43 |
//console.log(user);
|
40 |
}
|
44 |
}
|
41 |
} else {
|
|
|
42 |
console.log('User %s is saved to db.', data.user);
|
|
|
43 |
console.log(user);
|
|
|
44 |
}
|
|
|
45 |
});
|
45 |
});
|
46 |
},
|
46 |
},
|
47 |
|
47 |
|
48 |
// search the user by id
|
48 |
// search the user by id
|
49 |
searchUser: function (userID, callback) {
|
49 |
searchUser: function(userID,callback){
|
50 |
User.find({ _id: userID }, function (err, user) {
|
50 |
User.find({ _id: userID }, function (err, user) {
|
51 |
if (err) {
|
51 |
if (err){
|
52 |
console.log(err);
|
52 |
console.log(err);
|
53 |
} else if (user === '') {
|
53 |
}else if (user == ''){
|
54 |
console.log('user is not found!');
|
54 |
console.log('user is not found!');
|
55 |
} else {
|
55 |
}else{
|
56 |
callback(user);
|
56 |
callback(user);
|
57 |
console.log(user);
|
57 |
//console.log(user);
|
58 |
}
|
58 |
}
|
59 |
});
|
59 |
});
|
60 |
},
|
60 |
},
|
61 |
|
61 |
|
62 |
// update the device status
|
62 |
// update the device status
|
63 |
updateStatus: function (userID, deviceID, status) {
|
63 |
updateStatus: function(userID, deviceID, status){
|
64 |
User.findOneAndUpdate({ '_id': userID, 'devices.deviceID': deviceID
|
64 |
User.findOneAndUpdate({ "_id": userID, "devices.deviceID": deviceID
|
65 |
}, {'$set': { 'devices.$.status': status }
|
65 |
},{ "$set": { "devices.$.status": status}
|
66 |
}, function (err, device) {
|
66 |
}, function (err, device) {
|
67 |
if (err) {
|
67 |
if (err){
|
68 |
console.log(err);
|
68 |
console.log(err);
|
69 |
} else if (device === '') {
|
69 |
}else if (device == ''){
|
70 |
console.log('The device is not found!');
|
70 |
console.log('The device is not found!');
|
71 |
} else {
|
71 |
}else{
|
72 |
console.log(device);
|
72 |
console.log(device);
|
73 |
}
|
73 |
}
|
74 |
});
|
74 |
});
|
75 |
},
|
75 |
},
|
76 |
|
76 |
|
77 |
// add new notification into database
|
|
|
78 |
newNotification: function (userID, notif) {
|
77 |
newNotification: function(userID, notif){
|
79 |
var notification = notif;
|
78 |
var notification = notif;
|
80 |
module.exports.searchUser(userID, function (user, notification) {
|
79 |
module.exports.searchUser(userID, function(user, notification){
|
81 |
console.log(notif);
|
80 |
console.log (notif);
|
82 |
user[0].notifications.push(notif);
|
81 |
user[0].notifications.push(notif);
|
83 |
|
82 |
|
84 |
user[0].save(function (err) {
|
83 |
user[0].save(function (err) {
|
85 |
if (err) console.log(err);
|
84 |
if (err) console.log(err)
|
86 |
console.log('Success: add new notification!');
|
85 |
console.log('Success: add new notification!');
|
87 |
});
|
86 |
});
|
|
|
87 |
|
88 |
});
|
88 |
});
|
89 |
},
|
89 |
},
|
90 |
|
90 |
|
91 |
// show 10 latest notification
|
|
|
92 |
defaultNotication: function (userID, lastId, callback) {
|
91 |
defaultNotication: function(userID, lastId, callback){
|
93 |
User.find({ _id: userID }, function (err, user) {
|
92 |
User.find({ _id: userID }, function (err, user) {
|
94 |
if (err) {
|
93 |
if (err){
|
95 |
console.log(err);
|
94 |
console.log(err);
|
96 |
} else {
|
95 |
}else{
|
97 |
var notifsArray = user[0].notifications;
|
96 |
var notifsArray = user[0].notifications;
|
98 |
if (lastId === 'inital') {
|
97 |
if (lastId == "inital"){
|
99 |
var notifications = notifsArray.sort(function (a, b) { return b.timestamp - a.timestamp; }).slice(0, 10);
|
98 |
var notifications = notifsArray.sort(function(a, b){return b.timestamp-a.timestamp}).slice( 0, 10);
|
100 |
callback(notifications);
|
99 |
callback(notifications);
|
101 |
} else {
|
100 |
}else{
|
102 |
var sortArray = notifsArray.sort(function (a, b) { return b.timestamp - a.timestamp; });
|
101 |
var sortArray = notifsArray.sort(function(a, b){return b.timestamp-a.timestamp});
|
103 |
var index = sortArray.map(function (d) { return d['_id'].toString(); }).indexOf(lastId);
|
102 |
var index = sortArray.map(function(d) { return d['_id'].toString();}).indexOf(lastId);
|
104 |
var notifications = sortArray.slice(index + 1, index + 11);
|
103 |
var notifications = sortArray.slice( index+1, index+11);
|
105 |
console.log(index);
|
104 |
console.log(index);
|
106 |
callback(notifications);
|
105 |
callback(notifications);
|
107 |
}
|
106 |
}
|
108 |
}
|
107 |
}
|
109 |
});
|
108 |
});
|
110 |
},
|
109 |
},
|
111 |
|
110 |
|
112 |
// get the total number of notifications
|
|
|
113 |
getStatistics: function (userID, callback) {
|
111 |
getStatistics: function(userID, callback){
|
114 |
User.aggregate([
|
112 |
User.aggregate([
|
115 |
{$match: { _id: userID }},
|
113 |
{ $match:{_id:userID}},
|
116 |
{ $group: { status: '$notifications.status', total: { $sum: 1 } } }
|
114 |
{ $group: { status: '$notifications.status', total: { $sum: 1 } } }
|
117 |
|
115 |
|
118 |
]).exec(function (e, d) {
|
116 |
]).exec(function ( e, d ) {
|
119 |
console.log(d);
|
117 |
console.log(d);
|
120 |
callback(d);
|
118 |
callback(d);
|
121 |
});
|
119 |
});
|
122 |
},
|
120 |
},
|
123 |
|
121 |
|
124 |
// change notification status from unread to read
|
|
|
125 |
readNotification: function (userID, notifisId) {
|
122 |
readNotification: function(userID, notifisId){
|
126 |
User.findOneAndUpdate({ '_id': userID, 'notifications._id': notifisId
|
123 |
User.findOneAndUpdate({ "_id": userID, "notifications._id": notifisId
|
127 |
}, {'$set': {'notifications.$.status': 'read'}
|
124 |
},{ "$set": { "notifications.$.status": "read"}
|
128 |
}, function (err, data) {
|
125 |
}, function (err, data) {
|
129 |
if (err) {
|
126 |
if (err){
|
130 |
console.log(err);
|
127 |
console.log(err);
|
131 |
} else if (data === '') {
|
128 |
}else if (data == ''){
|
132 |
console.log('The notification is not found!');
|
129 |
console.log('The notification is not found!');
|
133 |
} else {
|
130 |
}else{
|
134 |
var notifis = data.notifications;
|
131 |
var notifis =data.notifications;
|
135 |
var index = notifis.map(function (d) { return d['_id'].toString(); }).indexOf(notifisId);
|
132 |
var index = notifis.map(function(d) { return d['_id'].toString();}).indexOf(notifisId);
|
136 |
console.log(notifis[index]);
|
133 |
console.log(notifis[index]);
|
137 |
}
|
134 |
}
|
138 |
});
|
135 |
});
|
139 |
}
|
136 |
}
|
140 |
|
137 |
|