Skip to main content

Aggregate Design Canvas

Built on develop, not deployed

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.

Following the ddd-crew Aggregate Design Canvas v1.1. The model has one aggregate root in this context, Cart, and three read models, CheckoutReadiness, PurchasabilityView and EstimateView. Go packages as built: internal/domain/cart, internal/domain/estimate (plus shared).

Cart​

1. Name​

Cart (cart.Cart), identified by cartId.

2. Description​

One shopper's cart: optional shopperId (guest-first, D4), lines (catalogItemId, quantity), destinationPostalPrefix, the delivery quote (groups with their lines and offers — offerId, tier, window, expiresAt — plus the tiers that were unavailable), the selections (groupNo to offerId) and version. Value objects:

Value objectFieldsNote
CartLinecatalogItemId, quantityOne line per item; quantity in 1..99.
QuotedOfferofferId, groupNo, tier, deliverByEarliest, deliverByLatest, expiresAtOne offer held by the quote; a frozen delivery-promise fact.
DeliverySelectiongroupNo, offerIdThe shopper's pick per shipment group.

The quote itself (groups, their lines, the unavailable tiers) is held inside the aggregate as cart.DeliveryQuote; it holds no capacity, and every mutating command on lines or destination clears it together with the selections.

3. State Transitions​

Derived from: model.yaml → contexts[shopping-cart].aggregates[Cart].states, .transitions, .commands, .policies.

The entry arrow [*] --> OPEN is the creating command CreateCart. All three terminal states are entered from OPEN only, and ErrCartClosed rejects every later change: a cart is never reopened, and a MERGED cart leaves its lines in the target. A CheckoutCompleted for a cart that is not OPEN has no transition — the race needs a checkout session to outlive 30 days of cart idleness, which the session's own expiry makes impossible in practice.

4. Enforced Invariants​

InvariantEnforced by
One line per item; adding an existing item merges quantitiesAddLine merges quantities into the existing line; no error exists for it, nothing is ever rejected as a duplicate
Quantity in 1..99AddLine, SetLineQuantity → ErrQuantityOutOfRange
At most 100 lines (customer-ordering A15); quantities merged by MergeInto clamp at 99AddLine, MergeInto → ErrTooManyLines
The item is purchasable in the local viewsAddLine → ErrItemNotPurchasable (against PurchasabilityView)
A delivery quote needs a destination postal prefixQuoteDelivery → ErrNoDestination
A selection names an offer of the current quote; any line or destination change clears quote and selectionsSelectDelivery and every line/destination mutation → ErrStaleDeliverySelection
No change to a CHECKED_OUT, ABANDONED or MERGED cartall commands → ErrCartClosed
A merge target is another OPEN cart that has a shopper, never the cart itself; the source is a guest cart or belongs to the same shopperMergeInto → ErrMergeTarget

Derived from: model.yaml → contexts[shopping-cart].aggregates[Cart].invariants.

5. Corrective Policies​

PolicyOwnerRuleEffect on this aggregate
AbandonInactivethis contextAn OPEN cart with no change for 30 days is ABANDONED.OPEN → ABANDONED, CartAbandoned.
CloseOnCheckoutCompletedthis contextOn CheckoutCompleted the cart is CHECKED_OUT.OPEN → CHECKED_OUT, CartCheckedOut.
Quote clearingthis contextAny line or destination change clears quote and selections (business decision 3).No state change; the held quote and selections are dropped.
PurgeAbandonedCartsthis contextScheduled: carts idle for 30 days (ABANDONED, and closed carts past the same idle age) are deleted.The row and its children are deleted; no event.
Idempotent consumptionthis contextEvery consumed event is applied once (inbox on the CloudEvents id).A repeated CheckoutCompleted is acknowledged without effect.

6. Handled Commands​

