fix rejected friends
All checks were successful
Build Images and Deploy / Update-PROD-Stack (push) Successful in 32s

This commit is contained in:
2026-01-30 18:01:34 -05:00
parent a2d5454780
commit 3aba9f28ff

View File

@@ -22,9 +22,9 @@ router.get('/search', authMiddleware, asyncHandler(async (req, res) => {
WHERE (username LIKE ? OR email LIKE ?) WHERE (username LIKE ? OR email LIKE ?)
AND id != ? AND id != ?
AND id NOT IN ( 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 UNION
SELECT user_id FROM friendships WHERE friend_id = ? SELECT user_id FROM friendships WHERE friend_id = ? AND status IN ('accepted', 'pending')
) )
LIMIT 20`, LIMIT 20`,
[searchTerm, searchTerm, req.user.userId, req.user.userId, req.user.userId] [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); 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( const existing = await query(
`SELECT * FROM friendships `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] [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); 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( await query(
'INSERT INTO friendships (user_id, friend_id, status) VALUES (?, ?, "pending")', 'INSERT INTO friendships (user_id, friend_id, status) VALUES (?, ?, "pending")',
[req.user.userId, user_id] [req.user.userId, user_id]