From 241f92770c57920f1d660a6c80ac5f88abdb0cbb Mon Sep 17 00:00:00 2001 From: arnaucode Date: Tue, 18 Oct 2016 13:19:04 +0200 Subject: [PATCH] notification system almost implemented on backend --- README.md | 6 +++++ controllers/travelController.js | 44 +++++++++++++++++++++++++++++++++ controllers/userController.js | 15 ++++++++++- models/userModel.js | 7 ++++++ 4 files changed, 71 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4a14efc..a6cb385 100644 --- a/README.md +++ b/README.md @@ -56,6 +56,12 @@ frontend app code: https://github.com/idoctnef/collectivecarApp -es veu el telèfon dels users? -com evitem q algú xungu es registri a la app i vegi els viatges i info dels users? + + +-falta acabar notification system + quan mires les notificacions, que avisi al server q ja les has vist, i que el server les marqui com a llegides + link a les notificacions + acabar de mirar q a la app, quan agafa els users del server, tingui actualitzades les notificacions del user al menu ``` diff --git a/controllers/travelController.js b/controllers/travelController.js index 4c898aa..9097d36 100644 --- a/controllers/travelController.js +++ b/controllers/travelController.js @@ -167,6 +167,8 @@ exports.addJoin = function(req, res) { }; travel.joins.push(join); + + travel.save(function(err, travel) { if(err) return res.send(500, err.message); //res.status(200).jsonp(travel); @@ -175,7 +177,28 @@ exports.addJoin = function(req, res) { res.status(200).jsonp(travels); }); }); + + //start saving notification, get user owner of travel + userModel.find({ + username: travel.owner + }, function(err, userowners) { + var userowner=userowners[0]; + //notification + var notification = { + type: "join", + otherusername: user.username, + description: "user "+user.username+" joins your travel "+travel.title, + date: new Date(), + link: "" + }; + userowner.notifications.push(notification); + userowner.save(function(err, userowner) { + console.log("notification saved"); + }); + });//end saving notification + }); + }); }; @@ -202,6 +225,7 @@ exports.doUnjoin = function(req, res) { res.status(200).jsonp(travels); }); }); + }); }); }; @@ -262,6 +286,26 @@ exports.addComment = function(req, res) { res.status(200).jsonp(travels); }); }); + + //start saving notification, get user owner of travel + userModel.find({ + username: travel.owner + }, function(err, userowners) { + var userowner=userowners[0]; + //notification + var notification = { + type: "comment", + otherusername: user.username, + description: "user "+user.username+" comments your travel "+travel.title, + date: new Date(), + link: "" + }; + userowner.notifications.push(notification); + userowner.save(function(err, userowner) { + console.log("notification saved"); + }); + });//end saving notification + }); });//end of userModel.find }; diff --git a/controllers/userController.js b/controllers/userController.js index bc1757d..f775807 100644 --- a/controllers/userController.js +++ b/controllers/userController.js @@ -127,6 +127,7 @@ exports.addFav = function(req, res) { console.log("favRepeated: " + favRepeated); if(favRepeated==false) { + //fav var fav = { userId: tokenuser._id, username: tokenuser.username, @@ -134,9 +135,21 @@ exports.addFav = function(req, res) { }; user.favs.push(fav); + //notification + var notification = { + type: "fav", + otherusername: tokenuser.username, + description: "user "+tokenuser.username+" favs you", + date: new Date(), + link: "" + }; + user.notifications.push(notification); + user.save(function(err, user) { if(err) return res.send(500, err.message); - //res.status(200).jsonp(travel); + + + //once saved, send the users json to client userModel.find(function(err, users) { if(err) res.send(500, err.message); res.status(200).jsonp(users); diff --git a/models/userModel.js b/models/userModel.js index 0d91952..e381614 100644 --- a/models/userModel.js +++ b/models/userModel.js @@ -22,6 +22,13 @@ var userSchema = new Schema({ username: { type: String }, userId: { type: String }, avatar: { type: String } + }], + notifications: [{ + type: { type: String },//fav, comment, join + otherusername: { type: String }, + description: { type: String }, + date: { type: Date }, + link: { type: String } }] })