Child: [65557d] (diff)

Download this file

user.js    146 lines (128 with data), 4.4 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
'use strict';
var mongoose = require('mongoose');
var Device = new mongoose.Schema({
deviceID: {type: String, required: true, trim: true, default: 'android-1234567'},
name: {type: String, required: true, trim: true, default: 'S7'},
config: {type: String, required: true, default: 'default', trim: true},
status: {type: String, required: true, default: 'disconnected', trim: true},
// timestamps: {
// creation: {type: Date, default: Date.now}
// }
}, {_id: false});
var Notification = new mongoose.Schema({
title: {type: String, required: true, trim: true},
msg: {type: String, required: true},
status: {type: String, required: true, trim: true},
type: {type: String, required: true, trim: true},
timestamp: {type: Date, default: Date.now}
});
var User = new mongoose.Schema({
_id: {type: String, required: true, trim: true},
devices: {type: [Device]},
notifications: {type: [Notification]}
// firstname: {type: String, required: true, trim: true},
// lastname: {type: String, required: true, trim: true}
});
var User = mongoose.model('c2net_tut', User);
module.exports = {
createUser: function(data){
User.create({
_id: data.user,
devices: [{ deviceID: data.deviceID, name: data.deviceName, status: 'connected' }]
// firstname: data.firstname,
// lastname: data.lastname
}, function (err, user) {
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);
}
});
},
// search the user by id
searchUser: function(userID,callback){
User.find({ _id: userID }, function (err, user) {
if (err){
console.log(err);
}else if (user == ''){
console.log('user is not found!');
}else{
callback(user);
console.log(user);
}
});
},
// update the device status
updateStatus: function(userID, deviceID, status){
User.findOneAndUpdate({ "_id": userID, "devices.deviceID": deviceID
},{ "$set": { "devices.$.status": status}
}, function (err, device) {
if (err){
console.log(err);
}else if (device == ''){
console.log('The device is not found!');
}else{
console.log(device);
}
});
},
newNotification: function(userID, notif){
var notification = 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)
console.log('Success: add new notification!');
});
});
},
defaultNotication: function(userID, lastId, callback){
User.find({ _id: userID }, function (err, user) {
if (err){
console.log(err);
}else{
var notifsArray = user[0].notifications;
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);
console.log(index);
callback(notifications);
}
}
});
},
getStatistics: function(userID, callback){
User.aggregate([
{ $match:{_id:userID}},
{ $group: { status: '$notifications.status', total: { $sum: 1 } } }
]).exec(function ( e, d ) {
console.log(d);
callback(d);
});
},
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 == ''){
console.log('The notification is not found!');
}else{
var notifis =data.notifications;
var index = notifis.map(function(d) { return d['_id'].toString();}).indexOf(notifisId);
console.log(notifis[index]);
}
});
}
};