Allow filtering invitations by status
This commit is contained in:
parent
b5af28c5b1
commit
49946660c3
|
@ -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]
|
||||
|
||||
|
|
Loading…
Reference in a new issue