'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]);
}
});
}
};