By YX Fulfillment
A rate card has several reasons why a service might lack a displayed amount. A proposal can explicitly waive a fee, the provider can still be waiting for a measurement to price it, or the service can be absent from a particular proposal. Rendering all three as “0.00” gives the reader a false answer.
This response schema represents a priced service with integer minor units and currency. A pending service has a distinct status. A service for which the source supplied no entry is missing from the service map. The reader uses membership to tell that last case apart from an explicit entry with an unsupported shape.
import json
quote = {
"packing": {"state": "priced", "minor_units": 0, "currency": "USD"},
"palletising": {"state": "pending"},
}
def classify(services, service):
if service not in services:
return "missing", None
line = services[service]
if line.get("state") == "pending":
return "pending", None
if (
line.get("state") == "priced"
and type(line.get("minor_units")) is int
and line["minor_units"] >= 0
and line.get("currency")
):
return "priced", (line["currency"], line["minor_units"])
raise ValueError("Unsupported rate entry")
stored = json.dumps(quote, sort_keys=True)
restored = json.loads(stored)
statuses = [classify(restored, name) for name in ("packing", "palletising", "photo check")]
assert statuses == [("priced", ("USD", 0)), ("pending", None), ("missing", None)]
for name, outcome in zip(("packing", "palletising", "photo check"), statuses):
print(name, "=>", outcome)
Output from a local run on Python 3.9.6
packing => ('priced', ('USD', 0))
palletising => ('pending', None)
photo check => ('missing', None)
The round trip preserves the price of zero and the two reasons an amount isn't available. A subtotal can add confirmed amounts only and display how many service lines are pending, rather than implying the entire quote is priced. Store source currency in each price entry and add across currencies only under an explicit conversion policy.
The example makes “missing” a statement about this response payload. It does not establish that the provider never offers the service. If clients also need “out of scope” or “included elsewhere,” give that business state a separate explicit value and test the display. Converting every absent amount to zero during ingestion removes information no front end can recover.
Top comments (0)