Fulfillment
Fulfillment turns an order into warehouse work: pick, pack, label, and ship. The warehouse app calls these APIs. It does not own a second engine.
Responsibilities
- Create and transition
FulfillmentOrderrows. - Allocate when stock arrives (
stock.received). - Produce labels through
fulfillment_queuejobbatch-labels. - React to order and shipment events.
Architecture
Code map
| Piece | Path |
|---|---|
| State machine | fulfillment-state-machine.ts |
| Workflow | fulfillment-workflow.service.ts |
| Order listener | order-fulfillment.listener.ts |
| Schema | 42-fulfillment.prisma |
Entities: FulfillmentOrder, FulfillmentLineItem, FulfillmentStatusHistory, Package, PackageItem, Manifest.
State machine
isValidTransition returns false when the current status is missing from the map. It does not use the order helper’s “null means allow any” shortcut.
| From | To |
|---|---|
OPEN | PICKING, CANCELLED, ON_HOLD, LABEL_CREATING |
PICKING | PICKED, ON_HOLD, LABEL_CREATING |
PICKED | PACKING, ON_HOLD, LABEL_CREATING |
PACKING | PACKED, LABEL_CREATING |
PACKED | LABEL_CREATED, ON_HOLD, LABEL_CREATING |
LABEL_CREATING | LABEL_CREATED, LABEL_CREATION_FAILED, CANCELLED, ON_HOLD |
LABEL_CREATION_FAILED | LABEL_CREATING, CANCELLED, ON_HOLD, OPEN |
LABEL_CREATED | PACKED, READY_TO_SHIP, LABEL_CREATING |
READY_TO_SHIP | PACKED, PARTIALLY_SHIPPED, SHIPPED, LABEL_CREATING |
PARTIALLY_SHIPPED | PARTIALLY_SHIPPED, SHIPPED, ON_HOLD, LABEL_CREATING |
SHIPPED | DELIVERED, RTO_IN_TRANSIT, RTO_DELIVERED |
RTO_IN_TRANSIT | RTO_DELIVERED |
DELIVERED, RTO_DELIVERED, CANCELLED | none |
ON_HOLD | OPEN, PICKING, PICKED, PACKED, LABEL_CREATION_FAILED, LABEL_CREATED, READY_TO_SHIP, PARTIALLY_SHIPPED, SHIPPED, CANCELLED |
The source comment on PARTIALLY_SHIPPED says a fulfillment order may ship in several consignments and stays there until the last backordered line goes out. The self-edge is in the map.
Holds and post-dispatch outcomes
ON_HOLD exists only before dispatch: a dispatched worksheet (SHIPPED, DELIVERED, RTO_*) cannot be held. PARTIALLY_SHIPPED can, because its backordered lines are still in the building.
After dispatch the status follows courier tracking (shipment-fulfillment.listener.ts):
| Shipment event | Worksheet |
|---|---|
PICKED_UP, IN_TRANSIT, OUT_FOR_DELIVERY | SHIPPED |
DELIVERY_FAILED (NDR) | stays SHIPPED; shipped first if the pickup scan was missed. Worked from the NDR queue (hasOpenNdr list filter). |
RETURN_IN_TRANSIT | RTO_IN_TRANSIT |
RETURN_DELIVERED | RTO_DELIVERED (final) |
A return event ships the worksheet first when its pickup was never seen, so stock is consumed and the RTO return has shipped units. The worksheet moves to RTO_* only when no units are left to ship and every live forward parcel is returning (settledDispatchStatus). A delivered-plus-returned split ends DELIVERED. RTO_* statuses have no stock or order side effects: receiving and restock belong to the RTO return, which the returns module opens from the same tracking event. Only forced system writes may enter RTO_* (fulfillment.error.rtoFromTrackingOnly).
Releasing a hold resolves on the server (resolveHoldReleaseTarget): the worksheet resumes the status it was held from, or, if its goods already left, its parcels' outcome. Clients send OPEN.
Status groups that several modules branch on (post-dispatch, RTO, final, dispatch-settled) and the open-NDR worksheet filter live in fulfillment-status.util.ts, so order sync, cancellation, address changes, allocation and the dashboards ask "has this left?" from one list. A spec checks the groups against the state machine.
The order roll-up counts RTO_* siblings as dispatched and RTO_DELIVERED/CANCELLED siblings as settled, and never moves an order that is already in a return status.
OPEN can jump to LABEL_CREATING without PICKING. That is an implemented transition. Whether the warehouse UI always picks first is unknown.
Events and queues
Listeners: order.created, order.status.changed, order.location.changed, shipment.status.changed, stock.received. See Holds and post-dispatch outcomes for how shipment.status.changed moves a dispatched worksheet.
Queue: fulfillment_queue / batch-labels, concurrency 5, default 3 attempts with exponential backoff 2 seconds.
Stock side effects from fulfillment enqueue stock.sync.requested.
Integrations
Label files go to the courier strategy. When the courier returns a file, it is stored in S3. Carrier HTTP is not in the fulfillment database transaction. See Labels and Couriers.
Failure scenarios
LABEL_CREATION_FAILED is an operational status, not only a log line. It can return to LABEL_CREATING, CANCELLED, ON_HOLD, or OPEN.
If the courier accepted the shipment and the local transaction did not commit, the two sides diverge. Automatic compensating cancel was not verified per carrier.
Workflows
Development
Call isValidTransition before writing a new status. Enqueue stock sync in the same transaction as the stock mutation.