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

Files touched

--- 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"]),
+    }
+

Review this PR

Python practice