How would you structure the data models and state transitions for an order processing service to prevent race conditions during concurrent updates?
Implement a pure function using optimistic concurrency control. An order has id, status, and version; an update succeeds only when expected_version matches the current version and the action is valid for the current state. Return the updated order or a structured failure without mutating the input.
Signature: apply_order_update(order, action, expected_version)
id and status, plus a nonnegative integer version.pending, paid, shipped, delivered, and cancelled.pay, ship, deliver, and cancel.def apply_order_update(order, action, expected_version):