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(