Add event-specific guest gift reservations endpoint

Introduced a function `get_event_guest_gift_reservations` in the CRUD layer to fetch gift reservations filtered by event ID. Updated the API endpoint to optionally accept an `event_id` query parameter for retrieving reservations specific to an event.
This commit is contained in:
2025-03-19 19:56:10 +01:00
parent 9fe5e60907
commit c81e27c602
2 changed files with 46 additions and 5 deletions

View File

@@ -387,6 +387,41 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
return result
def get_event_guest_gift_reservations(self, db: Session, event_id: UUID | str) -> List[GiftPurchase]:
"""Retrieve all gift reservations for guests belonging to a specific event."""
event_id = event_id if isinstance(event_id, UUID) else UUID(event_id)
stmt = (
select(Guest)
.where(Guest.event_id == event_id)
.options(joinedload(Guest.gifts))
)
# Correct: Call unique() on the RESULT, not on the stmt
guests = db.execute(stmt).unique().scalars().all()
results = []
for guest in guests:
for gift in guest.gifts:
reservation_stmt = select(GuestGifts).where(
(GuestGifts.c.guest_id == guest.id) &
(GuestGifts.c.gift_id == gift.id)
)
reservation = db.execute(reservation_stmt).first()
if reservation:
purchase = GiftPurchase(
id=UUID('00000000-0000-0000-0000-000000000000'),
gift_id=gift.id,
guest_id=guest.id,
quantity=1,
purchased_at=reservation.reserved_at,
notes=reservation.notes
)
results.append(purchase)
return results
# Create CRUD instances
gift_item_crud = CRUDGiftItem(GiftItem)