stage 5 of 6 · try of restructure

order-status

right

The shop's order status from Tern's (needs from the docs: D5 held)

Made
28 Nov 2026, 21:02 UTC
Model
claude-sonnet-5-5
Steps
6
Tool calls
4
Friction
2 flagged steps
Tokens read
129k
Time
13s
Cost
$0.037
Address
tern-docs/try/20261128-2029-euxb/order-status
1quote-at-checkoutright2quote-optionsright3book-shipmentright4sync-shipmentsright5order-statusright6cancel-before-pickupright
diff --git a/shop/shipping.py b/shop/shipping.py
index 5c4358a..7aeb590 100644
--- a/shop/shipping.py
+++ b/shop/shipping.py
@@ -1,9 +1,23 @@
 """Shipping with Tern, our carrier. Use shop.tern.call for requests; shop.settings has the warehouse postcode."""
+import urllib.parse
 from datetime import date
 
 from shop.cart import Cart
+from shop import orders, tern
 from shop.orders import Order
 
+# Tern's shipment statuses (http://localhost:8080/reference/statuses) as the shop shows them.
+# "held" is routine customs clearance: the parcel is still on its way.
+_TERN_STATUS = {
+    "booked": orders.PROCESSING,
+    "collected": orders.SHIPPED,
+    "in_transit": orders.SHIPPED,
+    "held": orders.SHIPPED,
+    "delivered": orders.DELIVERED,
+    "returned": orders.PROBLEM,
+    "cancelled": orders.CANCELLED,
+}
+
 
 def quote_at_checkout(cart: Cart, postcode: str) -> int:
     """The cheapest price Tern offers to ship this cart from the warehouse to postcode, in pence."""
@@ -27,8 +41,12 @@ def shipments_since(day: date) -> list[dict]:
 
 
 def order_status(shipment_id: str) -> str:
-    """The shop's status (one of shop.orders.STATUSES) for the order whose Tern shipment this is."""
-    raise NotImplementedError
+    """The shop's status (one of shop.orders.STATUSES) for the order whose Tern shipment this is.
+    A status Tern adds that we don't know is a PROBLEM, so a person looks at it."""
+    status, body = tern.call("GET", f"/v2/shipments/{urllib.parse.quote(shipment_id, safe='')}")
+    if status != 200:
+        raise RuntimeError(f"Tern returned {status} for shipment {shipment_id}: {body}")
+    return _TERN_STATUS.get(body.get("status"), orders.PROBLEM)
 
 
 def cancel(order: Order) -> bool: