Use cases
Every use case is a struct in internal/application/usecases with an Execute
method over the shared Deps (the ports of
internal/application/ports). The inbound adapters decode and call them; they
never hold a rule. The step-by-step flows are on
Sequence diagrams, the payloads on
Events and the REST contract under
API reference.
At a glance
| Use case | Trigger | Inbound adapter | Event raised |
|---|---|---|---|
CreateCart | the shopper, via storefront-bff | REST POST /carts | none |
AddLine | the shopper | REST POST /carts/{cartId}/lines | CartLineAdded |
SetLineQuantity | the shopper | REST PUT /carts/{cartId}/lines/{catalogItemId} | none |
RemoveLine | the shopper | REST DELETE /carts/{cartId}/lines/{catalogItemId} | none |
SetDestination | the shopper | REST PUT /carts/{cartId}/destination | none |
QuoteDelivery | the shopper | REST POST /carts/{cartId}/delivery-quotes | CartDeliveryQuoted |
SelectDelivery | the shopper | REST PUT /carts/{cartId}/delivery-selection | none |
AcceptRequotedOffers | checkout, after a requote | REST POST /carts/{cartId}/delivery-requote-acceptances | none |
MergeInto | the shopper at sign-in | REST POST /carts/{cartId}/merges | none |
GetCart | the shopper, checkout, agents | REST GET /carts/{cartId}, A2A skill get_cart | none |
ListCarts | the shopper, agents | REST GET /carts?shopperId=&state=, A2A skill list_shopper_carts | none |
AbandonInactive | the clock | scheduler job, every ABANDON_SWEEP_INTERVAL | CartAbandoned |
PurgeAbandonedCarts | the clock | scheduler job, every PURGE_SWEEP_INTERVAL | none |
Consumer.ApplyCatalogItemCreated, ApplyCatalogItemDiscontinued | offer-availability events | Kafka consumer, retailnetwork.offer-availability.events | none |
Consumer.ApplyItemPriceChanged, ApplyItemPriceWithdrawn, ApplyDeliveryChargeScheduleChanged, ApplyTaxTableChanged | pricing events | Kafka consumer, retailnetwork.pricing.events | none |
Consumer.ApplyCheckoutCompleted (policy CloseOnCheckoutCompleted) | checkout event | Kafka consumer, retailnetwork.checkout.events | CartCheckedOut |
Common mechanics
- One unit of work. Every command loads the cart, calls the aggregate, saves
it and appends the events it raised to the outbox, inside one
ports.UnitOfWork. A rolled-back command leaves neither a cart change nor an outbox row (Deps.mutate,Deps.persist). - Optimistic concurrency. A save is conditional on the
versionthe cart was loaded at. A lost race isErrConcurrentUpdate; a command re-runs the whole attempt once on fresh state (commandAttempts = 2), then the REST adapter answers409concurrent-update. - Idempotency. Every
POSTroute requires anIdempotency-Keyheader of 1 to 128 printable ASCII characters (absent is400idempotency-key-required, malformed or over 128 characters is400idempotency-key-invalid). The first response below 500 for a key is stored for 24 hours and replayed withIdempotency-Replayed: true; the same key with a different method, path or body (another route included) is409idempotency-key-reused; a 5xx is never stored. - Ids. A
cartId,shopperId,catalogItemIdorofferIdis an opaque id of 1 to 64 characters fromA-Z a-z 0-9 . _ : -, starting with a letter or a digit (shared.ValidID). A request path identifier longer than 128 characters is a404. - Every change refreshes the idle clock. A change bumps
versionandlastChangedAt; the 30-day abandonment rule counts fromlastChangedAt. - Views. Each command returns the cart view: lines, destination, quote, selections, the labelled estimate and checkout readiness, computed at the use case's clock from the local read models.
- Error mapping. Domain, shared and port errors pass through unchanged and the REST adapter maps them to problem slugs (Troubleshooting).
CreateCart
- Trigger:
POST /carts. Input: optionalshopperId; the body may be empty (a guest cart). - Invariants:
shopperId, when present, is a valid id; the creation instant is set. The new cart isOPEN, empty,version1, with no destination. - Result:
201with the cart view. - Errors:
422validation-failed. - Events: none.
AddLine
- Trigger:
POST /carts/{cartId}/lines. Input:catalogItemId,quantity. - Invariants: the cart is
OPEN(ErrCartClosed); the id is valid; quantity is 1 to 99 (ErrQuantityOutOfRange); the item is purchasable in the localrm_itemscopy, that is catalogued, not discontinued and priced (ErrItemNotPurchasable); an item already in the cart merges quantities and the merged quantity must stay in 1 to 99; a new line must not make more than 100 lines (ErrTooManyLines). - Effect: the line is added or merged; the quote and every selection are cleared.
- Events:
CartLineAddedwith the merged quantity. - Metric: every attempt, success or failure, counts on
cart.lines.addedwithoutcomeand, when rejected, the problem slug asreason. - Errors:
404not-found,409cart-closedandconcurrent-update,422item-not-purchasable,quantity-out-of-range,too-many-lines,validation-failed.
SetLineQuantity and RemoveLine
- Triggers:
PUT /carts/{cartId}/lines/{catalogItemId}withquantity, andDELETE /carts/{cartId}/lines/{catalogItemId}. - Invariants: the cart is
OPEN; the cart holds a line for the item (ErrLineNotFound, answered404not-found); for a new quantity, 1 to 99. - Effect: the line changes or is dropped (the others keep their order); the quote and selections are cleared.
- Events: none (by design, ADR 0002: analytics sees adds only).
- Errors:
404,409cart-closed/concurrent-update,422quantity-out-of-range.
SetDestination
- Trigger:
PUT /carts/{cartId}/destination. Input:destinationPostalPrefix. - Invariants: the cart is
OPEN; the prefix is 1 to 10 letters or digits (a prefix, never an address). - Effect: the prefix is stored; the quote and selections are cleared. The prefix reaches the tax band through the next estimate.
- Events: none.
- Errors:
404,409,422validation-failed.
QuoteDelivery
- Trigger:
POST /carts/{cartId}/delivery-quotes(no body). - Preconditions: the cart is
OPENand has a destination (ErrNoDestination). - Flow: the cart is read; outside any transaction the use case calls
delivery-promise
POST /delivery-quoteswith the lines and the prefix, forwarding the request'sIdempotency-Key; then the cart is reloaded and, if itsversionis unchanged, the quote is stored in one unit of work. If the cart changed in between the quote is stale and the whole attempt runs once more before409concurrent-update. - Invariants on the quote: group numbers start at 1 and are unique; line
numbers lie within the cart's lines and belong to one group; each offer belongs
to its group, has a valid id, a known tier (
SAME_DAY,NEXT_DAY,TWO_DAY,STANDARD,CONSOLIDATED), two dates (deliverByEarliestnot afterdeliverByLatest) and anexpiresAt. An offer may carry acapacityBand(AMPLE,TIGHT,SCARCE) and, forCONSOLIDATEDonly,consolidationEligible(ADR 0010). A quote holds no capacity. - Effect: the quote replaces any previous one; the selections are cleared.
- Events:
CartDeliveryQuoted(destination prefix and group count; no offer detail). - Errors:
422no-destination;503delivery-promise-unavailable(timeout, transport error, 5xx, undecodable answer, or a 4xx, which is a contract bug);409cart-closed/concurrent-update;404.
SelectDelivery
- Trigger:
PUT /carts/{cartId}/delivery-selection. Input:groupNo,offerId. - Invariants: the cart is
OPEN; the offer belongs to that group of the current quote (ErrStaleDeliverySelection, also when there is no quote). - Effect: the selection of the group is set or replaced. Checkout readiness
holds when every group has a selection and no selected offer is expired
(
nowis not beforeexpiresAt). - Events: none.
- Errors:
404,409stale-delivery-selection/cart-closed/concurrent-update.
AcceptRequotedOffers
- Trigger:
POST /carts/{cartId}/delivery-requote-acceptances, called by checkout when its own requote replaced the offers. Input:offers, one per group, each withofferId,groupNo,tier,deliverByEarliest,deliverByLatest,expiresAt. - Invariants: the cart is
OPEN; at least one offer; one offer per group and distinct offer ids; each offer valid as above. - Effect: the offers replace the quote and each is selected for its group, without calling delivery-promise (checkout re-verifies each offer before authorizing). A group keeps the line numbers of the previous quote's group of the same number; a single new group with no predecessor spans every line.
- Events: none. Errors:
404,409,422validation-failed.
MergeInto
- Trigger:
POST /carts/{cartId}/merges. Input:targetCartId. The path cart is the source. - Invariants: the source is
OPEN; the target is anotherOPENcart that has a shopper; the source is a guest cart or belongs to the same shopper (ErrMergeTargetotherwise); the merged line list must fit 100 lines (ErrTooManyLines) and merged quantities clamp at 99. - Effect: the source's lines are taken into the target, the target's quote and
selections are cleared, the source becomes
MERGED(terminal). Both carts are saved in one unit of work, each version-checked; the target is returned. - Events: none. Errors:
404,409,422merge-target.
GetCart and ListCarts
- GetCart:
GET /carts/{cartId}, any state;404not-foundwhen absent. Checkout calls it at session open and reads the checkout readiness. - ListCarts:
GET /carts?shopperId=&state=, newest first.shopperIdis required and must be a valid id,stateis optional and one ofOPEN,CHECKED_OUT,ABANDONED,MERGED; a violation is422validation-failed. A shopper with no carts gets an empty list. - Both are also the A2A skills
get_cartandlist_shopper_carts, over the same use case structs.
Estimate (the view of every command)
estimate.Compute sums price times quantity over the lines the read model prices
(items). Without a destination it stops there and estimateComplete is false.
With one it adds the selected groups' tier charges from the current schedule (a
group's charge is waived when the group's goods reach the tier's freeOverMinor),
then tax: the band covering the first three characters of the prefix (upper-cased),
rate in basis points, rounded half up on goods plus delivery. estimateComplete
is true only when every line is priced, every group is selected with a known
tier charge, and a tax band covers the prefix. The currency is the one the priced
items state, falling back to CART_CURRENCY.
AbandonInactive (policy)
- Trigger: the scheduler, immediately at boot and then every
ABANDON_SWEEP_INTERVAL(default 1 h), in batches of 200 until a batch comes back short. - Rule: an
OPENcart unchanged for 30 days (cart.IdleAfter) becomesABANDONED. - Events:
CartAbandonedcarryinglastChangedAt. - Tolerance: a cart that changed, closed or vanished since the scan is skipped, never failed, so a second sweep or a second replica is harmless.
PurgeAbandonedCarts (policy)
- Trigger: the scheduler, every
PURGE_SWEEP_INTERVAL(default 6 h), batches of 200. - Rule: a cart that is not
OPENand unchanged for 30 days is deleted (FOR UPDATE SKIP LOCKED). This coversCHECKED_OUT,ABANDONEDandMERGED. - Events: none.
Consumer use cases
Each runs in one unit of work together with the inbox claim on the CloudEvents
id (processed_events): a redelivery is a no-op, a rollback releases the claim.
A transient failure re-runs the unit up to five times (consumerAttempts).
Validation failures are poison messages and are dead-lettered by the adapter.
| Use case | Effect | Validation |
|---|---|---|
ApplyCatalogItemCreated | marks the item catalogued in rm_items | valid catalogItemId |
ApplyCatalogItemDiscontinued | marks the item discontinued (terminal); lines already in carts stay | valid catalogItemId |
ApplyItemPriceChanged | records the effective price, applied only when version is above the stored price version | valid id, three upper-case letter currency, non-negative price, version at least 1 |
ApplyItemPriceWithdrawn | clears the price when the version is newer and remembers it, so a late older price is dropped | valid id, version at least 1 |
ApplyDeliveryChargeScheduleChanged | replaces rm_current_schedule when the version is higher | version at least 1; 1 to 16 tiers; a known tier; non-negative amounts |
ApplyTaxTableChanged | replaces rm_current_tax_table when the version is higher | version at least 1; 1 to 1000 bands; postalPrefixFrom set and not after postalPrefixTo; non-negative rate |
ApplyCheckoutCompleted | policy CloseOnCheckoutCompleted: the cart named by cartId becomes CHECKED_OUT and CartCheckedOut is raised with the session id | valid cartId and sessionId. An unknown cart or one that is not OPEN is acknowledged, logged (CheckoutCompleted names an unknown cart, ... for a cart that is not open) and left unchanged |
The adapter additionally rejects empty required fields and fields longer than 200
characters as poison (internal/adapters/inbound/kafka/handlers.go).