Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed reject friend request button #21

Merged
merged 1 commit into from
Jun 21, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions controllers/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,51 @@ AddFriend: (req, res) => {
throw err;
});
},

RejectFriendRequest: (req, res) => {
const currentUser = req.session.user;
const friendEmail = req.body.friendEmail;

// Remove the friend request from the current user's friendRequests
User.findOneAndUpdate(
{ email: currentUser.email },
{ $pull: { friendRequests: friendEmail } },
{ new: true },
(err, updatedUser) => {
if (err) {
throw err;
}

// Remove the current user from the friend's sentFriendRequests
User.findOneAndUpdate(
{ email: friendEmail },
{ $pull: { sentFriendRequests: currentUser.email } },
{ new: true },
(err) => {
if (err) {
throw err;
}

// Re-query the user data with updated friendRequests and sentFriendRequests
User.find({}, (err) => {
if (err) {
throw err;
}

// Update the friendRequests and sentFriendRequests in the session
currentUser.friendRequests = updatedUser.friendRequests;

// Redirect to the Index route to render the updated data
res.redirect('/profile');
});
}
);
}
);
},


};


module.exports = ProfileController;
2 changes: 1 addition & 1 deletion routes/profile.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ router.get("/", sessionChecker, ProfileController.Index);
router.post("/remove-friend", sessionChecker, ProfileController.RemoveFriend);
router.post("/add-friend", sessionChecker, ProfileController.AddFriend);
router.post("/accept-friend-request", sessionChecker, ProfileController.AcceptFriendRequest);

router.post("/reject-friend-request", sessionChecker, ProfileController.RejectFriendRequest);

module.exports = router;