arnaucube
arnaucube/commonroutesServerPublic Self-hosted repository
decline join travel, and leave travel implemented
arnaucode idoctnef@openmailbox.org
Mon, 30 Jan 2017 12:41:34 +0100
commit

7e53192ccd70bb0503263d184ca925f0989445fa

parent

28b8aa896bd611ebd02fed98fe5020529dea1a40

2 files changed, 130 insertions(+), 0 deletions(-)

jump to
M controllers/travelController.js → controllers/travelController.js

@@ -229,6 +229,73 @@

}); }; + +exports.declineJoin = function(req, res) { + userModel.findOne({'token': req.headers['x-access-token']}) + .exec(function(err, userOwner){ + if (err) return res.send(500, err.message); + if (!userOwner) { + res.json({success: false, message: 'User not found.'}); + } else if (userOwner) { + travelModel.findOne({ + _id: req.params.travelid, + user: userOwner._id, + joinPetitions: req.body.userid + }) + .exec(function(err, travel){ + if (err) return res.send(500, err.message); + if (!travel) { + res.json({success: false, message: 'travel not found. You can not join a travel if you have created it, or if you have already joined'}); + } else if (travel) { + var indexPetition=-1; + for(var i=0; i<travel.joinPetitions.length; i++) + { + if(travel.joinPetitions[i].equals(req.body.userid)) + { + indexPetition=JSON.parse(JSON.stringify(i)); + } + } + if(indexPetition>-1) + { + travel.joinPetitions.splice(indexPetition, 1); + } + travel.save(function(err, travel) { + if(err) return res.send(500, err.message); + + //start saving notification, get user owner of travel + userModel.findOne({_id: req.body.userid}) + .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) { + //notification + var notification = new notificationModel({ + concept: "travel", + message: "user "+userOwner.username+" declines your petition for "+travel.title, + date: new Date(), + icon: 'travel.png', + link: "travels/" + travel._id + }); + notification.save(function(err, notification) { + if (err) return res.send(500, err.message); + user.notifications.push(notification._id); + user.save(function(err, user) { + if (err) return res.send(500, err.message); + + console.log("notification saved"); + exports.getTravelById(req, res); + }); + }); + } + });//end saving notification + });//end of travel save + }//end of else if travel + }); + }//end of else if user + }); +}; + exports.acceptJoin = function(req, res) { userModel.findOne({'token': req.headers['x-access-token']}) .exec(function(err, userOwner){

@@ -294,6 +361,65 @@ });//end of travel save

}//end of else if travel }); }//end of else if user + }); +}; + +exports.leave = function(req, res) { + userModel.findOne({'token': req.headers['x-access-token']}) + .exec(function(err, userLeaving){ + if (!userLeaving) { + res.json({success: false, message: 'userLeaving not found.'}); + } else if (userLeaving) { + travelModel.findOne({ + _id: req.params.travelid, + joins: userLeaving._id + }) + .exec(function(err, travel){ + if (err) return res.send(500, err.message); + if (!travel) { + res.json({success: false, message: 'can not unjoin this travel'}); + } else if (travel) { + for(var i=0; i<travel.joins.length; i++) + { + if(travel.joins[i].equals(userLeaving._id)) + { + travel.joins.splice(i, 1); + } + } + travel.save(function(err, travel) { + if(err) return res.send(500, err.message); + //start saving notification, get user owner of travel + userModel.findOne({_id: travel.user}) + .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) { + //notification + var notification = new notificationModel({ + concept: "leave", + message: "user "+userLeaving.username+" leaves your travel "+travel.title, + date: new Date(), + icon: 'leave.png', + link: "travels/" + travel._id + }); + notification.save(function(err, notification) { + if (err) return res.send(500, err.message); + user.notifications.push(notification._id); + user.save(function(err, user) { + if (err) return res.send(500, err.message); + + console.log("notification saved"); + exports.getTravelById(req, res); + }); + }); + } + });//end saving notification + }); + } + }); + } + }); };
M server.js → server.js

@@ -126,8 +126,12 @@ apiRoutes.route('/travels/join/:travelid')

.post(travelCtrl.addJoinPetition); apiRoutes.route('/travels/unjoin/:travelid') .post(travelCtrl.unJoin); +apiRoutes.route('/travels/leave/:travelid') + .post(travelCtrl.leave); apiRoutes.route('/travels/byuser/id/:userid') .get(travelCtrl.getTravelsByUserId); +apiRoutes.route('/travels/declineJoin/:travelid') + .post(travelCtrl.declineJoin); apiRoutes.route('/travels/acceptJoin/:travelid') .post(travelCtrl.acceptJoin); //FINS AQUÏ COMPROVAT