POST /v1/orders/{id}/events: order lifecycle

You build POST /v1/orders/{id}/events to apply authorized transitions, GET /v1/orders to page the caller's orders, and GET /v1/orders/{id} to return order history. Each applied transition advances the version once and writes its event and outbox row together.

Ticket

Build POST /v1/orders/{id}/events to move an order through its lifecycle. Also build GET /v1/orders for the caller's order list and GET /v1/orders/{id} for an order with its event history.

Week 4's app needs order history and a paged order list. W3B6's relay depends on every accepted transition being recorded with its outbox row, so the order update, event record and outbox row must succeed together.

Endpoints

POST/v1/orders/{id}/events

Move an order through its lifecycle

Path parameters
  • idintegerrequired
Request body JSON
  • event_idstringrequired
  • event"ACCEPT" | "REJECT" | "START_PREP" | "MARK_READY" | "PICK_UP" | "DELIVER" | "CANCEL"required
Response 200 OK
  • idintegerrequired
  • restaurant_idintegerrequired
  • status"PLACED" | "ACCEPTED" | "PREPARING" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REJECTED"required
  • versionintegerrequired
  • total_paiseintegerrequired
  • created_atstring (date-time)required
  • eventsobject[]

    Only on GET /v1/orders/{id}.

    • event_idstringrequired
    • from_statusstringrequired
    • to_statusstringrequired
    • actor"customer" | "restaurant" | "rider"required
Response 400 Bad Request

invalid_bodyinvalid_parameter

Response 401

unauthorized

Response 403

forbidden

Response 404 Not Found

order_not_found

Response 409

illegal_transitionconcurrent_updateevent_id_conflict

GET/v1/orders

The caller's own orders, newest first

Query parameters
  • limitinteger

    default 20. Clamped to 1 to 50.

  • cursorstring
Response 200 OK
  • itemsOrder[]required
    • idintegerrequired
    • restaurant_idintegerrequired
    • status"PLACED" | "ACCEPTED" | "PREPARING" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REJECTED"required
    • versionintegerrequired
    • total_paiseintegerrequired
    • created_atstring (date-time)required
    • eventsobject[]

      Only on GET /v1/orders/{id}.

      • event_idstringrequired
      • from_statusstringrequired
      • to_statusstringrequired
      • actor"customer" | "restaurant" | "rider"required
  • next_cursorstringrequired
Response 400 Bad Request

invalid_parameterinvalid_cursor

Response 401

unauthorized

GET/v1/orders/{id}

One order with its events, oldest event first

Path parameters
  • idintegerrequired
Response 200 OK
  • idintegerrequired
  • restaurant_idintegerrequired
  • status"PLACED" | "ACCEPTED" | "PREPARING" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REJECTED"required
  • versionintegerrequired
  • total_paiseintegerrequired
  • created_atstring (date-time)required
  • eventsobject[]

    Only on GET /v1/orders/{id}.

    • event_idstringrequired
    • from_statusstringrequired
    • to_statusstringrequired
    • actor"customer" | "restaurant" | "rider"required
Response 400 Bad Request

invalid_parameter

Response 401

unauthorized

Response 404 Not Found

order_not_found

