Use Cases
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 application service per command of model.yaml → contexts[shipping-and-tracking].aggregates[*].commands, in the proposed
package internal/application/usecases. Step-by-step diagrams are on
Sequence Diagrams; event
payloads are on Domain Events.
GET /journeys/{trackingNumber} (GetJourney) is a read, not a use case: the
HTTP adapter loads the journey through the repository and returns it.
Triggers at a glance
| Use case | Actor / trigger | Inbound adapter (proposed) |
|---|---|---|
IssueLabel | vendor-connectivity | REST POST /labels |
IssueReturnLabel | customer-returns | REST POST /return-labels (in-org, service ADR 0017) |
VoidLabel | Not named in the model | REST POST /labels/{labelRef}/voids |
StartJourney | Inside IssueLabel (no trigger named in the model) | none |
RecordScan | Five consumed event types; an unnamed REST caller | Kafka consumers; REST POST /journeys/{trackingNumber}/scans |
RaiseException | ExceptionOnSilence sweep | internal scheduler |
ReturnToSender | Kafka consumer of last-mile-delivery's PackageReturnInitiated (ADR 0002 A8) | Kafka consumer |
DeclareLost | The silence sweep, 48 h after an unanswered NO_SCAN (ADR 0002 A8) | internal scheduler |
How save and publish commit
Every use case that changes an aggregate saves it and writes its events to
outbox_events in one Postgres transaction. A relay publishes the
outbox rows to retailnetwork.shipping-and-tracking.events. Kafka consumers
add the CloudEvents id to processed_events in the same transaction.
package_journeys carries a version column; a save whose version moved
since the load is retried from the load (bounded retries; the model names
no Err... for it). The one synchronous call, GetShipTo, is made before
the transaction opens, so no lock is held across the network.
Error mapping for every REST route: validation 422, conflict or state 409,
not found 404, stale 409. Problems are application/problem+json
(RFC 9457) with type = the slug in the tables below.
IssueLabel
- Actor / trigger:
vendor-connectivity, while handling a vendor's label request (itsRequestLabel,POST /vendor/shipping-labels). - Inbound adapter: REST
POST /labels(in-org),Idempotency-Keyrequired. - Input (assumed, the model does not list it):
poNumber,packageNo,assignmentId,orderId,tier. - Preconditions: the purchase order is
ACCEPTED(checked by the caller,ErrLabelBeforeAcceptanceinvendor-connectivity); no label exists for(poNumber, packageNo). - Main flow:
- If the
Idempotency-Keywas seen with the same body, return the stored response. - Check that no label exists for
(poNumber, packageNo). - Call
customer-orderingGetShipTo(GET /orders/{orderId}/ship-to). - Encrypt the ship-to into a snapshot.
- Generate a tracking number and a
labelRef; choose the carrier. - Build the
ShippingLabel(ISSUED) and runStartJourneyfor the same tracking number (LABELLED). - In one transaction: insert the label, the snapshot, the journey and
the
LabelIssuedoutbox row. - Return
201withlabelRef,trackingNumber,carrierand the label metadata, never the address.
- If the
| Error | HTTP | Problem type |
|---|---|---|
ErrLabelAlreadyIssued | 409 | label-already-issued |
ErrTrackingNumberTaken | 409 | tracking-number-taken (a generator collision; the use case retries once with a new number before answering) |
Request validation (no Err... in the model) | 422 | validation-failed |
GetShipTo fails or times out (no Err... in the model) | 503 | ship-to-unavailable |
- Events:
LabelIssued. - Idempotency: the
Idempotency-Keyreplays the first response; a new key for the same(poNumber, packageNo)hits the unique constraint and answers409 label-already-issued.
IssueReturnLabel
- Actor / trigger:
customer-returns, inside a shopper's label request for a return authorization (service ADR 0017). The reverse twin ofIssueLabel. - Inbound adapter: REST
POST /return-labels(in-org),Idempotency-Keyrequired; bodyreturnAuthorizationId,orderId,packageNo,destinationPostalPrefix(the return destination's prefix, which only the caller knows). - Main flow:
- Read the ship-to of
orderIdfromcustomer-ordering(GetShipTo) before the transaction opens. - Derive the
stopKeyexactly like the outbound label of the same order (same secret over the same normalized address), so a return pickup stop groups with the outbound delivery stop. - Save an
ISSUEDlabel with purposeRETURNand thereturnAuthorizationId, tierSTANDARD, carrierRN-GROUND. No package journey is started and no ship-to snapshot is sealed. - Answer
201with aReturnLabel(nopoNumber, noassignmentId).
- Read the ship-to of
- Errors:
| Error | HTTP | Problem type slug |
|---|---|---|
ErrLabelAlreadyIssued (a label already covers this returnAuthorizationId and packageNo) | 409 | label-already-issued |
ErrTrackingNumberTaken | 409 | tracking-number-taken |
ErrValidation (a malformed request, or an order unknown to customer-ordering) | 422 | validation-failed |
ErrShipToUnavailable (no ship-to in time; no label is stored) | 503 | ship-to-unavailable |
- Events emitted:
LabelIssuedwith the optionalpurpose(RETURN) andreturnAuthorizationId. - Idempotency:
Idempotency-Key; a replay returns the first response.
VoidLabel
- Actor / trigger: not named in the model.
- Inbound adapter: REST
POST /labels/{labelRef}/voids, bodyreason,Idempotency-Keyrequired. - Preconditions: the label exists.
- Main flow:
- Load the label by
labelRef. - If it is already
VOIDED, return it unchanged (no second event). - Move it
ISSUED -> VOIDEDwith the reason. - In one transaction: save the label and write the
LabelVoidedoutbox row. - Return
200with the label.
- Load the label by
| Error | HTTP | Problem type |
|---|---|---|
Label not found (ErrNotFound) | 404 | not-found |
Request validation (ErrValidation) | 422 | validation-failed |
The journey is past LABELLED and still open (ErrVoidAfterHandover) | 409 | void-after-handover |
The journey is closed, for example DELIVERED (ErrJourneyClosed) | 409 | journey-closed |
- Events:
LabelVoided. - Journey: the void closes the
LABELLEDjourney of the same tracking number asVOIDEDin the same transaction (ADR 0002 A13). The journey is checked first: a delivered, returned, lost or already voided journey answersjourney-closed, an open journey pastLABELLEDvoid-after-handover. - Idempotency: voiding a
VOIDEDlabel returns it unchanged with200and publishes nothing.
StartJourney
- Actor / trigger: the
IssueLabeluse case, step 6. The model names the command but no trigger; running it insideIssueLabelis this pack's assumption. - Inbound adapter: none of its own.
- Preconditions: no journey exists for the tracking number.
- Main flow:
- Create
PackageJourneyinLABELLEDwithtrackingNumber,labelRef,poNumber,assignmentIdandorderId. - Saved in the
IssueLabeltransaction.
- Create
| Error | HTTP | Problem type |
|---|---|---|
ErrTrackingNumberTaken (a journey already holds the number) | 409 | tracking-number-taken |
- Events: none of its own (the model lists no journey-started event;
LabelIssuedannounces the package). - Idempotency: inherits
IssueLabel's.
RecordScan
- Actor / trigger: a consumed event, or an unnamed REST caller.
- Inbound adapters:
- Kafka consumer of
com.retailnetwork.partner.vendor-connectivity.purchaseorder.VendorShipmentConfirmed; - Kafka consumer of
com.retailnetwork.logistics.linehaul-transportation.load.LoadDepartedandcom.retailnetwork.logistics.linehaul-transportation.load.LoadArrived; - Kafka consumer of
com.retailnetwork.logistics.last-mile-delivery.route.PackageOutForDeliveryandcom.retailnetwork.logistics.last-mile-delivery.route.DeliveryAttempted; - REST
POST /journeys/{trackingNumber}/scans, bodyscanId,nodeId,scanType,at,Idempotency-Keyrequired.
- Kafka consumer of
- Preconditions: the journey exists and is open.
- Main flow:
- The adapter turns the fact into one
Scanper tracking number (scanId=<CloudEvents id>:<trackingNumber>for events; the caller'sscanIdfor REST). - Load the journey.
RecordScan, in this order: refuse on a closed journey (ErrJourneyClosed); refuse a knownscanId(ErrDuplicateScan); if the scan is older than the last applied scan, store it as not applied; a scan with no edge from the current state is stored as not applied and answersErrIllegalTransition; otherwise apply the transition from the fact (mapping on the Aggregate Design Canvas).- On
LABELLED -> HANDED_OVERraisePackageHandedOver(HandoverOnFirstScan); on-> DELIVEREDraisePackageDelivered; on every applied scan raisePackageScanned. - In one transaction: save the journey (version check), its new scan,
the outbox rows and, for a consumer, the
processed_eventsrow.
- The adapter turns the fact into one
| Error | HTTP (REST only) | Problem type | Consumer behaviour |
|---|---|---|---|
ErrDuplicateScan | 409 | duplicate-scan | treated as done, acknowledged |
ErrScanOutOfOrder | 409 | scan-out-of-order | scan stored, not applied, acknowledged |
ErrJourneyClosed | 409 | journey-closed | logged, acknowledged |
ErrIllegalTransition (no edge from the current state) | 409 | illegal-transition | scan stored, not applied, acknowledged |
Journey not found (no Err... in the model) | 404 | not-found | logged, counted, acknowledged |
Request validation (no Err... in the model) | 422 | validation-failed | not applicable |
- Events:
PackageScanned, plusPackageHandedOverorPackageDeliveredon those transitions. - Idempotency: the inbox on the CloudEvents
id, andErrDuplicateScanonscanIdfor a partly applied multi-package load event.
RaiseException
- Actor / trigger: the
ExceptionOnSilencesweep, on a timer. - Inbound adapter: internal scheduler.
- Preconditions: the journey is open and no scan arrived within the expected leg time plus tolerance. The model does not give this context the expected leg time (no departure, lane or wave event is consumed); see the canvas open questions.
- Main flow:
- Select open journeys whose next scan is overdue.
- For each,
RaiseExceptionwith reasonNO_SCAN; the state does not change. - In one transaction per journey: record the exception and write the
PackageExceptionRaisedoutbox row.
| Error | HTTP | Problem type |
|---|---|---|
ErrJourneyClosed | none (internal) | the journey closed between select and save; skipped |
- Events:
PackageExceptionRaised. - Idempotency: one exception per silence: the journey records when it last raised one, and the sweep skips it until a new scan arrives.
ReturnToSender
- Actor / trigger: the consumed
PackageReturnInitiatedoflast-mile-delivery(raised on the third failed attempt; ADR 0002 A8). - Inbound adapter: Kafka consumer of
com.retailnetwork.logistics.last-mile-delivery.packageattempthistory.PackageReturnInitiated(therouteentity spelling is accepted). - Preconditions: the journey is
AT_STATION. - Main flow: move
AT_STATION -> RETURNED; save with thePackageReturnedoutbox row in one transaction;returnedAtis the event's CloudEventstime.
| Error | HTTP | Problem type |
|---|---|---|
ErrJourneyClosed | 409 (if a route is added) | journey-closed |
- Events:
PackageReturned. - Idempotency: the inbox on the CloudEvents
id; a second call on aRETURNEDjourney hitsErrJourneyClosed.
DeclareLost
- Actor / trigger: the silence sweep (ADR 0002 A8): an
IN_TRANSITjourney whoseNO_SCANexception stayed unanswered (no applied scan since) for 48 hours is declared lost. The model names no other trigger. - Inbound adapter: internal scheduler.
- Preconditions: the journey is
IN_TRANSIT(the only state with aLOSTedge). - Main flow: move
IN_TRANSIT -> LOST; save with thePackageLostoutbox row in one transaction.
| Error | HTTP | Problem type |
|---|---|---|
ErrJourneyClosed | 409 (if a route is added) | journey-closed |
- Events:
PackageLost. - Idempotency: a second call on a
LOSTjourney hitsErrJourneyClosed.