--- a/backend/webserver/api/c2net/db/user.js
+++ b/backend/webserver/api/c2net/db/user.js
@@ -17,122 +17,119 @@
timestamp: {type: Date, default: Date.now}
});
-var UserSchema = new mongoose.Schema({
+var User = new mongoose.Schema({
_id: {type: String, required: true, trim: true},
devices: {type: [Device]},
notifications: {type: [Notification]}
});
-var User = mongoose.model('c2net_tut', UserSchema);
+var User = mongoose.model('c2net_tut', User);
module.exports = {
- createUser: function (data) {
+ createUser: function(data){
User.create({
_id: data.user,
devices: [{ deviceID: data.deviceID, name: data.deviceName, status: 'connected' }]
}, function (err, user) {
- if (err) {
- if (err.code === 11000) {
- console.error('The user is already exist in db');
- } else {
- console.error(err);
+ if (err){
+ if(err.code == 11000){
+ console.error('The user is already exist in db');
+ }else{
+ console.error(err);
+ }
+ }else{
+ //console.log("User %s is saved to db.", data.user);
+ //console.log(user);
}
- } else {
- console.log('User %s is saved to db.', data.user);
- console.log(user);
- }
});
},
// search the user by id
- searchUser: function (userID, callback) {
+ searchUser: function(userID,callback){
User.find({ _id: userID }, function (err, user) {
- if (err) {
+ if (err){
console.log(err);
- } else if (user === '') {
+ }else if (user == ''){
console.log('user is not found!');
- } else {
+ }else{
callback(user);
- console.log(user);
+ //console.log(user);
}
});
},
// update the device status
- updateStatus: function (userID, deviceID, status) {
- User.findOneAndUpdate({ '_id': userID, 'devices.deviceID': deviceID
- }, {'$set': { 'devices.$.status': status }
+ updateStatus: function(userID, deviceID, status){
+ User.findOneAndUpdate({ "_id": userID, "devices.deviceID": deviceID
+ },{ "$set": { "devices.$.status": status}
}, function (err, device) {
- if (err) {
+ if (err){
console.log(err);
- } else if (device === '') {
+ }else if (device == ''){
console.log('The device is not found!');
- } else {
+ }else{
console.log(device);
}
});
},
-
- // add new notification into database
- newNotification: function (userID, notif) {
+
+ newNotification: function(userID, notif){
var notification = notif;
- module.exports.searchUser(userID, function (user, notification) {
- console.log(notif);
+ module.exports.searchUser(userID, function(user, notification){
+ console.log (notif);
user[0].notifications.push(notif);
-
+
user[0].save(function (err) {
- if (err) console.log(err);
+ if (err) console.log(err)
console.log('Success: add new notification!');
});
+
});
},
-
- // show 10 latest notification
- defaultNotication: function (userID, lastId, callback) {
+
+ defaultNotication: function(userID, lastId, callback){
User.find({ _id: userID }, function (err, user) {
- if (err) {
+ if (err){
console.log(err);
- } else {
+ }else{
var notifsArray = user[0].notifications;
- if (lastId === 'inital') {
- var notifications = notifsArray.sort(function (a, b) { return b.timestamp - a.timestamp; }).slice(0, 10);
+ if (lastId == "inital"){
+ var notifications = notifsArray.sort(function(a, b){return b.timestamp-a.timestamp}).slice( 0, 10);
callback(notifications);
- } else {
- var sortArray = notifsArray.sort(function (a, b) { return b.timestamp - a.timestamp; });
- var index = sortArray.map(function (d) { return d['_id'].toString(); }).indexOf(lastId);
- var notifications = sortArray.slice(index + 1, index + 11);
+ }else{
+ var sortArray = notifsArray.sort(function(a, b){return b.timestamp-a.timestamp});
+ var index = sortArray.map(function(d) { return d['_id'].toString();}).indexOf(lastId);
+ var notifications = sortArray.slice( index+1, index+11);
console.log(index);
callback(notifications);
}
}
});
},
-
- // get the total number of notifications
- getStatistics: function (userID, callback) {
+
+ getStatistics: function(userID, callback){
User.aggregate([
- {$match: { _id: userID }},
+ { $match:{_id:userID}},
{ $group: { status: '$notifications.status', total: { $sum: 1 } } }
- ]).exec(function (e, d) {
+ ]).exec(function ( e, d ) {
console.log(d);
- callback(d);
+ callback(d);
});
},
-
- // change notification status from unread to read
- readNotification: function (userID, notifisId) {
- User.findOneAndUpdate({ '_id': userID, 'notifications._id': notifisId
- }, {'$set': {'notifications.$.status': 'read'}
- }, function (err, data) {
- if (err) {
+
+ readNotification: function(userID, notifisId){
+ User.findOneAndUpdate({ "_id": userID, "notifications._id": notifisId
+ },{ "$set": { "notifications.$.status": "read"}
+}, function (err, data) {
+ if (err){
console.log(err);
- } else if (data === '') {
+ }else if (data == ''){
console.log('The notification is not found!');
- } else {
- var notifis = data.notifications;
- var index = notifis.map(function (d) { return d['_id'].toString(); }).indexOf(notifisId);
+ }else{
+ var notifis =data.notifications;
+ var index = notifis.map(function(d) { return d['_id'].toString();}).indexOf(notifisId);
console.log(notifis[index]);
}
});