Fix several problems with gift registry api
This commit is contained in:
@@ -926,8 +926,13 @@ def cancel_gift_reservation(
|
|||||||
|
|
||||||
# For admin users, allow direct cancellation
|
# For admin users, allow direct cancellation
|
||||||
if current_user:
|
if current_user:
|
||||||
return gift_item_crud.cancel_reservation(db, gift_id=item_id, guest_id=guest_id)
|
try:
|
||||||
|
return gift_item_crud.cancel_reservation(db, gift_id=item_id, guest_id=guest_id)
|
||||||
|
except ValueError as e:
|
||||||
|
raise HTTPException(status_code=400, detail=str(e))
|
||||||
|
|
||||||
|
# Missing the return statement here, which is also a problem
|
||||||
|
return gift_item_crud.cancel_reservation(db, gift_id=item_id, guest_id=guest_id)
|
||||||
|
|
||||||
# ===== GIFT PURCHASES ===== #
|
# ===== GIFT PURCHASES ===== #
|
||||||
|
|
||||||
@@ -1042,6 +1047,4 @@ def read_gift_purchases_by_guest(
|
|||||||
if not event or not event.is_public:
|
if not event or not event.is_public:
|
||||||
raise HTTPException(status_code=403, detail="Not enough permissions")
|
raise HTTPException(status_code=403, detail="Not enough permissions")
|
||||||
|
|
||||||
gifts = gift_purchase_crud.get_gift_reservations_by_guest(db, guest_id=guest_id)
|
return gift_purchase_crud.get_gift_reservations_by_guest(db, guest_id=guest_id)
|
||||||
print(gifts)
|
|
||||||
return gifts
|
|
||||||
|
|||||||
@@ -271,11 +271,12 @@ def get_event_by_slug(
|
|||||||
db: Session = Depends(get_db),
|
db: Session = Depends(get_db),
|
||||||
slug: str,
|
slug: str,
|
||||||
access_code: Optional[str] = Query(None),
|
access_code: Optional[str] = Query(None),
|
||||||
current_user: Optional[User] = Depends(get_current_user)
|
current_user: Optional[User] = Depends(get_optional_current_user)
|
||||||
) -> EventResponse:
|
) -> EventResponse:
|
||||||
"""Get event by slug."""
|
"""Get event by slug."""
|
||||||
try:
|
try:
|
||||||
event_obj = event_crud.get_by_slug(db=db, slug=slug)
|
event_obj = event_crud.get_by_slug(db=db, slug=slug)
|
||||||
|
print(f"Event {event_obj}")
|
||||||
return validate_event_access(
|
return validate_event_access(
|
||||||
db=db,
|
db=db,
|
||||||
event_obj=event_obj,
|
event_obj=event_obj,
|
||||||
|
|||||||
@@ -1,4 +1,6 @@
|
|||||||
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
from typing import Any, Dict, Generic, List, Optional, Type, TypeVar, Union
|
||||||
|
from uuid import UUID
|
||||||
|
|
||||||
from fastapi.encoders import jsonable_encoder
|
from fastapi.encoders import jsonable_encoder
|
||||||
from pydantic import BaseModel
|
from pydantic import BaseModel
|
||||||
from sqlalchemy.orm import Session
|
from sqlalchemy.orm import Session
|
||||||
@@ -19,7 +21,7 @@ class CRUDBase(Generic[ModelType, CreateSchemaType, UpdateSchemaType]):
|
|||||||
"""
|
"""
|
||||||
self.model = model
|
self.model = model
|
||||||
|
|
||||||
def get(self, db: Session, id: str) -> Optional[ModelType]:
|
def get(self, db: Session, id: UUID | str) -> Optional[ModelType]:
|
||||||
return db.query(self.model).filter(self.model.id == id).first()
|
return db.query(self.model).filter(self.model.id == id).first()
|
||||||
|
|
||||||
def get_multi(
|
def get_multi(
|
||||||
|
|||||||
@@ -121,26 +121,35 @@ class CRUDGiftItem(CRUDBase[GiftItem, GiftItemCreate, GiftItemUpdate]):
|
|||||||
return gift
|
return gift
|
||||||
|
|
||||||
def cancel_reservation(
|
def cancel_reservation(
|
||||||
self, db: Session, *, gift_id: UUID, guest_id: UUID
|
self, db: Session, *, gift_id: UUID | str, guest_id: UUID | str
|
||||||
) -> GiftItem:
|
) -> GiftItem:
|
||||||
"""Cancel a gift reservation"""
|
"""Cancel a gift reservation"""
|
||||||
|
gift_id = UUID(gift_id) if isinstance(gift_id, str) else gift_id
|
||||||
gift = self.get(db, gift_id)
|
gift = self.get(db, gift_id)
|
||||||
guest = db.query(Guest).get(guest_id)
|
|
||||||
|
|
||||||
if gift and gift.status == GiftStatus.RESERVED:
|
if not gift:
|
||||||
# Using the ORM relationship approach
|
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:
|
if guest in gift.reserved_by:
|
||||||
gift.reserved_by.remove(guest)
|
gift.reserved_by.remove(guest)
|
||||||
|
|
||||||
# Update gift status
|
# Only change status if no other guests have reserved this gift
|
||||||
gift.status = GiftStatus.AVAILABLE
|
if not gift.reserved_by:
|
||||||
gift.last_status_change = datetime.now(timezone.utc)
|
gift.status = GiftStatus.AVAILABLE
|
||||||
|
gift.last_status_change = datetime.now(timezone.utc)
|
||||||
|
|
||||||
db.commit()
|
db.commit()
|
||||||
db.refresh(gift)
|
db.refresh(gift)
|
||||||
|
|
||||||
return gift
|
|
||||||
|
|
||||||
|
return gift # Always return the gift object, even if no changes were made
|
||||||
|
|
||||||
class CRUDEventGiftCategory:
|
class CRUDEventGiftCategory:
|
||||||
def create(self, db: Session, *, obj_in: EventGiftCategoryCreate) -> EventGiftCategory:
|
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
|
self, db: Session, *, guest_id: UUID | str
|
||||||
) -> List[GiftPurchase]:
|
) -> List[GiftPurchase]:
|
||||||
"""Convert gift reservations to purchase-like objects for API compatibility"""
|
"""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)
|
guest_id = guest_id if isinstance(guest_id, UUID) else UUID(guest_id)
|
||||||
|
|
||||||
# Query the guest object first
|
# Query the guest object first
|
||||||
@@ -351,6 +359,7 @@ class CRUDGiftPurchase(CRUDBase[GiftPurchase, GiftPurchaseCreate, GiftPurchaseUp
|
|||||||
|
|
||||||
return result
|
return result
|
||||||
|
|
||||||
|
|
||||||
# Create CRUD instances
|
# Create CRUD instances
|
||||||
gift_item_crud = CRUDGiftItem(GiftItem)
|
gift_item_crud = CRUDGiftItem(GiftItem)
|
||||||
gift_category_crud = CRUDGiftCategory(GiftCategory)
|
gift_category_crud = CRUDGiftCategory(GiftCategory)
|
||||||
|
|||||||
Reference in New Issue
Block a user