Compare commits

..

No commits in common. "516bac573af101adf2933a009842327932074ad6" and "b5af28c5b1589004a838bc8ebc71f9ca45d74a07" have entirely different histories.

View file

@ -1,8 +1,8 @@
from datetime import UTC, datetime
from typing import Annotated, Literal, cast, final, overload
from typing import Literal, cast, final, overload
from beanie import Link, PydanticObjectId
from fastapi import APIRouter, HTTPException, Query, Response, status
from fastapi import APIRouter, HTTPException, Response, status
from pydantic import BaseModel
from src.db.models.event import Event
@ -153,14 +153,11 @@ 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,
invite_status: Annotated[Literal["pending", "accepted", "declined"] | None, Query()] = None,
) -> list[InvitationData]:
async def get_user_incoming_invitatinos(user_id: PydanticObjectId, user: CurrentUserDep) -> 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)
@ -168,12 +165,11 @@ async def get_user_incoming_invitatinos(
if user.id != user_id:
raise HTTPException(status_code=status.HTTP_403_FORBIDDEN, detail="You can only access your own invitations.")
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()
invitations = await Invitation.find(
expr(Invitation.invitee).id == user_id,
Invitation.status == "pending",
fetch_links=True,
).to_list()
return [InvitationData.from_invitation(invitation) for invitation in invitations]
@ -224,7 +220,7 @@ async def delete_invitation(invitation_id: PydanticObjectId, user: CurrentUserDe
@invitations_router.post("/{invitation_id}/accept")
async def accept_invitation(invitation_id: PydanticObjectId, user: CurrentUserDep) -> InvitationData:
async def accept_invitation(invitation_id: PydanticObjectId, user: CurrentUserDep) -> Response:
"""Accept an invitation."""
invitation = await Invitation.get(invitation_id, fetch_links=True)
if invitation is None:
@ -242,7 +238,7 @@ async def accept_invitation(invitation_id: PydanticObjectId, user: CurrentUserDe
# Mark the invitation as accepted
invitation.status = "accepted"
invitation.responded_at = datetime.now(tz=UTC)
_ = await invitation.replace()
_ = await invitation.save()
# Add the user to the event
event = invitation.event
@ -262,11 +258,11 @@ async def accept_invitation(invitation_id: PydanticObjectId, user: CurrentUserDe
)
notification = await notification.create()
return InvitationData.from_invitation(invitation)
return Response(status_code=status.HTTP_204_NO_CONTENT)
@invitations_router.post("/{invitation_id}/decline")
async def decline_invitation(invitation_id: PydanticObjectId, user: CurrentUserDep) -> InvitationData:
async def decline_invitation(invitation_id: PydanticObjectId, user: CurrentUserDep) -> Response:
"""Decline an invitation."""
invitation = await Invitation.get(invitation_id, fetch_links=True)
if invitation is None:
@ -283,7 +279,7 @@ async def decline_invitation(invitation_id: PydanticObjectId, user: CurrentUserD
invitation.status = "declined"
invitation.responded_at = datetime.now(tz=UTC)
_ = await invitation.replace()
_ = await invitation.save()
# Send back a notification to the invitor about the decline
notification = Notification(
@ -295,7 +291,7 @@ async def decline_invitation(invitation_id: PydanticObjectId, user: CurrentUserD
)
notification = await notification.create()
return InvitationData.from_invitation(invitation)
return Response(status_code=status.HTTP_204_NO_CONTENT)
router = APIRouter()