Fix crud query for guests reservations
This commit is contained in:
@@ -91,7 +91,7 @@ class CRUDGiftItem(CRUDBase[GiftItem, GiftItemCreate, GiftItemUpdate]):
|
||||
) -> GiftItem:
|
||||
"""Reserve a gift for a guest with specified quantity"""
|
||||
|
||||
gift = self.get(db, gift_id)
|
||||
gift: GiftItem = self.get(db, gift_id)
|
||||
if gift and gift.status == GiftStatus.AVAILABLE:
|
||||
# Add to the association table using the SQLAlchemy Core Table directly
|
||||
from app.models.guest import guest_gifts
|
||||
@@ -306,18 +306,50 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
|
||||
self, db: Session, *, gift_id: UUID
|
||||
) -> List[GiftPurchase]:
|
||||
"""Get all purchases for a specific gift"""
|
||||
return db.query(self.model).filter(
|
||||
return db.query(GiftPurchase).filter(
|
||||
GiftPurchase.gift_id == gift_id
|
||||
).order_by(desc(GiftPurchase.purchased_at)).all()
|
||||
|
||||
def get_by_guest(
|
||||
self, db: Session, *, guest_id: UUID
|
||||
def get_gift_reservations_by_guest(
|
||||
self, db: Session, *, guest_id: UUID | str
|
||||
) -> List[GiftPurchase]:
|
||||
"""Get all purchases made by a specific guest"""
|
||||
return db.query(self.model).filter(
|
||||
GiftPurchase.guest_id == guest_id
|
||||
).order_by(desc(GiftPurchase.purchased_at)).all()
|
||||
"""Convert gift reservations to purchase-like objects for API compatibility"""
|
||||
print(f"Getting all gift reservations of: {guest_id}")
|
||||
guest_id = guest_id if isinstance(guest_id, UUID) else UUID(guest_id)
|
||||
|
||||
# Query the guest object first
|
||||
guest = db.query(Guest).get(guest_id)
|
||||
|
||||
if not guest:
|
||||
return []
|
||||
|
||||
# Create pseudo-purchase objects from reservations
|
||||
result = []
|
||||
for gift in guest.gifts:
|
||||
# Access the association data through the relationship metadata
|
||||
from sqlalchemy import select
|
||||
from app.models.guest import guest_gifts
|
||||
|
||||
# Get the reservation data
|
||||
stmt = select(guest_gifts).where(
|
||||
(guest_gifts.c.guest_id == guest_id) &
|
||||
(guest_gifts.c.gift_id == gift.id)
|
||||
)
|
||||
reservation = db.execute(stmt).first()
|
||||
|
||||
if reservation:
|
||||
# Create a new GiftPurchase object
|
||||
purchase = GiftPurchase(
|
||||
id=UUID('00000000-0000-0000-0000-000000000000'), # Placeholder UUID
|
||||
gift_id=gift.id,
|
||||
guest_id=guest_id,
|
||||
quantity=1, # Default
|
||||
purchased_at=reservation.reserved_at, # Use reservation time
|
||||
notes=reservation.notes
|
||||
)
|
||||
result.append(purchase)
|
||||
|
||||
return result
|
||||
|
||||
# Create CRUD instances
|
||||
gift_item_crud = CRUDGiftItem(GiftItem)
|
||||
|
||||
Reference in New Issue
Block a user