Allow filtering invitations by status

This commit is contained in:
Peter Vacho 2025-01-04 16:42:00 +01:00
parent b5af28c5b1
commit 49946660c3
Signed by: school
GPG key ID: 8CFC3837052871B4

View file

@ -1,8 +1,8 @@
from datetime import UTC, datetime 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 beanie import Link, PydanticObjectId
from fastapi import APIRouter, HTTPException, Response, status from fastapi import APIRouter, HTTPException, Query, Response, status
from pydantic import BaseModel from pydantic import BaseModel
from src.db.models.event import Event 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") @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. """Get all incoming invitations for a given user.
Note that this endpoint only allows you to access the invitations you received. 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: if user.id is None:
raise MissingIdError(user) raise MissingIdError(user)
@ -165,11 +168,12 @@ async def get_user_incoming_invitatinos(user_id: PydanticObjectId, user: Current
if user.id != user_id: if user.id != user_id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You can only access your own invitations.") raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You can only access your own invitations.")
invitations = await Invitation.find( query = Invitation.find(expr(Invitation.invitee).id == user_id)
expr(Invitation.invitee).id == user_id, if invite_status is not None:
Invitation.status == "pending", query = query.find(Invitation.status == invite_status)
fetch_links=True, query = query.find(fetch_links=True)
).to_list()
invitations = await query.to_list()
return [InvitationData.from_invitation(invitation) for invitation in invitations] return [InvitationData.from_invitation(invitation) for invitation in invitations]