From 3aba9f28ffddeadef4c7d2ed3046929bdc500c2e Mon Sep 17 00:00:00 2001 From: Mike Johnston Date: Fri, 30 Jan 2026 18:01:34 -0500 Subject: [PATCH] fix rejected friends --- backend/src/routes/friends.js | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/backend/src/routes/friends.js b/backend/src/routes/friends.js index 4c7aea8..3ea2743 100644 --- a/backend/src/routes/friends.js +++ b/backend/src/routes/friends.js @@ -22,9 +22,9 @@ router.get('/search', authMiddleware, asyncHandler(async (req, res) => { WHERE (username LIKE ? OR email LIKE ?) AND id != ? AND id NOT IN ( - SELECT friend_id FROM friendships WHERE user_id = ? + SELECT friend_id FROM friendships WHERE user_id = ? AND status IN ('accepted', 'pending') UNION - SELECT user_id FROM friendships WHERE friend_id = ? + SELECT user_id FROM friendships WHERE friend_id = ? AND status IN ('accepted', 'pending') ) LIMIT 20`, [searchTerm, searchTerm, req.user.userId, req.user.userId, req.user.userId] @@ -101,10 +101,11 @@ router.post('/request', authMiddleware, asyncHandler(async (req, res) => { throw new AppError('Cannot add yourself as friend', 400); } - // Check if already friends or request exists + // Check if already friends or pending request exists const existing = await query( `SELECT * FROM friendships - WHERE (user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)`, + WHERE (user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?) + AND status IN ('accepted', 'pending')`, [req.user.userId, user_id, user_id, req.user.userId] ); @@ -112,6 +113,14 @@ router.post('/request', authMiddleware, asyncHandler(async (req, res) => { throw new AppError('Friend request already exists or you are already friends', 400); } + // Delete any old rejected requests before creating a new one + await query( + `DELETE FROM friendships + WHERE ((user_id = ? AND friend_id = ?) OR (user_id = ? AND friend_id = ?)) + AND status = 'rejected'`, + [req.user.userId, user_id, user_id, req.user.userId] + ); + await query( 'INSERT INTO friendships (user_id, friend_id, status) VALUES (?, ?, "pending")', [req.user.userId, user_id]