Skip to main content

2. SellerAccount, InboundPlan and InboundShipment: aggregates, invariants and the assumptions the model left open

Status​

Accepted (2026-10-24).

Context​

The model (network/model/model.yaml, context seller-inbound) names three aggregates, their invariants, errors, events and policies. It does not say how an id is minted, what a repeated command answers, how a network-issued SSCC is built, or which bounds apply. This ADR records what the code does and the assumptions it rests on. The assumptions are numbered; code and docs cite them as ADR 0002 A<n>.

Decision​

Aggregates (all in internal/domain, no import outside internal/domain)​

  • SellerAccount (seller): ACTIVE <-> SUSPENDED. A suspended seller creates no plan, shipment or carton and ships nothing (ErrSellerSuspended). Events SellerOnboarded, SellerSuspended, SellerReinstated.
  • InboundPlan (inboundplan): DRAFT -> CONFIRMED | CANCELLED. Lines are {catalogItemId, gtin, qtyEaches}, 1 to 500, one line per catalogue item; packingOption is INDIVIDUAL or CASE_PACKED. Event InboundPlanConfirmed.
  • InboundShipment with Carton entities (inboundshipment): DRAFT -> LABELLED -> SHIPPED -> RECEIVING -> CLOSED | CLOSED_WITH_DISCREPANCY. Events InboundShipmentLabelled, InboundShipmentShipped, InboundShipmentReceived, InboundDiscrepancyRecorded, SellerComplianceViolationRecorded.
  • network holds the two read-model value types (Node, PackLevel) and ErrNodeCannotReceive; shared holds Event, ErrValidation, ErrIllegalTransition and the GS1 value objects (shared/gs1: GTIN, SSCC, mod-10 check digit).

Every aggregate has Snapshot(), a validating Restore(Snapshot) and an optimistic Version(); now and ids are passed in. ErrNotFound and ErrConcurrentUpdate are port errors (internal/application/ports/errors.go).

Invariants and their errors​

ErrorRuleProblem slug
ErrCasePackNotUniformA HOMOGENEOUS_CASE carton holds exactly one item, one GTIN and one quantitycase-pack-not-uniform 422
ErrCaseQtyMismatchesGtinThe quantity of a case carton equals the unitsPerPack of the active CASE level of the itemcase-qty-mismatches-gtin 422
ErrCartonOverweightGross weight at most the node's maxCartonWeightG (when declared)carton-overweight 422
ErrCartonOversizeEvery edge fits the node's maxCartonDimsMm (when declared), sorted longest firstcarton-oversize 422
ErrSsccNotUniqueAn SSCC is unique network-wide (the sscc_registry primary key)sscc-not-unique 409
ErrInvalidSsccAn SSCC has 18 digits and a valid GS1 mod-10 check digitinvalid-sscc 422
ErrCartonContentsMismatchPlanAt labelling, the eaches per item and GTIN over all cartons equal the confirmed plancarton-contents-mismatch-plan 422
ErrNodeCannotReceiveThe node is known, ACTIVE, has the INBOUND capability (fulfillment centre or inbound cross-dock) and accepts pallets when a carton has a parent SSCCnode-cannot-receive 409
ErrSellerSuspendedSee SellerAccountseller-suspended 409
ErrShipmentNotLabelledA shipment ships only when LABELLEDshipment-not-labelled 409

The shared slugs are not-found 404, illegal-transition 409, concurrent-update 409 and validation-failed 422. The table lives in problemCatalogue (internal/adapters/inbound/http/errors.go); an unmapped error is an opaque 500 internal-error.

