diff --git a/frontend/src/components/pages/Orders.tsx b/frontend/src/components/pages/Orders.tsx index 4aeba6d7..bdeff20b 100644 --- a/frontend/src/components/pages/Orders.tsx +++ b/frontend/src/components/pages/Orders.tsx @@ -13,7 +13,7 @@ import ROUTES from "@/lib/types/routes"; const OrdersPage: React.FC = () => { const navigate = useNavigate(); const { api, currentUser, isAuthenticated, isLoading } = useAuthentication(); - const [orders, setOrders] = useState(null); + const [orders, setOrders] = useState([]); const [loadingOrders, setLoadingOrders] = useState(true); const { addErrorAlert } = useAlertQueue(); @@ -65,7 +65,7 @@ const OrdersPage: React.FC = () => {
- ) : orders && orders.length > 0 ? ( + ) : orders.length > 0 ? (
{orders.map((orderWithProduct: OrderWithProduct) => ( Order: async def get_orders_by_user_id(self, user_id: str) -> list[Order]: orders = await self._get_items_from_secondary_index("user_id", user_id, Order) if not orders: - raise ItemNotFoundError("No orders found for this user") + raise OrdersNotFoundError(f"No orders found for user {user_id}") return orders async def get_orders_by_stripe_connect_account_id(self, stripe_connect_account_id: str) -> list[Order]: diff --git a/store/app/model.py b/store/app/model.py index 1d568a8d..19616431 100644 --- a/store/app/model.py +++ b/store/app/model.py @@ -572,7 +572,6 @@ def create(cls, user_id: str, listing_id: str, is_upvote: bool) -> Self: class Order(StoreBaseModel): """Tracks completed user orders through Stripe.""" - id: str user_id: str listing_id: str user_email: str diff --git a/store/app/routers/orders.py b/store/app/routers/orders.py index a453919f..09949c19 100644 --- a/store/app/routers/orders.py +++ b/store/app/routers/orders.py @@ -1,12 +1,12 @@ """Defines the router endpoints for handling Orders.""" +import asyncio from logging import getLogger from fastapi import APIRouter, Depends, HTTPException, status from pydantic import BaseModel -from store.app.crud.base import ItemNotFoundError -from store.app.crud.orders import OrderDataUpdate +from store.app.crud.orders import OrderDataUpdate, OrdersNotFoundError from store.app.db import Crud from store.app.model import Order, User from store.app.routers import stripe @@ -82,18 +82,30 @@ async def get_product_info(order: Order) -> OrderWithProduct: ) return OrderWithProduct(order=order, product=product_info) except Exception as e: - logger.error("Error processing order", extra={"order_id": order.id, "error": str(e), "user_id": user.id}) + logger.error( + "Error getting product info for order", + extra={"order_id": order.id, "error": str(e), "user_id": user.id}, + ) return OrderWithProduct(order=order, product=None) try: orders = await crud.get_orders_by_user_id(user.id) - return [await get_product_info(order) for order in orders] - except ItemNotFoundError: + return await asyncio.gather(*[get_product_info(order) for order in orders]) + except OrdersNotFoundError: return [] + except asyncio.TimeoutError: + logger.error("Timeout while fetching orders", extra={"user_id": user.id}) + raise HTTPException( + status_code=status.HTTP_504_GATEWAY_TIMEOUT, detail="Request timed out while fetching orders" + ) except Exception as e: - logger.error("Error fetching user orders", extra={"user_id": user.id, "error": str(e)}) + logger.error( + "Unexpected error fetching user orders", + extra={"user_id": user.id, "error": str(e), "error_type": type(e).__name__}, + ) raise HTTPException( - status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, detail=f"Error fetching orders: {str(e)}" + status_code=status.HTTP_500_INTERNAL_SERVER_ERROR, + detail="An unexpected error occurred while fetching orders", )