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:
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user