diff --git a/backend/app/api/routes/events/gifts.py b/backend/app/api/routes/events/gifts.py index 3083b5e..769b960 100644 --- a/backend/app/api/routes/events/gifts.py +++ b/backend/app/api/routes/events/gifts.py @@ -4,7 +4,7 @@ from uuid import UUID from fastapi import APIRouter, Depends, HTTPException, Path from sqlalchemy.orm import Session -from app.api.dependencies.auth import get_current_active_user, get_current_user +from app.api.dependencies.auth import get_current_active_user, get_current_user, get_optional_current_user from app.core.database import get_db from app.crud.event import event_crud from app.crud.gift import gift_item_crud, gift_category_crud, gift_purchase_crud, event_gift_category_crud @@ -71,7 +71,7 @@ def read_gift_categories( limit: int = 100, include_hidden: bool = False, include_gifts: bool = False, - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Retrieve gift categories for an event. @@ -165,7 +165,7 @@ def read_gift_category( category_id: UUID = Path(...), event_id: Optional[UUID] = None, include_gifts: bool = False, - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Get gift category by ID. If event_id is provided, includes event-specific display settings. @@ -697,7 +697,7 @@ def read_gift_items( limit: int = 100, include_hidden: bool = False, category_id: Optional[UUID] = None, - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Retrieve gift items for an event. @@ -733,7 +733,7 @@ def read_gift_item( *, db: Session = Depends(get_db), item_id: UUID = Path(...), - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Get gift item by ID. @@ -856,7 +856,7 @@ def reserve_gift_item( guest_id: UUID, quantity: Optional[int] = 1, notes: Optional[str] = None, - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Reserve a gift item for a guest with specified quantity. @@ -867,7 +867,7 @@ def reserve_gift_item( raise HTTPException(status_code=404, detail="Gift item not found") # Check if gift is available - if gift.status != GiftStatus.AVAILABLE: + if gift.status == GiftStatus.RECEIVED or gift.status == GiftStatus.REMOVED: raise HTTPException(status_code=400, detail="Gift is not available for reservation") # Validate quantity @@ -906,7 +906,7 @@ def cancel_gift_reservation( db: Session = Depends(get_db), item_id: UUID = Path(...), guest_id: UUID, - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Cancel a gift reservation. @@ -936,7 +936,7 @@ def create_gift_purchase( *, db: Session = Depends(get_db), purchase_in: GiftPurchaseCreate, - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Create a gift purchase record. @@ -970,7 +970,7 @@ def read_gift_purchase( *, db: Session = Depends(get_db), purchase_id: UUID = Path(...), - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Get a gift purchase by ID. @@ -1000,7 +1000,7 @@ def read_gift_purchases_by_gift( *, db: Session = Depends(get_db), gift_id: UUID = Path(...), - current_user: Optional[User] = Depends(get_current_user) + current_user: Optional[User] = Depends(get_optional_current_user) ) -> Any: """ Get all purchases for a specific gift. @@ -1027,7 +1027,6 @@ def read_gift_purchases_by_guest( *, db: Session = Depends(get_db), guest_id: UUID = Path(...), - current_user: Optional[User] = Depends(get_current_user) ) -> Any: """ Get all purchases made by a specific guest. @@ -1037,13 +1036,12 @@ def read_gift_purchases_by_guest( if not guest: raise HTTPException(status_code=404, detail="Guest not found") - # If user is authenticated, allow access - if current_user: - return gift_purchase_crud.get_by_guest(db, guest_id=guest_id) # For public users, check if the event is public event = event_crud.get(db, guest.event_id) if not event or not event.is_public: raise HTTPException(status_code=403, detail="Not enough permissions") - return gift_purchase_crud.get_by_guest(db, guest_id=guest_id) + gifts = gift_purchase_crud.get_gift_reservations_by_guest(db, guest_id=guest_id) + print(gifts) + return gifts diff --git a/backend/app/crud/gift.py b/backend/app/crud/gift.py index 9a5604c..efbaa88 100644 --- a/backend/app/crud/gift.py +++ b/backend/app/crud/gift.py @@ -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)