Acceptance criteria

  • Support these transitions, with the specified role permitted to perform each one. Every allowed transition returns 200, moves the order to the specified status and increases its version by exactly one:
    • PLACED to ACCEPTED with ACCEPT, restaurant.
    • PLACED to REJECTED with REJECT, restaurant.
    • PLACED to CANCELLED with CANCEL, customer.
    • ACCEPTED to PREPARING with START_PREP, restaurant.
    • ACCEPTED to CANCELLED with CANCEL, customer.
    • PREPARING to READY with MARK_READY, restaurant.
    • READY to PICKED_UP with PICK_UP, rider.
    • PICKED_UP to DELIVERED with DELIVER, rider.
  • DELIVERED, CANCELLED and REJECTED are final states. Sending any event from a role not permitted to perform that event returns 403 with error forbidden, even when the event is also invalid for the current status. The test checks every event against both disallowed roles from a PLACED order.
  • Sending an event that the permitted role may perform, but that the current status does not allow, returns 409 with error illegal_transition.
  • Repeating an event_id on the same order returns 200 with the current order. Repeating ACCEPT with the same ID returns status ACCEPTED and the same version as the first response.
  • Reusing an event_id from another order returns 409 with error event_id_conflict.
  • When two restaurant accounts belonging to restaurant 1 concurrently send ACCEPT for the same order, exactly one request returns 200 and the other returns 409 with error concurrent_update or illegal_transition. This check runs five times. The order version increases exactly once.
  • Order detail returns the complete event history oldest first. An order advanced through READY has these transitions in order: >PLACED, PLACED>ACCEPTED, ACCEPTED>PREPARING, PREPARING>READY.
  • Every applied event creates an order_events row and a corresponding outbox row. An order that reaches PICKED_UP has five of each, including its initial PLACED event. Sending an invalid ACCEPT from the restaurant at that point does not increase the outbox row count beyond five.
  • A customer can read their own order, a restaurant user can read an order belonging to their restaurant, and a rider can read any order. Each receives 200.
  • A customer from another account and a restaurant user from another restaurant receive 404 with error order_not_found when reading an inaccessible order. The same response applies when that restaurant user attempts to transition it, or when any caller requests nonexistent order 999999999. A non-numeric ID such as abc returns 400 with error invalid_parameter and field id.
  • An event request containing unknown event FLY returns 400 with error invalid_parameter and field event. An event_id containing 37 characters, or a request missing event_id, returns 400 with error invalid_parameter and field event_id. Malformed JSON such as {"event": returns 400 with error invalid_body.
  • GET /v1/orders returns only the caller's orders, newest first, in the shape {items, next_cursor}. With five orders for one customer and one for another, requesting limit=2 and following the cursors returns exactly the first customer's five orders, each once. Every non-final page has two items, and the final page has an empty next_cursor. limit=abc returns 400 with error invalid_parameter and field limit; cursor=forged returns 400 with error invalid_cursor.

Constraints

  • Accept only the seven event types in the transition table. An event_id must contain between 1 and 36 characters.
  • Each applied transition advances orders.version by exactly one. Concurrent requests cannot both apply a transition to the same version.
  • An event_id cannot be applied twice to one order or reused on another order.
  • The order update, event record and outbox row must be committed together.
  • Order visibility follows the customer, restaurant and rider rules above. Inaccessible and nonexistent orders both return 404.
  • The order list must support the tested page size of two, exclude other customers' orders and reject invalid limits and cursors.

Design direction

Keep the state machine separate from persistence. Represent allowed status and event combinations alongside the role permitted to send each event. Check the role before checking the current status. This keeps the 403 response consistent when a caller is not authorized.

In the persistence layer, consider optimistic locking. Read the current status and version, then condition the update on those values. If another request has already changed the order, the update must not overwrite it. You can return a conflict or re-evaluate the transition against the new status.

Keep the order update, event insert and outbox insert in one database transaction. Give event_id a uniqueness constraint. A MySQL duplicate-key error needs to distinguish a replay on the same order from reuse on another order. For the list endpoint, consider keyset pagination using a stable ordering. Any package or file layout that meets the contract passes.

How to check your work

From the backend directory, run:

bash
cd backend && make up
cd backend && make test W=W3B5

Read the first failing assertion to identify whether the lifecycle, concurrency, event history, visibility or pagination behaviour differs from the expected result.

Finished Exercise 3.12?

Order lifecycle API

2 items

  • 3.11State machines and optimistic lockingnot completed
  • 3.12POST /v1/orders/{id}/events: order lifecyclenot completed

Kafka event publishing

Up next · Module 4 · 3 items · about 2 h 45 min

Start module 4

Implementation

Run locally

Write the code in your editor inside zomato-prep, then work through this list.

  1. Implement every allowed lifecycle transition and role check.
  2. Handle repeated and cross-order event_id values.
  3. Prevent concurrent transitions from both succeeding.
  4. Record applied events and outbox rows together.
  5. Enforce order visibility and return chronological event history.
  6. Page the caller's orders newest first and validate limits and cursors.

Commands

terminal
cd backend && make up
terminal
cd backend && make test W=W3B5

Tests

  1. Every allowed event moves an order to the correct next status and increases its version by exactly one.

    POST/v1/orders/{order_id}/events
    Headers
    AuthorizationBearer {restaurant token}
    Body raw JSON
    {
      "event_id": "e-accept-1",
      "event": "ACCEPT"
    }
    Response 200 OK
    {
      "id": 41,
      "restaurant_id": 1,
      "status": "ACCEPTED",
      "version": 2,
      "total_paise": 55800,
      "created_at": "2026-01-05T10:00:00.000Z"
    }

    Input Exercise each allowed transition from PLACED, ACCEPTED, PREPARING, READY and PICKED_UP, using the customer, restaurant or rider role specified for that event.

    Expected Each request returns 200 with the expected next status and version equal to the previous version plus one.

    TestW3B5_EveryAllowedTransitionMovesTheOrderAndRaisesItsVersion in backend/e2e/w3_lifecycle_test.go