The shop's order status from Tern's (needs from the docs: D5 held)
diff --git a/shop/shipping.py b/shop/shipping.py
index 5c4358a..1f16fed 100644
--- a/shop/shipping.py
+++ b/shop/shipping.py
@@ -2,7 +2,19 @@
from datetime import date
from shop.cart import Cart
-from shop.orders import Order
+from shop import tern
+from shop.orders import CANCELLED, DELIVERED, PROBLEM, PROCESSING, SHIPPED, Order
+
+# Tern status -> shop status. "held" is routine customs clearance: the parcel is still on its way.
+_TERN_STATUS = {
+ "booked": PROCESSING,
+ "collected": SHIPPED,
+ "in_transit": SHIPPED,
+ "held": SHIPPED,
+ "delivered": DELIVERED,
+ "returned": PROBLEM,
+ "cancelled": CANCELLED,
+}
def quote_at_checkout(cart: Cart, postcode: str) -> int:
@@ -28,7 +40,11 @@ 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
+ status, body = tern.call("GET", f"/v2/shipments/{shipment_id}")
+ if status != 200:
+ raise RuntimeError(f"Tern returned {status} for shipment {shipment_id}: {body}")
+ # An unrecognised status needs a person to look at it rather than a guess.
+ return _TERN_STATUS.get(body.get("status"), PROBLEM)
def cancel(order: Order) -> bool:
diff --git a/tests/test_shipping.py b/tests/test_shipping.py
new file mode 100644
index 0000000..797685f
--- /dev/null
+++ b/tests/test_shipping.py
@@ -0,0 +1,19 @@
+import pytest
+
+from shop import shipping
+from shop.orders import CANCELLED, DELIVERED, PROBLEM, PROCESSING, SHIPPED
+
+
+@pytest.mark.parametrize("tern_status, expected", [
+ ("booked", PROCESSING), ("collected", SHIPPED), ("in_transit", SHIPPED), ("held", SHIPPED),
+ ("delivered", DELIVERED), ("returned", PROBLEM), ("cancelled", CANCELLED), ("new_thing", PROBLEM),
+])
+def test_order_status_maps_tern_status(monkeypatch, tern_status, expected):
+ monkeypatch.setattr(shipping.tern, "call", lambda *a, **k: (200, {"status": tern_status}))
+ assert shipping.order_status("shp_1") == expected
+
+
+def test_order_status_raises_on_error(monkeypatch):
+ monkeypatch.setattr(shipping.tern, "call", lambda *a, **k: (404, {"error": "not_found"}))
+ with pytest.raises(RuntimeError):
+ shipping.order_status("nope")