fix rejected friends
All checks were successful
Build Images and Deploy / Update-PROD-Stack (push) Successful in 32s
All checks were successful
Build Images and Deploy / Update-PROD-Stack (push) Successful in 32s
This commit is contained in:
@@ -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]
|
||||
|
||||
Reference in New Issue
Block a user