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