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
Move an order through its lifecycle
idintegerrequired
event_idstringrequiredevent"ACCEPT" | "REJECT" | "START_PREP" | "MARK_READY" | "PICK_UP" | "DELIVER" | "CANCEL"required
idintegerrequiredrestaurant_idintegerrequiredstatus"PLACED" | "ACCEPTED" | "PREPARING" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REJECTED"requiredversionintegerrequiredtotal_paiseintegerrequiredcreated_atstring (date-time)requiredeventsobject[]Only on GET /v1/orders/{id}.
event_idstringrequiredfrom_statusstringrequiredto_statusstringrequiredactor"customer" | "restaurant" | "rider"required
invalid_bodyinvalid_parameter
unauthorized
forbidden
order_not_found
illegal_transitionconcurrent_updateevent_id_conflict
The caller's own orders, newest first
limitintegerdefault 20. Clamped to 1 to 50.
cursorstring
itemsOrder[]requiredidintegerrequiredrestaurant_idintegerrequiredstatus"PLACED" | "ACCEPTED" | "PREPARING" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REJECTED"requiredversionintegerrequiredtotal_paiseintegerrequiredcreated_atstring (date-time)requiredeventsobject[]Only on GET /v1/orders/{id}.
event_idstringrequiredfrom_statusstringrequiredto_statusstringrequiredactor"customer" | "restaurant" | "rider"required
next_cursorstringrequired
invalid_parameterinvalid_cursor
unauthorized
One order with its events, oldest event first
idintegerrequired
idintegerrequiredrestaurant_idintegerrequiredstatus"PLACED" | "ACCEPTED" | "PREPARING" | "READY" | "PICKED_UP" | "DELIVERED" | "CANCELLED" | "REJECTED"requiredversionintegerrequiredtotal_paiseintegerrequiredcreated_atstring (date-time)requiredeventsobject[]Only on GET /v1/orders/{id}.
event_idstringrequiredfrom_statusstringrequiredto_statusstringrequiredactor"customer" | "restaurant" | "rider"required
invalid_parameter
unauthorized
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:
PLACEDtoACCEPTEDwithACCEPT, restaurant.PLACEDtoREJECTEDwithREJECT, restaurant.PLACEDtoCANCELLEDwithCANCEL, customer.ACCEPTEDtoPREPARINGwithSTART_PREP, restaurant.ACCEPTEDtoCANCELLEDwithCANCEL, customer.PREPARINGtoREADYwithMARK_READY, restaurant.READYtoPICKED_UPwithPICK_UP, rider.PICKED_UPtoDELIVEREDwithDELIVER, rider.
DELIVERED,CANCELLEDandREJECTEDare final states. Sending any event from a role not permitted to perform that event returns 403 with errorforbidden, even when the event is also invalid for the current status. The test checks every event against both disallowed roles from aPLACEDorder.- 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_idon the same order returns 200 with the current order. RepeatingACCEPTwith the same ID returns statusACCEPTEDand the same version as the first response. - Reusing an
event_idfrom another order returns 409 with errorevent_id_conflict. - When two restaurant accounts belonging to restaurant 1 concurrently send
ACCEPTfor the same order, exactly one request returns 200 and the other returns 409 with errorconcurrent_updateorillegal_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
READYhas these transitions in order:>PLACED,PLACED>ACCEPTED,ACCEPTED>PREPARING,PREPARING>READY. - Every applied event creates an
order_eventsrow and a corresponding outbox row. An order that reachesPICKED_UPhas five of each, including its initialPLACEDevent. Sending an invalidACCEPTfrom 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_foundwhen reading an inaccessible order. The same response applies when that restaurant user attempts to transition it, or when any caller requests nonexistent order999999999. A non-numeric ID such asabcreturns 400 with errorinvalid_parameterand fieldid. - An event request containing unknown event
FLYreturns 400 with errorinvalid_parameterand fieldevent. Anevent_idcontaining 37 characters, or a request missingevent_id, returns 400 with errorinvalid_parameterand fieldevent_id. Malformed JSON such as{"event":returns 400 with errorinvalid_body. GET /v1/ordersreturns only the caller's orders, newest first, in the shape{items, next_cursor}. With five orders for one customer and one for another, requestinglimit=2and 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 emptynext_cursor.limit=abcreturns 400 with errorinvalid_parameterand fieldlimit;cursor=forgedreturns 400 with errorinvalid_cursor.
Constraints
- Accept only the seven event types in the transition table. An
event_idmust contain between 1 and 36 characters. - Each applied transition advances
orders.versionby exactly one. Concurrent requests cannot both apply a transition to the same version. - An
event_idcannot 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:
cd backend && make up
cd backend && make test W=W3B5Read the first failing assertion to identify whether the lifecycle, concurrency, event history, visibility or pagination behaviour differs from the expected result.
The reference solution passes every check for this exercise. Try the hints first; reading the answer before you have struggled with the problem teaches much less.
Order lifecycle API
2 items
- 3.11State machines and optimistic lockingnot completed
- 3.12POST /v1/orders/{id}/events: order lifecyclenot completed
Up next · Module 4 · 3 items · about 2 h 45 min
Implementation
Run locally
Write the code in your editor inside zomato-prep, then work through this list.
- Implement every allowed lifecycle transition and role check.
- Handle repeated and cross-order
event_idvalues. - Prevent concurrent transitions from both succeeding.
- Record applied events and outbox rows together.
- Enforce order visibility and return chronological event history.
- Page the caller's orders newest first and validate limits and cursors.
Commands
cd backend && make upcd backend && make test W=W3B5Tests
Every allowed event moves an order to the correct next status and increases its version by exactly one.
POST/v1/orders/{order_id}/eventsHeadersAuthorization Bearer {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,READYandPICKED_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_EveryAllowedTransitionMovesTheOrderAndRaisesItsVersioninbackend/e2e/w3_lifecycle_test.go