Use cases
One method of usecases.Service (internal/application/usecases) per command, query or consumed event. Each command
runs in one Postgres transaction (atomically): the aggregate change, its outbox rows and, for consumers, the
processed-event claim commit or roll back together. Nothing leaves the context synchronously: the node and packaging
facts are read from local tables fed by Kafka (Integration).
Conventions
- Errors are
application/problem+json; the status and slug of every error are in Troubleshooting. - Idempotency-Key is required on every
POST(1 to 128 characters, kept 24 h); a repeat with the same body replays the first answer (Idempotency-Replayed: true) when it was a 2xx or a refusal that is final for the request; a refusal that depends on state that can change (RETRYABLE, ADR 0018) is not stored and the same key executes again. The same key on a different method, path or body is409 idempotency-key-reused. - Seller must be
ACTIVEfor every plan, shipment and carton command and for confirm and ship (requireActiveSeller,internal/application/usecases/sellers.go). Cancelling a plan and the receipt consumer do not check it. - Destination node must be able to receive (
requireNode): known tonode_inbound_views,ACTIVE, typeFULFILLMENT_CENTERorINBOUND_CROSS_DOCK. An unknown node isnode-cannot-receive. - Quantities are always eaches; GTINs are normalised to 14 digits; variable-measure GTINs (indicator digit 9) are refused.
Source: internal/application/usecases/{sellers,plans,shipments,scorecard,consumers,placement}.go, domain rules in
internal/domain/{seller,inboundplan,inboundshipment,network,placementplan}.
Summary
| Use case | Trigger | Raises |
|---|---|---|
OnboardSeller | POST /sellers | SellerOnboarded |
SuspendSeller | POST /sellers/{sellerId}/suspensions | SellerSuspended |
ReinstateSeller | POST /sellers/{sellerId}/reinstatements | SellerReinstated |
CreateInboundPlan | POST /inbound-plans | none |
ConfirmInboundPlan | POST /inbound-plans/{planId}/confirmations | InboundPlanConfirmed |
CancelInboundPlan | POST /inbound-plans/{planId}/cancellations | none |
CreateInboundShipment | POST /inbound-plans/{planId}/shipments | none |
AddCarton | POST /inbound-shipments/{shipmentId}/cartons | none |
LabelShipment | POST /inbound-shipments/{shipmentId}/labels | InboundShipmentLabelled |
ShipInboundShipment | POST /inbound-shipments/{shipmentId}/dispatches | InboundShipmentShipped |
ComputePlacementPlan | POST /placement-plans | PlacementPlanSuggested |
AcceptPlacementPlan | POST /placement-plans/{planId}/acceptances | none |
ApplyInboundReceipt | InboundReceiptSubmitted on Kafka | InboundDiscrepancyRecorded, SellerComplianceViolationRecorded, InboundShipmentReceived |
ApplyNodeRegistered, ApplyNodeProfileDeclared, ApplyNodeActivated, ApplyNodeSuspended, ApplyNodeRetired | node-topology events | none (updates node_inbound_views) |
ApplyPackagingDeclared, ApplyPackLevelRetired | offer-availability events | none (updates catalog_pack_levels) |
ApplyListingAvailabilityChanged | offer-availability ListingAvailabilityChanged | none (updates listing_availability) |
ApplyFulfillmentReleased, ApplyFulfillmentCancelled | order-sourcing FulfillmentReleasedToNode, FulfillmentCancelled | none (updates demand_heat_lines) |
GetInboundPlan, GetInboundShipment, GetLabels, GetSellerScorecard | GET routes, MCP tools, A2A skills | none |
GetPlacementPlan, ListPlacementPlans | GET /placement-plans/{planId}, GET /placement-plans, MCP tools | none |
Seller commands
OnboardSeller
- Inputs:
sellerId(1 to 64 characters ofA-Z a-z 0-9 . _ -, it is also theownerRefof the stock),name(non-blank, trimmed, at most 120 characters). - Invariants: the id is not onboarded yet (
seller-exists). - Result: an
ACTIVEseller,SellerOnboardedappended to the outbox.
SuspendSeller and ReinstateSeller
- Inputs:
sellerId; for suspend an optionalreason(trimmed, at most 200 characters). - Invariants: the seller exists (
not-found). Suspending a suspended seller, or reinstating an active one, is a no-op that raises no event and returns the seller (it is not anillegal-transition). - Effect while suspended: plan creation and confirmation, shipment creation, carton adding, labelling and shipping
are refused (
seller-suspended). A shipment alreadySHIPPEDstill closes on the vendor's receipt. - Events:
SellerSuspended,SellerReinstated.
Inbound plan commands
CreateInboundPlan
- Inputs:
sellerId,destinationNodeId,packingOption(INDIVIDUALorCASE_PACKED),lines(1 to 500; eachcatalogItemIdonce, a valid GTIN of 8, 12, 13 or 14 digits,qtyEachesfrom 1 to 2147483647). - Invariants: seller
ACTIVE; node able to receive; the lines above. - Result: a
DRAFTplan at version 1. No event: a draft is private to the seller. - Note: the plan is not checked against the packaging view. Whether a
CASE_PACKEDitem has an active CASE level is enforced onAddCarton, not here.
ConfirmInboundPlan
- Invariants: plan
DRAFT(illegal-transitionotherwise); seller stillACTIVE; node still able to receive. - Events:
InboundPlanConfirmed(seller, node, packing option and lines).
CancelInboundPlan
- Invariants: plan
DRAFT.CONFIRMEDandCANCELLEDare terminal. No event is published for a cancellation.
Shipment commands
CreateInboundShipment
- Inputs:
planId. - Invariants: plan
CONFIRMED; sellerACTIVE; node able to receive. A confirmed plan may have several shipments. - Result: an empty
DRAFTshipment (shp-...) for the plan's destination node. No event.
AddCarton
- Inputs:
contents(1 to 100 lines ofcatalogItemId,gtin,qtyEaches, optionallotup to 20 characters and ISOexpiry),grossWeightG(at least 1),dimsMm(l,w,hat least 1), optionalkind, optionalsscc, optionalparentSscc. - Invariants (
internal/domain/inboundshipment/carton.go):- shipment
DRAFTand fewer than 1000 cartons; - SSCC: a supplied one is 18 digits with a valid mod-10 check digit (
invalid-sscc) and unique in the shipment and insscc_registry(sscc-not-unique); without one the network issues it fromSSCC_COMPANY_PREFIXandSSCC_EXTENSION_DIGIT(re-issued up toSsccIssueAttemptstimes if it collides); parentSsccdiffers from the SSCC, and the node must not forbid pallets (acceptsPallets=falsegivesnode-cannot-receive);- kind: a
CASE_PACKEDplan makes every cartonHOMOGENEOUS_CASEwith exactly one item (case-pack-not-uniformif askedMIXEDor more than one item); anINDIVIDUALplan makes itMIXED; - node limits: weight at most
maxCartonWeightG(carton-overweight), size fitsmaxCartonDimsMmin some orientation (carton-oversize); a limit the node never declared is not checked; - plan fit: each item is on the plan and the total over this shipment and the plan's other shipments does not exceed
the plan line (
carton-contents-mismatch-plan); - case-packed: the item is packed at one quantity per carton (
case-pack-not-uniform), and that quantity equals theunitsPerPackof an active CASE level with this GTIN incatalog_pack_levels(case-qty-mismatches-gtin).
- shipment
- Result: the carton,
ssccIssued, and its label data. No event: cartons are published with the shipment when it is labelled.
LabelShipment
- Invariants: shipment
DRAFTwith at least one carton; sellerACTIVE; node able to receive. If the plan has only this one shipment, its cartons must add up to every plan line exactly (carton-contents-mismatch-plan). - Effect:
LABELLED, every carton sealed, contents frozen. - Events:
InboundShipmentLabelledcarrying the whole carton manifest.
ShipInboundShipment
- Invariants: shipment
LABELLED(DRAFTgivesshipment-not-labelled, any later stateillegal-transition); sellerACTIVE; node still able to receive. - Events:
InboundShipmentShipped. Every ship attempt, successful or not, increments the counterseller_inbound.shipment.shipped(Observability).
Placement advisory commands
The advisory of ADR 0012 (internal/application/usecases/placement.go). It
suggests, per vendor fulfilment node, how many units of one item to send; it moves no stock.
ComputePlacementPlan
- Inputs:
sellerId,catalogItemId, optionalcoverDays(default 14, from 1 to 90). - Invariants: seller
ACTIVE(requireActiveSeller);coverDaysin range (ports.ErrInvalidCoverDays). - Effect: reads the demand view over the 28-day observation window, the vendor fulfilment nodes and the node
availability, builds the suggestions deterministically, moves the seller's previous plan of the item to
SUPERSEDEDand records the new plan asSUGGESTED(plp-...). An item with no demand yields an empty plan, still recorded and still published. - Events:
PlacementPlanSuggested.
AcceptPlacementPlan
- Invariants: the plan exists (
not-found) and isSUGGESTED(illegal-transitionotherwise). The seller is not checked. - Effect:
ACCEPTED. Advisory only in v1: no event and no effect outside the aggregate.
Consumed events
ApplyInboundReceipt (policy ReceiveOnVendorReceipt)
- Trigger:
InboundReceiptSubmittedfrom vendor-connectivity. - Invariants (
Shipment.RecordReceipt): the shipment exists (otherwisenot found, dead-lettered at once); it isSHIPPEDorRECEIVING(aCLOSEDorCLOSED_WITH_DISCREPANCYshipment ignores the receipt as a no-op); the receipt'snodeIdis the shipment's node; every carton has a valid SSCC, a status inRECEIVED,MISSING,UNEXPECTED,DAMAGED, and appears once. - Effect: the first answer for a carton is final. A
MISSING,UNEXPECTEDorDAMAGEDcarton and every quantity difference per GTIN (Short,Over) become anInboundDiscrepancyRecorded. A carton the manifest does not list is recorded once asUNEXPECTEDwith the codeUNMANIFESTED_CARTON. Each distinct compliance code of a carton becomes aSellerComplianceViolationRecorded. When every manifest carton has an answer the shipment closes (CLOSED, orCLOSED_WITH_DISCREPANCYwhen anything differed) andInboundShipmentReceivedis raised; until then it isRECEIVING. - Same transaction: the shipment, the outbox rows, the scorecard facts (
seller_scorecard_receiptsandseller_scorecard_occurrences) and theprocessed_eventsclaim. - Errors: none are returned to a caller. A transient database error is retried; a permanent one (validation, illegal transition, unknown shipment) is dead-lettered (Troubleshooting).
Node and packaging views
ApplyNodeRegistered, ApplyNodeProfileDeclared, ApplyNodeActivated, ApplyNodeSuspended, ApplyNodeRetired fold
node-topology events into node_inbound_views; ApplyPackagingDeclared and ApplyPackLevelRetired fold
offer-availability events into catalog_pack_levels. They tolerate duplicates and out-of-order delivery: a status
already seen is kept when NodeRegistered arrives late, a RETIRED node stays retired, only a profile revision newer
than the one held replaces the limits, and a retired GTIN is never reactivated. Each is claimed in processed_events
by the CloudEvents id.
Demand and availability views (placement advisory)
Three more consumer use cases feed the read model of the advisory (ADR 0012),
each claimed in processed_events by the CloudEvents id:
ApplyListingAvailabilityChanged(offer-availabilityListingAvailabilityChanged) saves the node's current declared availability of the item inlisting_availability.ApplyFulfillmentReleased(order-sourcingFulfillmentReleasedToNode) records the release's lines indemand_heat_lines; a re-release of the same assignment group replaces its lines.ApplyFulfillmentCancelled(order-sourcingFulfillmentCancelled) removes the group's lines.
The two order-sourcing use cases are read by the fourth consumer cmd/api starts
(ADR 0016); see Integration.
Queries
| Query | Reads | Notes |
|---|---|---|
GetInboundPlan | inbound_plans | not-found for an unknown plan. |
GetInboundShipment | inbound_shipments and cartons | State, cartons, per-carton receipt status. |
GetLabels | the shipment's cartons | One label per carton: LBL-<sscc>, GS1-128 element string (00)<sscc>, a human-readable line (00) E PPPP PPPP SSSS SSSS C, contents summary in eaches. Available in any state. |
GetSellerScorecard | seller_scorecard_receipts, seller_scorecard_occurrences | window 7d or 30d (default 30d, anything else invalid-window); unknown seller not-found; a suspended seller has a scorecard. A pure read: it raises nothing and never suspends the seller. |
GetPlacementPlan | placement_plans | not-found for an unknown plan. |
ListPlacementPlans | placement_plans | One seller's plans, optionally of one item, newest first, keyset-paged (internal/application/usecases/placement.go). |