mirror of
https://github.com/arnaucube/commonroutesServer.git
synced 2026-02-28 05:26:42 +01:00
travel accept join petition implemented
This commit is contained in:
@@ -22,6 +22,7 @@ exports.getTravelById = function (req, res) {
|
|||||||
.lean()
|
.lean()
|
||||||
.populate('user', 'username avatar telegram phone')
|
.populate('user', 'username avatar telegram phone')
|
||||||
.populate('joins', 'username avatar')
|
.populate('joins', 'username avatar')
|
||||||
|
.populate('joinPetitions', 'username avatar')
|
||||||
.populate('comments', 'comment user')
|
.populate('comments', 'comment user')
|
||||||
.exec(function (err, travel) {
|
.exec(function (err, travel) {
|
||||||
if (err) return res.send(500, err.message);
|
if (err) return res.send(500, err.message);
|
||||||
@@ -228,6 +229,74 @@ exports.unJoin = function(req, res) {
|
|||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
|
exports.acceptJoin = 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.joins.push(JSON.parse(JSON.stringify(travel.joinPetitions[indexPetition])));
|
||||||
|
travel.joinPetitions.splice(indexPetition, 1);
|
||||||
|
console.log(travel);
|
||||||
|
}
|
||||||
|
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+" accepts 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.getTravelsByUserId = function(req, res) {
|
exports.getTravelsByUserId = function(req, res) {
|
||||||
travelModel.find({
|
travelModel.find({
|
||||||
user: req.params.userid
|
user: req.params.userid
|
||||||
|
|||||||
@@ -128,6 +128,8 @@ apiRoutes.route('/travels/unjoin/:travelid')
|
|||||||
.post(travelCtrl.unJoin);
|
.post(travelCtrl.unJoin);
|
||||||
apiRoutes.route('/travels/byuser/id/:userid')
|
apiRoutes.route('/travels/byuser/id/:userid')
|
||||||
.get(travelCtrl.getTravelsByUserId);
|
.get(travelCtrl.getTravelsByUserId);
|
||||||
|
apiRoutes.route('/travels/acceptJoin/:travelid')
|
||||||
|
.post(travelCtrl.acceptJoin);
|
||||||
//FINS AQUÏ COMPROVAT
|
//FINS AQUÏ COMPROVAT
|
||||||
|
|
||||||
apiRoutes.route('/travels/comment/:travelid')
|
apiRoutes.route('/travels/comment/:travelid')
|
||||||
|
|||||||
Reference in New Issue
Block a user