notification system fixed
arnaucode idoctnef@openmailbox.org
Fri, 23 Jun 2017 02:56:30 +0200
2 files changed,
44 insertions(+),
3 deletions(-)
M
controllers/userController.js
→
controllers/userController.js
@@ -190,6 +190,24 @@ res.status(200).jsonp(user.likes);
} }); }; +exports.getNumNotificationsByToken = function(req, res) { + userModel.findOne({ + 'token': req.headers['x-access-token'] + }) + .lean() + .exec(function(err, user) { + if (err) return res.send(500, err.message); + if (!user) { + res.json({ + success: false, + message: 'User not found.' + }); + } else if (user) { + + res.status(200).jsonp(user.notifications); + } + }); +}; exports.getNotifications = function(req, res) { userModel.findOne({ 'token': req.headers['x-access-token']@@ -205,9 +223,9 @@ message: 'User not found.'
}); } else if (user) { - //res.status(200).jsonp(user.notifications); notificationModel.find({ - 'user': user._id + 'user': user._id, + 'state': 'pendent' }) .lean() .exec(function(err, notifications) {@@ -218,10 +236,31 @@ success: false,
message: 'No pendent notifications.' }); } else if (notifications) { - + //here, maybe in the future is better delete the viewed notifications + notificationModel.update( + {state: "pendent"}, + {state: "viewed"}, + {multi: true}, + function(err){ + if(err){ + console.log(err); + } + } + ); res.status(200).jsonp(notifications); } }); + + //now, clean notifications count from user + userModel.update( + {'token': req.headers['x-access-token']}, + {notifications: []}, + function(err){ + if(err){ + console.log(err); + } + } + ); } }); };