Order lifecycle

Order status is controlled by one explicit state machine (order-state-machine.ts): 29 allowed transitions, each listing which parties may make it and which checks guard it. Nothing writes a status directly. Each seller's order group moves through these states independently.

Order state diagram States from pending payment to completed or refunded, with cancellation, failed delivery, return and dispute paths. PENDING_PAYMENT PAID CONFIRMED PROCESSING READY_FOR_PICKUP PAYMENT_FAILED CANCELLED PICKED_UP IN_TRANSIT OUT_FOR_DELIVERY DELIVERED COMPLETED FAILED_DELIVERY CANCELLED RETURN_REQUESTED RETURN_APPROVED RETURN_IN_TRANSIT RETURN_RECEIVED REFUNDED DISPUTED captured seller accepts packs ready declined retry cancel driver collects return window closes fails retry (max 3) attempts exhausted request return seller rejects dispute for seller for buyer
Solid arrows: the forward path. Red dashed arrows: cancellation, failure and dispute paths. CANCELLED is drawn twice only to keep the picture readable.

Key rules

  • Cancellation is possible only before READY_FOR_PICKUP, and the buyer's own right to cancel ends as soon as a seller accepts. After pickup, the buyer's route is a return.
  • Failed delivery can be retried up to 3 times; after that the order is cancelled.
  • Delivered requires a validated proof of delivery (unless staff override).
  • Returns can be requested for 14 days after delivery.
  • COMPLETED is deliberately not final: a chargeback or late claim can reopen it as a dispute. Only CANCELLED and REFUNDED are final.

Three kinds of "no"

Error codeMeaning
order.ILLEGAL_TRANSITIONThe move does not exist (e.g. PAID → DELIVERED).
auth.FORBIDDENThe move exists but this party may not make it (a buyer cannot mark their own order paid).
order.CANCELLATION_WINDOW_CLOSED, order.RETURN_WINDOW_CLOSED, delivery.PROOF_INVALIDThe move is allowed in principle, but a guard failed.

Who may make each transition

"Staff" means admins and super admins.

TransitionWho may make it
PENDING_PAYMENT → PAID / PAYMENT_FAILEDSystem (payment result)
PAYMENT_FAILED → PENDING_PAYMENTBuyer, system (retry)
PAID → CONFIRMED → PROCESSING → READY_FOR_PICKUPSeller, staff
… → CANCELLED (before pickup)Buyer (only until a seller accepts), seller, system, staff
READY_FOR_PICKUP → PICKED_UP → IN_TRANSIT → OUT_FOR_DELIVERYDriver, system, staff
OUT_FOR_DELIVERY → DELIVEREDDriver (with validated proof), staff
OUT_FOR_DELIVERY → FAILED_DELIVERYDriver, system, staff
FAILED_DELIVERY → OUT_FOR_DELIVERY / CANCELLEDDispatcher, system, staff
DELIVERED → COMPLETEDSystem, staff
DELIVERED → RETURN_REQUESTEDBuyer (within 14 days), staff
RETURN_REQUESTED → RETURN_APPROVEDSeller, staff
RETURN_REQUESTED → DISPUTEDSeller, buyer, staff
RETURN_APPROVED → RETURN_IN_TRANSITBuyer, driver, system, staff
RETURN_IN_TRANSIT → RETURN_RECEIVEDSeller, staff
RETURN_RECEIVED → REFUNDEDSystem, staff
COMPLETED → DISPUTEDBuyer, staff
DISPUTED → COMPLETED / REFUNDEDStaff only
Worth knowing

The DELIVERED → COMPLETED move is defined for the system, but no scheduled job in the worker performs it today; orders remain DELIVERED until staff complete them. Returns and reviews treat DELIVERED and COMPLETED alike.