From 49946660c319bcc1fbe91691f6abb413154f7cb7 Mon Sep 17 00:00:00 2001 From: Peter Vacho Date: Sat, 4 Jan 2025 16:42:00 +0100 Subject: [PATCH] Allow filtering invitations by status --- src/api/invitations.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/src/api/invitations.py b/src/api/invitations.py index 9a9d8a5..7792ffb 100644 --- a/src/api/invitations.py +++ b/src/api/invitations.py @@ -1,8 +1,8 @@ from datetime import UTC, datetime -from typing import Literal, cast, final, overload +from typing import Annotated, Literal, cast, final, overload from beanie import Link, PydanticObjectId -from fastapi import APIRouter, HTTPException, Response, status +from fastapi import APIRouter, HTTPException, Query, Response, status from pydantic import BaseModel from src.db.models.event import Event @@ -153,11 +153,14 @@ async def get_user_invitatinos(user_id: PydanticObjectId, user: CurrentUserDep) @base_router.get("/users/{user_id}/invitations/incoming") -async def get_user_incoming_invitatinos(user_id: PydanticObjectId, user: CurrentUserDep) -> list[InvitationData]: +async def get_user_incoming_invitatinos( + user_id: PydanticObjectId, + user: CurrentUserDep, + invite_status: Annotated[Literal["pending", "accepted", "declined"] | None, Query()] = None, +) -> list[InvitationData]: """Get all incoming invitations for a given user. Note that this endpoint only allows you to access the invitations you received. - Only invitations with status "pending" are returned. """ if user.id is None: raise MissingIdError(user) @@ -165,11 +168,12 @@ async def get_user_incoming_invitatinos(user_id: PydanticObjectId, user: Current if user.id != user_id: raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You can only access your own invitations.") - invitations = await Invitation.find( - expr(Invitation.invitee).id == user_id, - Invitation.status == "pending", - fetch_links=True, - ).to_list() + query = Invitation.find(expr(Invitation.invitee).id == user_id) + if invite_status is not None: + query = query.find(Invitation.status == invite_status) + query = query.find(fetch_links=True) + + invitations = await query.to_list() return [InvitationData.from_invitation(invitation) for invitation in invitations]