Fix crud query for guests reservations
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user