Fix several problems with gift registry api

This commit is contained in:
2025-03-16 20:23:52 +01:00
parent 54fbed2816
commit 1a18347e93
4 changed files with 33 additions and 18 deletions

View File

@@ -121,26 +121,35 @@ class CRUDGiftItem(CRUDBase[GiftItem, GiftItemCreate, GiftItemUpdate]):
return gift
def cancel_reservation(
self, db: Session, *, gift_id: UUID, guest_id: UUID
self, db: Session, *, gift_id: UUID | str, guest_id: UUID | str
) -> GiftItem:
"""Cancel a gift reservation"""
gift_id = UUID(gift_id) if isinstance(gift_id, str) else gift_id
gift = self.get(db, gift_id)
guest = db.query(Guest).get(guest_id)
if gift and gift.status == GiftStatus.RESERVED:
# Using the ORM relationship approach
if not gift:
raise ValueError(f"Gift with ID {gift_id} not found")
guest_id = UUID(guest_id) if isinstance(guest_id, str) else guest_id
guest = db.query(Guest).get(guest_id)
if not guest:
raise ValueError(f"Guest with ID {guest_id} not found")
# Only perform the operation if the gift is reserved
if gift.status == GiftStatus.RESERVED:
# Check if this guest has actually reserved this gift
if guest in gift.reserved_by:
gift.reserved_by.remove(guest)
# Update gift status
gift.status = GiftStatus.AVAILABLE
gift.last_status_change = datetime.now(timezone.utc)
# Only change status if no other guests have reserved this gift
if not gift.reserved_by:
gift.status = GiftStatus.AVAILABLE
gift.last_status_change = datetime.now(timezone.utc)
db.commit()
db.refresh(gift)
return gift
db.commit()
db.refresh(gift)
return gift # Always return the gift object, even if no changes were made
class CRUDEventGiftCategory:
def create(self, db: Session, *, obj_in: EventGiftCategoryCreate) -> EventGiftCategory:
@@ -314,7 +323,6 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
self, db: Session, *, guest_id: UUID | str
) -> List[GiftPurchase]:
"""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
@@ -351,6 +359,7 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
return result
# Create CRUD instances
gift_item_crud = CRUDGiftItem(GiftItem)
gift_category_crud = CRUDGiftCategory(GiftCategory)