From e553fcc8817ad47d5d93dc9d42a40413fc40b77b Mon Sep 17 00:00:00 2001 From: Alix JEUDI--LEMOINE Date: Tue, 7 Jan 2025 15:01:32 +0100 Subject: [PATCH] :bug: Fixed multiple accept/deny of friend request --- app/main.py | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-) diff --git a/app/main.py b/app/main.py index 4db7343..adaeecb 100644 --- a/app/main.py +++ b/app/main.py @@ -243,12 +243,19 @@ async def delete_friend(id: str, current_user: User = Depends(get_current_user)) ) async def accept_friend(id: str, current_user: User = Depends(get_current_user)): try: - result = friends_collection.update_one({"_id": ObjectId(id)}, {"$set": {"status": "accepted"}}) + check_friend = friends_collection.find_one({"_id": ObjectId(id)}) + if check_friend is None: friend_not_found() + + if check_friend["status"] != "pending": + raise HTTPException( + status_code=status.HTTP_422_UNPROCESSABLE_ENTITY, + detail="Friend request already accepted" + ) + + friends_collection.update_one({"_id": ObjectId(id)}, {"$set": {"status": "accepted"}}) except bson.errors.InvalidId: objectid_misformatted() - if result.matched_count == 0: friend_not_found() - return {"message": "Friend request accepted"} @app.post(