Use Cases
Derived from model/model.yaml. The shopping-cart service is built on develop (IQVO/shopping-cart at 95bc5bc, checked 2026-10-27) and not deployed (no warehouse-infra entry). Every Err... name, REST route and CloudEvents type of this context named on this page exists in the service; where the built service differs from the model's design, the page says so (As built) or the context's Service and operations section lists it.
One section per command of contexts[shopping-cart].aggregates[Cart].commands,
plus the two policies and the view projector. They are application
services in internal/application/usecases. The step-by-step diagrams are
on Sequence Diagrams;
the payloads are on Domain Events;
the REST contract is storefront/apis/shopping-cart/openapi.yaml.
Reads are not use cases here: GET /carts/{cartId} (GetCart, with the
CheckoutReadiness view and the labelled estimate — also called by
checkout at session open) and GET /carts?shopperId=&state=
(ListCarts) read the repository and views directly.
Triggers at a glance
| Use case | Trigger | Inbound adapter | Emits |
|---|---|---|---|
CreateCart | Shopper via storefront-bff | REST POST /carts | none |
AddLine | Shopper via storefront-bff | REST POST /carts/{cartId}/lines | CartLineAdded |
SetLineQuantity | Shopper via storefront-bff | REST PUT /carts/{cartId}/lines/{catalogItemId} | none |
RemoveLine | Shopper via storefront-bff | REST DELETE /carts/{cartId}/lines/{catalogItemId} | none |
SetDestination | Shopper via storefront-bff | REST PUT /carts/{cartId}/destination | none |
QuoteDelivery | Shopper via storefront-bff | REST POST /carts/{cartId}/delivery-quotes | CartDeliveryQuoted |
SelectDelivery | Shopper via storefront-bff | REST PUT /carts/{cartId}/delivery-selection | none |
MergeInto | Shopper via storefront-bff (the sign-in moment, D4) | REST POST /carts/{cartId}/merges | none |
AbandonInactive | Clock, policy AbandonInactive (30 days) | scheduler | CartAbandoned |
CloseOnCheckoutCompleted | checkout CheckoutCompleted | Kafka consumer, retailnetwork.checkout.events | CartCheckedOut |
Project PurchasabilityView, EstimateView | offer-availability, pricing | Kafka consumers, retailnetwork.offer-availability.events, retailnetwork.pricing.events | none |
How save and publish commit
Every use case that changes the cart loads it with its version, calls
the aggregate, then saves it inside one Postgres transaction. The save is
conditional on the version it loaded (UPDATE ... WHERE version = ?); a
conflict is retried once inside the use case, then surfaces as 409
concurrent-update. If an event is raised, the outbox row
(outbox_events) is written in the same transaction. A consumer-driven
use case also inserts the CloudEvents id into processed_events in that
transaction. A relay publishes outbox rows to
retailnetwork.shopping-cart.analytics after the commit.
Error responses use application/problem+json (RFC 9457) with type
/problems/<slug> per conventions.integration_rules: not-found 404,
illegal-transition / cart-closed 409, concurrent-update 409,
validation-failed 422, plus one kebab-case slug per Err in the model.
Every write route takes an Idempotency-Key header, minted by the BFF: 1 to
128 printable ASCII characters (absent is 400 idempotency-key-required,
malformed or longer than 128 characters is 400 idempotency-key-invalid). A
replay of the same request returns the stored first response with the header
Idempotency-Replayed: true (only a replay carries it); the same key with a
different request is 409 idempotency-key-reused, never 422.
CreateCart
- Actor / trigger: the shopper, through
storefront-bff. - Inbound adapter: REST
POST /carts, headerIdempotency-Keyrequired. - Input: optional
shopperId(guest-first, D4; absent for a guest). - Main flow:
Cart.CreateCart(shopperId, now)creates anOPEN, empty cart; insert; store the response under the key. - Errors: 422
validation-failedonly. - Events emitted: none (
CreateCartpublishes nothing — hotspot H1). - Idempotency:
Idempotency-Key; the BFF keeps the returnedcartIdin its cookie.
AddLine
- Actor / trigger: the shopper, through
storefront-bff. - Inbound adapter: REST
POST /carts/{cartId}/lines, headerIdempotency-Keyrequired. - Input:
catalogItemId,quantity. - Preconditions: the cart is
OPEN; the item is purchasable inPurchasabilityView. - Main flow:
Idempotency-Keylookup; replay the stored response on a hit.- Load
Cart(404not-found). Cart.AddLine(...): one line per item — re-adding merges quantities (ErrDuplicateLineis the invariant behind the merge); quantity bounds and the line cap; the quote and the selections are cleared.- In one transaction: save (version-checked), outbox
CartLineAddedwith the merged quantity, store the key response. - Answer
200with the cart.
- Errors:
| Error | HTTP | Problem type slug | Note |
|---|---|---|---|
ErrItemNotPurchasable | 422 | item-not-purchasable | The view is fed by catalogue and price events only (hotspot H3). |
ErrQuantityOutOfRange | 422 | quantity-out-of-range | Quantity must be in 1..99. |
ErrTooManyLines | 422 | too-many-lines | At most 100 lines (customer-ordering A15). |
ErrCartClosed | 409 | cart-closed | The cart is CHECKED_OUT, ABANDONED or MERGED. |
| version conflict | 409 | concurrent-update | Retried once inside the use case. |
- Events emitted:
CartLineAdded(analytics). - Idempotency:
Idempotency-Key.
SetLineQuantity
- Actor / trigger: the shopper, through
storefront-bff. - Inbound adapter: REST
PUT /carts/{cartId}/lines/{catalogItemId}, headerIdempotency-Keyrequired. - Main flow: load the cart (404), set the line's quantity
(
ErrQuantityOutOfRange; unknown line → 404not-found), clear quote and selections, save (version-checked), one transaction. - Errors: as
AddLine, minusErrItemNotPurchasableandErrTooManyLines. - Events emitted: none (hotspot H1 — analytics never sees a quantity change).
- Idempotency:
Idempotency-Key.
RemoveLine
- Actor / trigger: the shopper, through
storefront-bff. - Inbound adapter: REST
DELETE /carts/{cartId}/lines/{catalogItemId}, headerIdempotency-Keyrequired. - Main flow: load the cart (404), remove the line (unknown line → 404), clear quote and selections, save (version-checked), one transaction.
- Errors:
ErrCartClosed409; 404not-found; 409concurrent-update. - Events emitted: none (hotspot H1).
- Idempotency:
Idempotency-Key; replaying returns the stored response.
SetDestination
- Actor / trigger: the shopper, through
storefront-bff. - Inbound adapter: REST
PUT /carts/{cartId}/destination, headerIdempotency-Keyrequired. - Input:
destinationPostalPrefix— a prefix, never an address. - Main flow: load the cart (404), set the prefix, clear quote and selections, save (version-checked), one transaction.
- Errors: 404
not-found; 409cart-closed/concurrent-update; 422validation-failed. - Events emitted: none; the prefix reaches the tax band through the next quote and every later estimate.
- Idempotency:
Idempotency-Key.
QuoteDelivery
- Actor / trigger: the shopper (or the BFF on their behalf), through
storefront-bff. - Inbound adapter: REST
POST /carts/{cartId}/delivery-quotes, headerIdempotency-Keyrequired. - Preconditions: the cart is
OPENand a destination prefix is set. - Main flow:
Idempotency-Keylookup.- Load
Cart(404);ErrNoDestinationwithout a prefix (422). - Call
delivery-promiseQuoteDeliveryOffers(REST POST /delivery-quotes, the network's Open Host Service route for theshopper / storefrontcaller) with the cart's lines and the prefix — the cart's only synchronous call. Cart.QuoteDelivery(quote, now): hold the groups with their lines, offers (offerId, tier, window,expiresAt) and the unavailable tiers, with their constraints. A quote holds no capacity.- In one transaction: save (version-checked), outbox
CartDeliveryQuoted, store the key response. - Answer
200with the cart, its quote and the labelled estimate.
- Errors:
| Error | HTTP | Problem type slug | Note |
|---|---|---|---|
ErrNoDestination | 422 | no-destination | Set a destination first. |
delivery-promise unreachable or timed out | 503 | delivery-promise-unavailable | Lines and prefix are kept; retry with the same Idempotency-Key. |
ErrCartClosed | 409 | cart-closed | |
| version conflict | 409 | concurrent-update |
- Events emitted:
CartDeliveryQuoted(analytics). - Idempotency:
Idempotency-Key.
SelectDelivery
- Actor / trigger: the shopper, through
storefront-bff. - Inbound adapter: REST
PUT /carts/{cartId}/delivery-selection, headerIdempotency-Keyrequired. - Input:
groupNo,offerId. - Preconditions: the cart is
OPENand holds a current quote. - Main flow: load the cart (404), record the selection
(
groupNotoofferId) — the offer must belong to the current quote (ErrStaleDeliverySelection); after every group is selected and no offer has expired,CheckoutReadinessholds. Save (version-checked), one transaction. - Errors: 404
not-found; 409stale-delivery-selection/cart-closed/concurrent-update. There is no 422: a missinggroupNoorofferId, a group the quote does not have, or a cart with no quote all name no offer of the current quote and answer 409stale-delivery-selection. - Events emitted: none (hotspot H1).
- Idempotency:
Idempotency-Key; re-selecting the same offer replays the stored response. - Note: expiry is a fact about the clock, not a stored flag —
readiness compares each
QuotedOffer'sexpiresAt, and checkout re-checks withErrCartNotCheckoutReadyat session open.
MergeInto
- Actor / trigger: the shopper at the sign-in moment (D4): the BFF merges the guest cart into the shopper's open cart.
- Inbound adapter: REST
POST /carts/{cartId}/merges, headerIdempotency-Keyrequired. - Input:
targetCartId. - Preconditions: the source is
OPEN; the target is anotherOPENcart that has a shopper, never the cart itself; the source is a guest cart (no shopper) or belongs to the same shopper as the target (ErrMergeTarget). - Main flow: load both carts and versions (404), take the source's
lines into the target, merging quantities (a merged quantity clamps at 99;
the merged line list must fit 100 lines, else
ErrTooManyLinesand neither cart changes), clear the target's quote and selections (its lines changed), mark the sourceMERGED; both saves in one transaction, both version-checked. - Errors: 404
not-found; 409cart-closed/concurrent-update; 422merge-target/too-many-lines/validation-failed. - Events emitted: none (hotspot H1 — analytics never sees a merge).
- Idempotency:
Idempotency-Key; the source is terminal after the merge (ErrCartClosedon any later change).
AbandonInactive (clock)
- Actor / trigger: the clock; policy
AbandonInactive— anOPENcart with no change for 30 days. - Inbound adapter: an internal scheduler scanning
last_changed_at. - Main flow: per idle cart,
Cart.AbandonInactive(now):OPEN → ABANDONED; save (version-checked), outboxCartAbandoned(carryinglastChangedAt), one transaction. - Errors: none reach a caller.
- Events emitted:
CartAbandoned(analytics). - Idempotency: the scan selects
OPENcarts only, so a second sweep finds nothing; the model's size estimate says abandoned carts are purged afterwards, but names no purge (hotspot H5).
CloseOnCheckoutCompleted (Kafka)
- Actor / trigger:
checkoutcompleted the purchase. - Inbound adapter: Kafka consumer on
retailnetwork.checkout.events, typecom.retailnetwork.storefront.checkout.checkoutsession.CheckoutCompleted, consumer groupshopping-cart.checkout. - Main flow:
- Inbox check on the CloudEvents
id; a duplicate is acknowledged. - Load
Cartby the event'scartId(the upstream key issessionId, so the cart never relies on the partition for order). Cart.CloseOnCheckoutCompleted(sessionId, now):OPEN → CHECKED_OUT; outboxCartCheckedOut(carryingcheckoutSessionId), inbox row, one transaction.- A cart that is not
OPENhas no transition: the inbox row is still written and the case is logged as an anomaly (ErrCartClosed).
- Inbox check on the CloudEvents
- Errors: none reach a caller.
- Events emitted:
CartCheckedOut(analytics). - Idempotency: inbox on the CloudEvents
id.
Project PurchasabilityView and EstimateView (Kafka)
- Actor / trigger:
offer-availabilityandpricingevents. - Inbound adapter: Kafka consumers on
retailnetwork.offer-availability.events(CatalogItemCreated,CatalogItemDiscontinued; groupshopping-cart.offer-availability) andretailnetwork.pricing.events(ItemPriceChanged,ItemPriceWithdrawn,DeliveryChargeScheduleChanged,TaxTableChanged; groupshopping-cart.pricing). - Main flow: inbox check; upsert
rm_purchasability/rm_item_prices, or replacerm_current_schedule/rm_current_tax_tablewith the version they carry; inbox row, one transaction. TheCartaggregate is never touched. - Errors: none reach a caller.
- Events emitted: none.
- Idempotency: inbox on the CloudEvents
id; per-item order comes from the producers'catalogItemIdkeying, and the schedule and table events carry the versions the views store.