Assumptions​

  • A1 Ids. sellerId is chosen by the caller (shared.ValidID: 1 to 64 characters, alphanumeric first, then ., _, :, -). planId and shipmentId are minted by the service as pln-<uuid> and shp-<uuid>. Cartons are identified by their SSCC.
  • A2 Eaches. Every quantity (plan line, carton content, receipt item, event payload) is in eaches. Case counts are derived from the catalogue's unitsPerPack, never stored.
  • A3 Repeated commands. Only the seller account is idempotent by state: suspending a suspended seller or reinstating an active one answers the account unchanged with 200 and publishes no event. Every other command is strict: a transition the state forbids is illegal-transition 409 (confirm of a confirmed or cancelled plan, cancel of a confirmed plan, label of a labelled shipment, ship of a shipped one, a carton added to a labelled shipment), so a retry of a half-completed flow is told what state it is in; the REST Idempotency-Key replays the first response of a request that did succeed. Onboarding an existing sellerId is seller-exists 409.
  • A4 Order-tolerant views. The local read models are rebuilt from events that may arrive twice or out of order: a profile for an unregistered node creates the row with its limits and neither type nor status (so it cannot receive yet); a later registration fills the type and keeps a status already seen; only a profile revision newer than the one held replaces the limits; RETIRED is terminal and a later activation does not revive it. CatalogPackagingView keys on item, level and GTIN; a retire before a declare stores a retired row.
  • A5 SSCC issuance. When a carton carries no SSCC the network issues one: extension digit, then the company prefix (SSCC_COMPANY_PREFIX, mandatory, 6 to 11 digits, validated at boot), then the serial from the Postgres sequence sscc_serial_seq zero-padded to fill 16 digits, then the check digit. The extension digit is SSCC_EXTENSION_DIGIT, default 0. A seller-supplied SSCC is accepted only when valid and unique; an issued SSCC that collides with a seller-supplied one is re-issued (up to 5 attempts, then an error). The response says which (ssccIssued).
  • A6 Bounds. At most 500 plan lines, 1000 cartons per shipment, 100 content lines per carton, lot at most 20 characters (GS1 AI 10), expiry an ISO date; weight, dimensions and quantities at most the Postgres integer ceiling.
  • A7 Integer ceiling. shared.MaxInt32 (2147483647) bounds every integer the domain accepts, so no value reaches Postgres as an overflow.
  • A8 Receipt policy. The vendor's receipt (InboundReceiptSubmitted) is applied per carton, first answer final. Statuses RECEIVED, MISSING, UNEXPECTED, DAMAGED. For each manifest carton: MISSING records MissingCarton; DAMAGED records Damaged; a counted quantity below the manifest records Short, above records Over. A carton the manifest does not list is recorded as UnexpectedCarton and raises the compliance code UNMANIFESTED_CARTON. Every compliance code the vendor reports on a carton raises one SellerComplianceViolationRecorded per distinct code. The manifest is never edited. The shipment is RECEIVING after a partial receipt and closes when every manifest carton has an answer: CLOSED when nothing differed, otherwise CLOSED_WITH_DISCREPANCY, publishing InboundShipmentReceived once. A receipt for a closed shipment, or the same event twice, changes nothing.
  • A9 Absent limits. A node that declared no inbound limits receives any carton; a limit is checked only when declared. requiresSscc does not change anything: every carton always has an SSCC.
  • A10 Plan fit at labelling. A plan may be filled by several shipments over time; the plan check at labelling compares the cartons of THIS shipment with the plan lines (every line exactly, no extra item).
  • A11 Event routing. The three events other contexts consume (InboundShipmentLabelled, InboundShipmentShipped, InboundShipmentReceived) go to the events topic; the other six, which no context consumes, go to the analytics topic (ADR 0004).
  • A12 Retention. Idempotency keys older than 24 h are purged by a daily sweep. Aggregates are kept.

Consequences​

  • Handlers hold no business logic; a new rule lands in the aggregate with a table test and a boundary test, a new error in problemCatalogue, and a changed number here is a new ADR that supersedes this one.
  • The label data (SSCC, GS1-128 element string (00)<sscc>, human readable line, contents in eaches, destination node) is derived on read and never stored.

Addendum 2026-11-13: current state​

A11 describes the first nine events. streamFor (internal/adapters/outbound/kafka/encoder.go:206) now sends six events only to the events topic: InboundShipmentLabelled, InboundShipmentShipped, InboundShipmentReceived, PlacementPlanSuggested (ADR 0012), SellerSuspended and SellerReinstated (ADR 0013). Four stay analytics-only: SellerOnboarded, InboundPlanConfirmed, InboundDiscrepancyRecorded and SellerComplianceViolationRecorded. InboundShipmentReceived also carries received per item (ADR 0014). The contract is apis/asyncapi.yaml.

Addendum 2026-11-15: sellerId characters (A1) and plan fit at labelling (A10)​

  • A1 sellerId. A1 says a sellerId follows shared.ValidID, which allows :. It does not. A seller is the ownerRef of the stock it sends (ADR 0001), so a sellerId follows shared.ValidOwnerRef (internal/domain/shared/shared.go:126): 1 to 64 characters from A-Z a-z 0-9 . _ -, no :, and it may start with ., _ or -. apis/openapi.yaml declares the same pattern (^[A-Za-z0-9._-]+$), and seller.ID, inboundplan and inboundshipment all check it. shared.ValidID still governs the other opaque ids it checks (they may contain :). This addendum supersedes the character list in A1; the rest of A1 stands.
  • A10 plan fit at labelling. A10 can be read two ways: "every line exactly" for each shipment, or for the plan. The first contradicts its own first clause: a plan filled by several shipments over time can never have each shipment carry every line in full, and AddCarton already refuses a carton that takes the eaches of all the plan's shipments above a plan line (checkAgainstPlan, internal/domain/inboundshipment/carton_add.go:198). The rule as shipped: AddCarton caps each item at its plan line across ALL shipments of the plan, and LabelShipment requires "every line exactly, no extra item" only when the plan has a single shipment (PlanRules.SingleShipment, internal/application/usecases/shipments.go:114, checked at internal/domain/inboundshipment/shipment.go:203). With several shipments there is no exactness check at labelling: the plan's lines are a ceiling, not a per-shipment target. docs/docs/ddd/use-cases.md (LabelShipment) and the aggregate design canvas already say this; apis/openapi.yaml says only that the cartons "add up to the confirmed plan" and is unchanged.