Use Cases
As built
Derived from network/model/model.yaml. This context's service is built; the model carries its as-built contracts, and identifiers (Err... names, routes, CloudEvents types) are the names the service uses.
One section per command in contexts[linehaul-transportation].aggregates[*].commands
(15 commands). Package: internal/application/usecases. The
runtime sequence of each is on
Sequence Diagrams.
Reads (ListDepartures on GET /departures, and GET /departures/{departureId})
are not use cases: the HTTP adapter reads the departure repository directly.
Conventions for every use case
- One transaction. The aggregate change, its outbox rows and (for a
Kafka-driven use case) the
processed_eventsrow commit together. - Errors are RFC 9457
application/problem+jsonwithtypehttps://retailnetwork.example/problems/<slug>. Mapping: validation 422, conflict or state 409, not found 404, capacity or closed 409, stale 409. - Shared slugs. An unknown id is
ErrNotFound(404not-found), a command from a wrong stateErrIllegalTransition(409illegal-transition), an optimistic-concurrency conflictErrConcurrentUpdate(409concurrent-update) and a value-object ruleErrValidation(422validation-failed). The earlier placeholdersinvalid-state-transitionandconcurrent-modificationare not used (ADR 0002 A25;apis/openapi.yaml). - Idempotency. Every state-changing REST call takes an
Idempotency-Keyheader; a replay with the same key returns the first response. Every Kafka consumer is idempotent on the CloudEventsid. - Kafka-driven errors have no HTTP status. A domain refusal is logged and counted, the event is marked processed, and nothing is retried; an infrastructure failure rolls back and the message is redelivered.
ScheduleDeparture
- Actor / trigger:
GenerateDepartures(policyHorizonRoll) for each lane-schedule slot in the horizon; policyPickupFromCutoffsfor each cutoff of a vendor fulfillment node onNodeOperationalProfileDeclared. - Inbound adapter: internal. The scheduler (
GenerateDepartures) or the Kafka consumer ofretailnetwork.node-topology.events. No REST route. - Preconditions: the lane is in
LaneView; for a pickup, the node is inNodeProfileViewas a vendorFULFILLMENT_CENTER. - Main flow:
- Derive a deterministic
departureIdfrom the lane, the operating day and the slot (or cutoff). - If that departure exists, stop (idempotent).
- Check the lane is
OPENinLaneView. - Build
DepartureinSCHEDULEDwithDepartureKind,Times{cutoffAt, departsAt, arrivesAt}andCapacity{packages, booked: 0}. For aPICKUPdeparturedepartsAt = cutoffAt(the truck leaves at the cutoff) andarrivesAtis the cutoff plus the lane'snominalTransitMinutes(ADR 0002 A7). - Save it and write
DepartureScheduledto the outbox, one transaction.
- Derive a deterministic
| Error | HTTP | Problem type slug |
|---|---|---|
ErrInvalidDepartureTimes | 422 | invalid-departure-times |
ErrLaneClosed | 409 | lane-closed |
- Events emitted:
DepartureScheduled. - Idempotency: the deterministic
departureId; the inbox for the Kafka-driven path.
BookCapacity
- Actor / trigger: policy
BookOnLockonFulfillmentLockedfromorder-sourcing. - Inbound adapter: Kafka consumer of
retailnetwork.order-sourcing.events(typecom.retailnetwork.commerce.order-sourcing.assignment.FulfillmentLocked). - Preconditions: the departure
pickupDepartureIdexists and isSCHEDULED; no booking is recorded for thisassignmentId; the lock does not carryconsolidatedInto(a follower's lock books nothing: the inbox row is claimed and the use case stops). - Main flow:
- Insert the CloudEvents
idintoprocessed_events; if present, stop. - Load the departure by
pickupDepartureIdwith itsversion. - Refuse if
now > cutoffAt. - Refuse if
booked + 1 > packages. - Increment
booked; record the booking againstassignmentIdinExpectedVolumeView. - Save with the version check and write
DepartureCapacityChanged(capacityPackages,bookedPackages,remainingPackages) to the outbox, one transaction.
- Insert the CloudEvents
| Error | HTTP (if exposed) | Problem type slug |
|---|---|---|
ErrDepartureFull | 409 | departure-full |
ErrDepartureClosed | 409 | departure-closed |
ErrNotFound (unknown departure) | 404 | not-found |
ErrConcurrentUpdate (version conflict) | 409 | concurrent-update (retried by the consumer) |
- Events emitted:
DepartureCapacityChanged. - Idempotency: inbox on the CloudEvents
id; one booking perassignmentId. - Refusal follow-up (ADR 0002 A14): when the named departure cannot take
the booking (
DEPARTURE_UNKNOWN,DEPARTURE_CANCELLED,DEPARTURE_CLOSED,DEPARTURE_FULL) the service books the nextSCHEDULEDpickup departure of the same origin node and publishesDepartureCapacityChangedandBookingMoved; with none it publishesBookingFailedwith the first failure's reason.
ReleaseCapacity
- Actor / trigger: none of its own.
ReleaseCapacityis used only by the rebooking thatCancelDepartureperforms (ADR 0002 A15, A28). The release branch ofBookOnLockwas removed (gap G15):FulfillmentCancelledandFulfillmentReassigneddo not release a booking. - Inbound adapter: none (internal, inside the
CancelDeparturetransaction). - Preconditions: a booking is recorded on the departure being cancelled.
- Main flow:
CancelDeparturecancels the departure, then for every assignment booked on it releases the package (allowed onSCHEDULEDandCANCELLEDdepartures) and books the next eligible pickup departure by the rules ofBookCapacity(reasonDEPARTURE_CANCELLED), after all departures of one event were cancelled.- On a cancelled departure the release publishes no
DepartureCapacityChanged: the cancellation is the signal.
- Errors:
ErrIllegalTransitionfor a release with nothing booked. - Events emitted: none of its own (
DepartureCapacityChangedfor the target departure;BookingMovedorBookingFailed). - Idempotency: the cancellation is idempotent; a recorded booking is moved once.
- What
FulfillmentCancelledandFulfillmentReassigneddo: only updateExpectedVolumeView(remove the assignment, or change its node and forget its planned departure).order-sourcingraises both only beforeLOCKED, so nothing is booked yet; if it ever cancels after the lock the capacity stays booked until the departure leaves (ADR 0002 A16, reported model gap).
CloseDeparture
- Actor / trigger: driven by
CloseLoad(ADR 0002 A21): it runsCloseDeparturewhen the load's departure isSCHEDULED, in the load's transaction. No clock closes a departure:ErrDepartureClosedis computed fromnow > cutoffAtat booking time. - Inbound adapter: none of its own; the operator's REST
POST /loads/{loadId}/closuresreaches it. - Preconditions: the departure is
SCHEDULED. - Main flow:
- Load the departure.
- Transition
SCHEDULED → CLOSED. - Save with the version check.
| Error | HTTP (if exposed) | Problem type slug |
|---|---|---|
ErrIllegalTransition (not SCHEDULED) | 409 | illegal-transition |
ErrNotFound (unknown departure) | 404 | not-found |
- Events emitted: none (the model lists none for this command).
- Idempotency: a departure already
CLOSEDis returned unchanged.
RecordDeparture
- Actor / trigger: driven by
DispatchLoad(ADR 0002 A21): it runsRecordDepartureon the load's departure, which must beCLOSED, in the load's transaction. - Inbound adapter: none of its own; the operator's REST
POST /loads/{loadId}/dispatchesreaches it. - Preconditions: the departure is
CLOSED. - Main flow:
- Load the departure.
- Transition
CLOSED → DEPARTED, stampdepartedAt. - Save and write
DepartureDepartedto the outbox, one transaction.
| Error | HTTP (if exposed) | Problem type slug |
|---|---|---|
ErrIllegalTransition (not CLOSED) | 409 | illegal-transition |
ErrNotFound (unknown departure) | 404 | not-found |
- Events emitted:
DepartureDeparted(analytics only). - Idempotency: a departure already
DEPARTEDis returned unchanged.
RecordArrival
- Actor / trigger: driven by
ReceiveLoad(ADR 0002 A21): it runsRecordArrivalon the load's departure in the load's transaction. - Inbound adapter: none of its own; the operator's REST
POST /loads/{loadId}/receiptsreaches it. - Preconditions: the departure is
DEPARTED. - Main flow:
- Load the departure.
- Transition
DEPARTED → ARRIVED, stamparrivedAt. - Save and write
DepartureArrivedto the outbox, one transaction.
| Error | HTTP (if exposed) | Problem type slug |
|---|---|---|
ErrIllegalTransition (not DEPARTED) | 409 | illegal-transition |
ErrNotFound (unknown departure) | 404 | not-found |
- Events emitted:
DepartureArrived(analytics only). - Idempotency: a departure already
ARRIVEDis returned unchanged.
CancelDeparture
- Actor / trigger: network-ops planner; policy
PickupFromCutoffsonNodeSuspendedorNodeRetired(every future pickup of the node). - Inbound adapter: REST
POST /departures/{departureId}/cancellations(bodyreason); Kafka consumer ofretailnetwork.node-topology.events. - Preconditions: the departure is
SCHEDULED. - Main flow:
- Load the departure.
- Transition
SCHEDULED → CANCELLED, stampcancelledAtandreason. - Save and write
DepartureCancelledto the outbox, one transaction. - Return the departure (REST).
| Error | HTTP | Problem type slug |
|---|---|---|
ErrIllegalTransition (not SCHEDULED) | 409 | illegal-transition |
ErrNotFound (unknown departure) | 404 | not-found |
ErrConcurrentUpdate (version conflict) | 409 | concurrent-update |
- Events emitted:
DepartureCancelled(consumed bydelivery-promiseforRepromiseOnRisk, and byorder-sourcing). - Idempotency:
Idempotency-Key; a departure alreadyCANCELLEDis returned unchanged; inbox on the Kafka path. - Business rule: cancel before
cutoffAtwherever possible, so promises can be revised.
DefineLaneSchedule
- Actor / trigger: network-ops planner.
- Inbound adapter: REST
POST /lane-schedules. - Preconditions: the lane is known in
LaneView. - Main flow:
- Validate the slots (proposed shape: day of week, local cutoff, departure and arrival times, package capacity).
- Refuse overlapping slots on the lane.
- Create
LaneScheduleinACTIVEforlaneId. - Save. Departures appear at the next
GenerateDepartures.
| Error | HTTP | Problem type slug |
|---|---|---|
ErrOverlappingSlots | 422 | overlapping-slots |
ErrNotFound (unknown lane) | 404 | not-found |
ErrIllegalTransition (schedule already RETIRED for the lane) | 409 | illegal-transition |
- Events emitted: none (
LaneSchedule.events: []). - Idempotency:
Idempotency-Key.
RetireLaneSchedule
- Actor / trigger: not named on an
inboundrow; the route exists. Presumably the network-ops planner. - Inbound adapter: REST
POST /lane-schedules/{laneId}/retirements. - Preconditions: the schedule is
ACTIVE. - Main flow:
- Load the schedule.
- Transition
ACTIVE → RETIRED. - Save.
GenerateDeparturesskips it from now on.
| Error | HTTP | Problem type slug |
|---|---|---|
ErrIllegalTransition (not ACTIVE) | 409 | illegal-transition |
ErrNotFound (unknown schedule) | 404 | not-found |
- Events emitted: none.
- Idempotency:
Idempotency-Key; an alreadyRETIREDschedule is returned unchanged.
GenerateDepartures
- Actor / trigger: the scheduler, nightly (policy
HorizonRoll). - Inbound adapter: internal timer.
- Preconditions: none.
- Main flow:
- For each
ACTIVElane schedule, list the slots that fall in the next 14 days. - For each pickup lane, list the cutoffs of the origin node from
NodeProfileViewon its operating days (skipping holidays). - For each slot or cutoff with no departure yet, run
ScheduleDeparture.
- For each
| Error | HTTP | Problem type slug |
|---|---|---|
ErrInvalidDepartureTimes, ErrLaneClosed (per departure, logged, the rest continue) | — | — |
- Events emitted:
DepartureScheduledper new departure (throughScheduleDeparture). - Idempotency: deterministic
departureIds; a rerun schedules nothing new.
OpenLoad
- Actor / trigger: sort-centre or station operator.
- Inbound adapter: REST
POST /loads(bodydepartureId). - Preconditions: the departure exists at this context's origin node.
- Main flow:
- Load the departure; copy
originNodeId,destinationNodeIdandcapacityPackagesinto the new load (capacity snapshot forErrLoadOverCapacity). - Create
LoadinOPENwith a newloadId. - Save. Return
201with the load.
- Load the departure; copy
| Error | HTTP | Problem type slug |
|---|---|---|
ErrNotFound (unknown departure) | 404 | not-found |
- Events emitted: none.
- Idempotency:
Idempotency-Key.
AddPackage
- Actor / trigger: sort-centre or station operator (scanner).
- Inbound adapter: REST
POST /loads/{loadId}/packages(bodytrackingNumber). - Preconditions: the load is
OPEN. - Main flow:
- Load the load with its
version. - Refuse if the tracking number is on another
OPENorCLOSEDload. - Refuse if the package count would exceed the departure capacity.
- Add the tracking number; save with the version check.
- Load the load with its
| Error | HTTP | Problem type slug |
|---|---|---|
ErrPackageAlreadyLoaded | 409 | package-already-loaded |
ErrLoadOverCapacity | 409 | load-over-capacity |
ErrIllegalTransition (load not OPEN) | 409 | illegal-transition |
ErrNotFound (unknown load) | 404 | not-found |
ErrConcurrentUpdate (version conflict) | 409 | concurrent-update |
- Events emitted: none.
- Idempotency:
Idempotency-Key; adding a tracking number already on this load is a no-op.
CloseLoad
- Actor / trigger: sort-centre or station operator.
- Inbound adapter: REST
POST /loads/{loadId}/closures. - Preconditions: the load is
OPEN. - Main flow:
- Transition
OPEN → CLOSED. - Save and write
LoadClosed(loadId,departureId,packageCount) to the outbox, one transaction.
- Transition
| Error | HTTP | Problem type slug |
|---|---|---|
ErrIllegalTransition (load not OPEN) | 409 | illegal-transition |
ErrNotFound (unknown load) | 404 | not-found |
- Events emitted:
LoadClosed(analytics only). - Idempotency:
Idempotency-Key; aCLOSEDload is returned unchanged.
DispatchLoad
- Actor / trigger: sort-centre or station operator, when the truck leaves.
- Inbound adapter: REST
POST /loads/{loadId}/dispatches. - Preconditions: the load is
CLOSED. - Main flow:
- Transition
CLOSED → DEPARTED, stampdepartedAt. - Save and write
LoadDeparted(loadId,departureId,originNodeId,trackingNumbers,departedAt), one transaction.
- Transition
| Error | HTTP | Problem type slug |
|---|---|---|
ErrIllegalTransition (load not CLOSED) | 409 | illegal-transition |
ErrNotFound (unknown load) | 404 | not-found |
- Events emitted:
LoadDeparted(consumed byshipping-and-tracking). - Idempotency:
Idempotency-Key; aDEPARTEDload is returned unchanged.
ReceiveLoad
- Actor / trigger: operator at the destination node (for example a delivery station).
- Inbound adapter: REST
POST /loads/{loadId}/receipts. - Preconditions: the load is
DEPARTED. - Main flow:
- Transition
DEPARTED → ARRIVED, stamparrivedAt. - Save and write
LoadArrived(loadId,departureId,destinationNodeId,trackingNumbers,arrivedAt), one transaction.
- Transition
| Error | HTTP | Problem type slug |
|---|---|---|
ErrIllegalTransition (load not DEPARTED) | 409 | illegal-transition |
ErrNotFound (unknown load) | 404 | not-found |
- Events emitted:
LoadArrived(consumed byshipping-and-trackingandlast-mile-delivery, whoseInductOnArrivalinducts the packages). - Idempotency:
Idempotency-Key; anARRIVEDload is returned unchanged.