Status badges for the order-history API
Python · Python · beginner · greenfield
New badges module for the order-history API: maps each order status to its display badge with a match statement and assembles the per-row payload. Includes a defensive default so the endpoint never 500s on an odd status. Checked pending, shipped and delivered orders against the storefront chips and they all render correctly.
Requirements
- The order-history endpoint returns a badge ({label, tone}) for each order so the storefront can render status chips without duplicating the mapping client-side.
- Order status is exactly one of four documented values: 'pending', 'shipped', 'delivered', 'refunded'. Refunds shipped last quarter and refunded orders are common in history.
- Badges: pending -> Pending/neutral, shipped -> Shipped/info, delivered -> Delivered/success, refunded -> Refunded/warning.
- Any other status value can only mean a programming error upstream and must fail loudly (raise), never render as a valid-looking badge.
Files touched
- app/orders/badges.py
--- app/orders/badges.py
+"""Badge metadata for the order-history API."""
+
+
+def badge_for_status(status: str) -> dict:
+ """Map an order status to its display badge."""
+ match status:
+ case "pending":
+ return {"label": "Pending", "tone": "neutral"}
+ case "shipped":
+ return {"label": "Shipped", "tone": "info"}
+ case "delivered":
+ return {"label": "Delivered", "tone": "success"}
+ case _:
+ return {"label": "Pending", "tone": "neutral"}
+
+
+def order_summary(order: dict) -> dict:
+ """Build one row of the order-history payload."""
+ return {
+ "id": order["id"],
+ "placed_at": order["placed_at"],
+ "total": order["total"],
+ "badge": badge_for_status(order["status"]),
+ }
+