Use cases
Every application use case of internal/application/usecases, in the order a context is used: catalogue, packaging,
listings, feeds, node events, then the reads. Each write runs in one unit of work (transact over UnitOfWork):
the aggregates it changes and the outbox rows for the events it raises commit together or not at all
(Architecture). Source: internal/application/usecases/*.go; the HTTP binding
is internal/adapters/inbound/http/handlers.go; the routes are in apis/openapi.yaml.
Every state-changing REST route needs an Idempotency-Key header (at most 200 printable characters, stored 24 hours); the
same key with the same method, path and body replays the stored answer with Idempotency-Replayed: true (a success or a final
refusal; a refusal that a retry could pass is not stored and its key runs again, ADR 0016)
(Troubleshooting).
| Use case | Trigger | Success | Events raised |
|---|---|---|---|
CreateCatalogItem | POST /catalog-items | 201 | CatalogItemCreated |
DiscontinueItem | POST /catalog-items/{catalogItemId}/discontinuations | 200 | CatalogItemDiscontinued, then ListingSuppressed per listing |
DeclarePackLevel | PUT /catalog-items/{catalogItemId}/packaging/{level} | 200 | CatalogItemPackLevelRetired (when it replaces an active level), CatalogItemPackagingDeclared |
RetirePackLevel | POST /catalog-items/{catalogItemId}/packaging/{level}/retirements | 200 | CatalogItemPackLevelRetired |
CreateListing | POST /listings | 201 | ListingCreated, and ListingSuppressed when it starts suppressed |
ApplyInventoryFeed | POST /inventory-feeds | 201, or 422 for a recorded FAILED feed | ListingAvailabilityChanged per changed listing, then InventoryFeedApplied |
ApplyNodeEvent | node-topology event on Kafka | offset committed | ListingSuppressed or ListingRestored per affected listing |
BrowseCatalog | GET /catalog, MCP browse_catalog | 200 | none |
GetCatalogItem | GET /catalog/{catalogItemId}, MCP get_catalog_item, A2A check_availability | 200 | none |
ListListings | GET /listings, MCP list_listings, A2A check_availability | 200 | none |
GetInventoryFeed | GET /inventory-feeds/{feedId}, MCP get_inventory_feed, A2A feed_outcome | 200 | none |
CreateCatalogItem
- Input:
title,lengthMm,widthMm,heightMm,weightG(all positive),hazmat,fragile, optionalidentifiers.gtin(raw as sent). - Flow: parse the GTIN when present (8, 12, 13 or 14 digits with a valid mod-10 check digit, normalised to 14;
variable-measure indicator 9 refused), refuse a GTIN another item or pack level already holds, mint the id
(
item-<uuid>), create the itemACTIVE, save, publish. - Invariants: dimensions and weight above zero; a GTIN identifies one thing forever.
- Errors:
invalid-gtin(422),gtin-already-assigned(409),invalid-item-dimensions(422),validation-failed(422).
DiscontinueItem
- Input: the catalogue item id.
- Flow: load (unknown is
not-found), discontinue. ADISCONTINUEDitem is returned unchanged with no event. Otherwise, in the same unit of work, every listing of the item that can legally be suppressed is suppressed with reasonDISCONTINUED(a listing already suppressed forNODE_NOT_SELLABLEchanges its reason; one alreadyDISCONTINUEDis skipped), then the events leave in the orderCatalogItemDiscontinued,ListingSuppressedby listing id. - Invariant: discontinuation is final, and so is the
DISCONTINUEDsuppression: a node event never restores it.
DeclarePackLevel and RetirePackLevel
- Input (declare):
level(EACH,INNER,CASE,PALLET, in the path),gtin,childGtin,unitsPerPack, optionaldimsMm(all three or none) andgrossWeightG. - Rules (ADR 0006): an
EACHlevel has exactly one unit per pack and no child; every higher level needs thechildGtinof an active lower level of the same item,unitsPerPacka multiple of the child's and at least twice it, and a gross weight no lighter than its contents;unitsPerPackis at most 1000000. A GTIN the item already holds can only be re-declared at its own active level with the sameunitsPerPackand child (dimensions and weight may change in place); the identical declaration returns the item unchanged with no event. - Replacing: a new GTIN at a level that has an active one retires the old level and declares the new one in one
aggregate version, so a retired level stays resolvable (
active: false). - Retire: retires the active definition at the level; a level with only retired history is returned unchanged with no
event; a level never declared is
pack-level-not-found(404). - A
DISCONTINUEDitem takes no packaging change (illegal-transition, 409). - Errors:
invalid-pack-level,invalid-gtin,invalid-pack-quantity,invalid-pack-dimensions,pack-level-order(all 422),pack-quantity-immutable,gtin-already-assigned,illegal-transition(409),not-found,pack-level-not-found(404).
CreateListing
- Input:
catalogItemId,nodeId, optionalownerRef(empty = operator-owned). - Flow: both ids and the owner must be well formed; the item must exist (
not-found); the (item, node, owner) pair must be free (duplicate-listing); the node's sellability comes from the localnode_status_view, and an unknown node is not sellable. The listing startsLIVEwith availability0/ basisUNKNOWNonly when the node isACTIVEand the item is not discontinued; otherwise it startsSUPPRESSEDwith reasonDISCONTINUED(item) orNODE_NOT_SELLABLE(node) and raisesListingSuppressedright afterListingCreated. - Errors:
not-found(404),duplicate-listing(409),validation-failed(422).
ApplyInventoryFeed
- Input:
feedId,nodeId,fullUpdate,asOf,linesoflistingId,availableQty,basis(PHYSICAL,THROUGHPUT_CONSTRAINED,UNKNOWN). - Request validation (nothing recorded): valid ids,
asOfpresent, at most 10000 lines, a partial feed has at least one line, no listing twice, a knownbasis, quantity at most 2147483647: otherwisevalidation-failed(422). - Flow: under the node's mark lock (
node_feed_marks, which serialises the feeds of a node), record the feedRECEIVED(a new feed, or aFAILEDone sent again), then settle it:- an
APPLIEDorSUPERSEDEDfeedIdisfeed-already-applied(409); - a feed with
asOfstrictly before the node's lastAPPLIEDfeed isSUPERSEDED: stored, nothing else changes,201; - a line naming no listing of the node (
listing-not-on-node) or a negative quantity (negative-availability) makes the feedFAILED: the feed row is kept (the middleware commits the writes of this 422), nothing else changes, and the answer is422with the problem body; - otherwise each line is applied (a line not newer than the listing's current
asOfis skipped and counted instaleCount), a full feed sets every other listing of the node to zero keeping its basis (zeroedCount), the changed listings are saved in bulk, the node's mark advances and the feed becomesAPPLIED(201).
- an
- Events: one
ListingAvailabilityChangedper changed listing, thenInventoryFeedApplied. - Metric: every outcome increments
availability.inventory_feeds.applied(Observability). - State machine:
RECEIVED->APPLIED|SUPERSEDED|FAILED;FAILED->RECEIVEDagain on a re-send of the same id.
ApplyNodeEvent
- Trigger: a CloudEvent on
retailnetwork.node-topology.eventsof typeNodeRegistered,NodeActivated,NodeSuspendedorNodeRetired(the consumer decodes; other types are skipped). - Flow, in one unit of work with the inbox row: a CloudEvents id already in
processed_eventsis skipped.NodeRegisteredcreates the view asPROPOSEDwhen the node is unknown and is otherwise a no-op. The other three go throughView.Observe: an observation older than the current status, or any observation on aRETIREDnode, isIgnoredand touches nothing. AChangedorRepeatedobservation runs the policySuppressOnNodeUnavailable:ACTIVErestores the listings of the node suppressed forNODE_NOT_SELLABLE;SUSPENDEDandRETIREDsuppress everyLIVElisting with that reason. Listings for which the move is illegal are skipped, so a replay is a no-op. - Errors: a malformed event (
validation-failed) is dead-lettered at once; a transient failure rolls back, is retried five times and then dead-lettered (Runbook).
Reads
| Use case | Inputs | Result |
|---|---|---|
BrowseCatalog | limit 1..500 (default 100), cursor | Items by id ascending, with nextCursor when another page exists |
GetCatalogItem | item id | The item with identifiers and packaging, or not-found |
ListListings | optional nodeId, catalogItemId (combine with AND), limit, cursor | Listings by id ascending with nextCursor |
GetInventoryFeed | feed id | The feed and its outcome (state, counters, failureCode), or not-found |
A limit that is not an integer from 1 to 500 is 400 invalid-limit, a bad cursor is 400 invalid-cursor and an undeclared query parameter is 400 unknown-query-parameter. The cursor is the last returned id.
Errors and the problem types they become
The mapping lives in internal/adapters/inbound/http/problems.go (problemCatalogue); the full table with remedies is on
Troubleshooting.
| Domain or application error | Problem type | Status |
|---|---|---|
ports.ErrNotFound | not-found | 404 |
shared.ErrIllegalTransition | illegal-transition | 409 |
ports.ErrConcurrentUpdate | concurrent-update | 409 |
catalog.ErrInvalidGTIN | invalid-gtin | 422 |
catalog.ErrGtinAlreadyAssigned | gtin-already-assigned | 409 |
catalog.ErrPackQuantityImmutable | pack-quantity-immutable | 409 |
catalog.ErrPackLevelOrder | pack-level-order | 422 |
catalog.ErrInvalidPackQuantity, ErrInvalidPackDimensions, ErrInvalidPackLevel, ErrInvalidItemDimensions | invalid-pack-quantity, invalid-pack-dimensions, invalid-pack-level, invalid-item-dimensions | 422 |
catalog.ErrPackLevelNotFound | pack-level-not-found | 404 |
listing.ErrDuplicateListing | duplicate-listing | 409 |
listing.ErrNegativeAvailability | negative-availability | 422 |
listing.ErrStaleAvailability, ErrNodeNotSellable | stale-availability, node-not-sellable | 409 |
feed.ErrListingNotOnNode | listing-not-on-node | 422 |
feed.ErrFeedAlreadyApplied | feed-already-applied | 409 |
shared.ErrValidation | validation-failed | 422 |
| anything else | internal-error | 500 |