CommandTriggerTransition
CreateCartShopper via BFF, REST POST /carts[*] → OPEN
AddLineShopper via BFF, REST POST /carts/{cartId}/linesnone (adds or merges a line; clears quote and selections)
SetLineQuantityShopper via BFF, REST PUT /carts/{cartId}/lines/{catalogItemId}none (sets a quantity; clears quote and selections)
RemoveLineShopper via BFF, REST DELETE /carts/{cartId}/lines/{catalogItemId}none (removes a line; clears quote and selections)
SetDestinationShopper via BFF, REST PUT /carts/{cartId}/destinationnone (sets the prefix; clears quote and selections)
QuoteDeliveryShopper via BFF, REST POST /carts/{cartId}/delivery-quotes (calls delivery-promise QuoteDeliveryOffers)none (holds the quote)
SelectDeliveryShopper via BFF, REST PUT /carts/{cartId}/delivery-selectionnone (records a selection)
MergeIntoShopper via BFF, REST POST /carts/{cartId}/mergesOPEN → MERGED (this cart); the target cart takes the lines
AcceptRequotedOffersShopper via BFF, REST POST /carts/{cartId}/delivery-requote-acceptancesOPEN → OPEN (writes the selected delivery from the supplied offers[] without calling delivery-promise; checkout re-verifies via its pre-authorize check)
AbandonInactivethe AbandonInactive policy (clock, 30 days)OPEN → ABANDONED
CloseOnCheckoutCompletedthe CloseOnCheckoutCompleted policy on Kafka CheckoutCompletedOPEN → CHECKED_OUT

7. Created Events​

EventRaised whenFull CloudEvents type
CartLineAddedAddLine is saved (merged quantities included)com.retailnetwork.storefront.shopping-cart.cart.CartLineAdded
CartDeliveryQuotedQuoteDelivery is saved with the held quotecom.retailnetwork.storefront.shopping-cart.cart.CartDeliveryQuoted
CartAbandonedAbandonInactive is savedcom.retailnetwork.storefront.shopping-cart.cart.CartAbandoned
CartCheckedOutCloseOnCheckoutCompleted is savedcom.retailnetwork.storefront.shopping-cart.cart.CartCheckedOut

All four are analytics-only (topic retailnetwork.shopping-cart.analytics); no cart consumer exists by design, because checkout reads the cart synchronously. Only AddLine publishes, by design: analytics needs add, quote, abandon and checkout, so SetLineQuantity, RemoveLine, SetDestination, SelectDelivery, AcceptRequotedOffers, MergeInto, CreateCart and PurgeAbandonedCarts raise nothing. Events leave through the transactional outbox in the same transaction as the save.

8. Throughput​

Estimate. The model says "medium-high: every add-to-cart". Every shopper-driven mutation lands on one cartId; contention is per cart, so one shopper's rapid edits are the hot path (the version column absorbs them).

9. Size​

Estimate. The model says "one row per active cart; closed carts idle for 30 days are purged by PurgeAbandonedCarts". One root row with its lines, its quote's groups and offers and its selections — a handful of child rows per cart, bounded at 100 lines.

Read models​

Read modelSource eventsServesWhy it is not an aggregate
CheckoutReadinessnone — derived from the cart itself: every group selected, no offer expiredGetCart responses; checkout at session open (ErrCartNotCheckoutReady mirrors it)It protects no invariant of its own; it is a fact computed from the aggregate plus the clock.
PurchasabilityViewoffer-availability CatalogItemCreated, CatalogItemDiscontinued; pricing ItemPriceChanged, ItemPriceWithdrawnErrItemNotPurchasable inside AddLineA projection of two other contexts' facts, rebuildable by replaying the topics; it takes no command.
EstimateViewpricing ItemPriceChanged, DeliveryChargeScheduleChanged, TaxTableChanged (effective prices, tier charges, tax bands)The labelled, non-binding estimate rendered on every cart readA projection of pricing's data; the estimate is derived at read time and stores no total.

Derived from: model.yaml → contexts[shopping-cart].read_models. The views keep exactly what the estimate and the purchasability check need. PurchasabilityView is deliberately narrower than product-discovery's four-input Purchasable verdict (the cart accepts any catalogued, priced item; checkout's quote is the gate), and EstimateView with no destination shows the item subtotal only (estimateComplete=false), and EstimateView keeps pricing's current versions so the estimate always names the schedule and tax table it was computed from.