Use cases
This page is a copy of docs/docs/ddd/use-cases.md on develop, derived from that repository's code. Edit it there, then re-sync.
Source: internal/application/usecases/*.go, internal/adapters/inbound/http/{server,errors}.go, internal/adapters/inbound/kafka/*.go, internal/adapters/inbound/mcp/tools.go.
Seventeen use cases live in internal/application/usecases: eight REST commands and nine consumer policies, plus the read side (Queries). The runtime sequence of the main ones is on Sequence diagrams; the events are described on Events.
Conventions
- One transaction. Every use case runs in one
ports.UnitOfWork: the aggregate writes, the outbox rows and, for consumers, the inbox claim commit or roll back together. - Every event is two outbox rows (integration topic and analytics twin, same CloudEvents id).
- Optimistic concurrency. Saves carry the version the aggregate was loaded at; a mismatch is
repository.ErrConcurrentModification(409 concurrent-modification). - Consumers return an outcome:
applied,duplicate(the CloudEvents id was already claimed for that consumer) orignored(nothing to do, still claimed).ErrInvalidEventis a deterministic payload problem and is dead-lettered without retry. - REST errors are
application/problem+jsonwithtypehttps://errors.outbound-shipping.warehouse-systems.dev/<slug>. Every POST needs anIdempotency-Key.
Error catalogue (problemCatalogue)
| Error | HTTP | Slug |
|---|---|---|
malformed body, bad parameter, unknown query parameter, ErrInvalidListQuery, ErrCancelReasonRequired, ErrRequiredField, ErrNoTimestamp, ErrVoidReasonRequired, a short-ship entry without a package or a reason (shared.ErrShortShipReasonRequired, ErrShortShipPackageRequired, shippingpackage.ErrShortShipReasonRequired, outboundload.ErrInvalidShortShip) | 400 | bad-request, invalid-parameter, unknown-query-parameter |
missing / reused Idempotency-Key | 400 / 409 | idempotency-key-required / idempotency-key-reused |
shippingpackage.ErrNotStagingLocation | 422 | not-staging-location |
shippingpackage.ErrWrongLoad | 409 | wrong-load |
shippingpackage.ErrPackageOnAnotherLoad | 409 | package-on-another-load |
shippingpackage.ErrAlreadyShipped | 409 | already-shipped |
shippingpackage.ErrInvalidTransition | 409 | package-state |
outboundload.ErrPackagesOutstanding | 409 | packages-outstanding |
outboundload.ErrTrailerNotDocked, ErrWrongDoor | 409 | trailer-not-docked |
outboundload.ErrLoadNotClosed | 409 | load-not-closed |
outboundload.ErrLoadConfirmed | 409 | load-confirmed |
outboundload.ErrDuplicateOpenLoad | 409 | duplicate-open-load |
outboundload.ErrInvalidTransition | 409 | load-state |
usecases.ErrLoadHasPackages | 409 | load-has-packages |
billoflading.ErrBolAlreadyIssued | 409 | bol-already-issued |
billoflading.ErrBolAlreadyVoided | 409 | bol-already-voided |
billoflading.ErrSealMissing | 409 | seal-missing |
repository.ErrLoadNotFound, ErrPackageNotFound, ErrBolNotFound | 404 | load-not-found, package-not-found, bol-not-found |
repository.ErrConcurrentModification | 409 | concurrent-modification |
| anything else | 500 | internal-error |
a dependency check failure on /readyz | 503 | dependency-unavailable |
REST commands
StagePackage: POST /packages/{packageId}/stagings
- Trigger: a dock operator scans a package into a staging location.
- Inputs: path
packageId; body{"staging_location": "<code>"}(required). - Flow: load the package, read the location from the layout copy,
ShippingPackage.Stage. - Invariants: the location must be a
Staginglocation of the package's own site (ErrNotStagingLocation); only anEXPECTEDpackage stages (aSTAGEDone at the same location is a no-op; any other state isErrInvalidTransition). - Raises:
PackageStaged.
LoadPackage: POST /packages/{packageId}/loadings
- Trigger: a dock operator scans a package onto its load.
- Inputs: body
{"load_id", "door_code"}(both required). - Invariants: the package must be assigned to a load, that load must be
load_id, anddoor_codemust equal the load's assigned door (ErrWrongLoadotherwise, including when the load has no door yet); a shipped package refuses withErrAlreadyShipped;EXPECTEDorSTAGEDonly. - Raises:
PackageLoaded.
UnloadPackage: POST /packages/{packageId}/unloadings
- Trigger: an operator takes a loaded package back off the load.
- Inputs: body
{"reason": "<text>"}(optional). - Invariants: only
LOADEDunloads (toSTAGED); aSHIPPEDpackage refusesErrAlreadyShipped. - Raises:
PackageUnloaded(the reason is omitted from the event when empty).
CloseLoad: POST /loads/{loadId}/closures
- Trigger: a dock supervisor closes the load.
- Inputs: body
{"short_ships": [{"package_id", "reason"}]}(optional list; each entry needs both fields, otherwise400 bad-requestbefore any package is touched). - Flow: mark each commanded short-ship
SHORT_SHIPPEDfirst, then collect the packages still outstanding. A package already short-shipped by the diversion policy that the command does not name is recorded once with reasonDIVERTED; one the command does name is recorded once with the reason the operator gave (the operator's entry wins, ADR 0010). - Invariants: the load must be
LOADING; no package may be neither loaded nor short-shipped (ErrPackagesOutstanding); an unknown package inshort_shipsispackage-not-found. - Raises:
LoadClosed(package_count= loaded packages,short_shipped_count).
ReopenLoad: POST /loads/{loadId}/reopenings
- Trigger: a supervisor reopens a closed load.
- Invariants:
CLOSEDonly (toLOADING); aSHIP_CONFIRMEDload never reopens (ErrLoadConfirmed). - Raises:
LoadReopened.
CancelLoad: POST /loads/{loadId}/cancellations
- Trigger: a supervisor cancels a planned load.
- Inputs: body
{"reason"}(required). - Invariants: the aggregate names the state refusals first: a
SHIP_CONFIRMEDload is409 load-confirmed(ErrLoadConfirmed), aLOADING,CLOSEDorCANCELLEDload is409 load-state(ErrInvalidTransition). Only aPLANNEDload goes on: if it already has packages assigned it refusesErrLoadHasPackages(409 load-has-packages: short-ship and close instead). - Raises:
LoadCancelled.
VoidBillOfLading: POST /bills-of-lading/{bolNumber}/voids
- Trigger: a supervisor voids an issued bill of lading.
- Inputs: body
{"reason"}(required). - Invariants:
ISSUEDonly (ErrBolAlreadyVoided). The shipment is not undone. - Raises:
BillOfLadingVoided.
SweepMissedLoads: POST /missed-load-sweeps
- Trigger: a scheduler around a cutoff.
- Inputs: body with
site_id(required) andcutoff(optional RFC 3339, default now). - Flow: list the site's
EXPECTEDorSTAGEDpackages on still-open loads whose cutoff has passed, flag each withFlagMissedLoad. - Invariants: at most once per package (the
missed_flaggedcolumn); a re-run flags nothing and publishes nothing. - Returns:
{"flagged": N, "swept_at": ...}. Raises:PackageMissedLoadper newly flagged package.
Consumer policies
Each handles one CloudEvents type on one topic (see Integration), claims (consumer, event id) first and returns an outcome.
ExpectPackage: PackageManifested
PlanLoadFromCutoff plus AssignPackageToLoad. Resolves the manifest's cpt instant in the site's timezone against the local schedule (ResolveCutoff) to a (cpt, operating date, ship method) key; finds the open load of that key or plans one; records the package EXPECTED and assigns it.
- Ignored when: no
site_idorcpton the manifest, no schedule copy for the site, or no schedule entry matches the cutoff's local time and weekday. - Invariants: one open load per key (
ErrDuplicateOpenLoad, backed byuq_open_load_per_cutoff; a raced insert retries the whole application once and joins the winner); an already-known package id is a no-op. - Raises:
LoadPlanned(only when this call planned the load).
DivertPackage: PackageDiverted
DropDiverted. An EXPECTED or STAGED package becomes SHORT_SHIPPED with reason DIVERTED, so its load can still close. Unknown, loaded, shipped or already short-shipped packages are ignored. Raises: nothing.
BookAppointment: DockAppointmentBooked
DoorFromAppointment. Always saves the appointment to the local copy. For an OUTBOUND appointment naming a cpt_id, assigns the door of that appointment to the first open load of (site, cpt) (AssignDoor, allowed while PLANNED or LOADING; a later appointment re-assigns).
- Ignored (copy still saved) when: the appointment is inbound, names no cpt, or no open load of that cpt exists yet (planned, not implemented: assigning the door from the saved copy when the load is planned later; see Troubleshooting).
- Raises:
LoadDoorAssigned.
DockTrailer: TrailerDocked
StartOnDock. Finds the open load at (site, door) (PLANNED first), attaches the trailer (ErrTrailerNotDocked when the load has no door, ErrWrongDoor when the door differs) and, when the load is still PLANNED, starts loading. A re-dock at a LOADING load attaches silently.
- Ignored when: no open load uses that door.
- Raises:
LoadingStarted(only when the state moved). There is no REST route to start loading.
RecordDoorWork: DoorWorkCompleted
Records the completion in door_work_completions. No state change in v1. Raises: nothing.
OnDeparture: TrailerDeparted
ConfirmOnDeparture and FlagDepartureWithoutClose (ADR 0002), split on the state of the latest load of (site, trailer):
| Load state | Effect |
|---|---|
CLOSED and the departure carries a seal_number | ConfirmShipment (SHIP_CONFIRMED), every LOADED package becomes SHIPPED, one BillOfLading is issued with the carrier and SCAC read from the appointment copy, the seal, the package count and total weight. |
CLOSED and no seal | Ignored: the document must certify the yard's seal; the load stays CLOSED. |
LOADING, or PLANNED with a trailer attached | Never confirmed. Every package not loaded raises PackageMissedLoad once; the load is left for a supervisor. |
SHIP_CONFIRMED or CANCELLED | Ignored. |
| no load has that trailer | Ignored. |
- Idempotency in three layers: the inbox claim (same CloudEvents id), the
SHIP_CONFIRMEDrefusal (any later departure) and the bill of lading's once-per-load invariant (UNIQUE load_id). - Invariants: only a
CLOSEDload confirms (ErrLoadNotClosed); the bill of lading needs a non-empty carrier, trailer and seal and at least one shipped package. - Raises:
ShipmentConfirmed(orders = distinctsource_order_idof the shipped packages, sorted) andBillOfLadingIssued, orPackageMissedLoad.
ApplyCptSchedule: CPTScheduleChanged
Replaces the site's whole schedule copy (cpt_schedules): last writer wins per site. Needs site, timezone, version at least 1, a changed-at instant, and every entry needs id, local time and ship method. Raises: nothing.
ApplyLayout: ZoneRegistered, LocationSlotRegistered
Saves the zone-to-site mapping and the location with its role into zones and locations; they may arrive in any order. A location's site is its zone's site; an empty role means Storage and never stages. Raises: nothing.
ProjectBoard: own analytics stream
Folds LoadPlanned, PackageStaged, PackageLoaded, PackageMissedLoad, LoadClosed, ShipmentConfirmed and LoadCancelled into load_board (staged, loaded and missed counts, closed-on-time, confirmed and cancelled stamps). Other own types are claimed and ignored. Raises: nothing.
Queries (Queries)
| Query | Served by | Notes |
|---|---|---|
GetLoad, ListLoads | GET /loads/{loadId}, GET /loads; MCP resource warehouse://outbound-shipping/loads/{loadId} | keyset pagination, limit 1 to 200 (default 50); nextCursor is null on the last page, also when that page is exactly full (the query reads one row past the page) |
GetPackage, ListLoadPackages | GET /loads/{loadId}/packages | filter by package state; same cursor rule |
GetBillOfLading | GET /bills-of-lading/{bolNumber} | |
AdherenceReport | GET /reports/load-adherence | reads load_board; adherence = loads_closed / loads_total; loads_closed counts every load that has closed, on time or late (ADR 0010) |
LoadsAtRisk | MCP loads_at_risk | open loads whose cutoff is within the horizon (default 4 h) |
MissedLoads | MCP missed_loads | packages that raised PackageMissedLoad, newest first |
AuditLoad | MCP audit_load (L2); A2A shipment_status with include=audit | the pre-departure consistency audit of one load from the local copies, through the pure loadreview.AuditLoad (ADR 0014) |
ClosureChecklist | MCP get_closure_checklist; A2A shipment_status with include=checklist | what one load still lacks to be closed out, through the pure loadreview.ClosureChecklist |