Skip to main content

Use cases

The application layer is internal/application/usecases. Every use case is a method of Gateway, one per public route, over one OUT port (ports.Upstream). There are 23 use cases: 11 under the query budget (the reads, the two composed reads and the e-mail lookup of Login), 11 under the command budget and 1 under the confirm budget; none raises a domain event (the BFF has no aggregates and publishes nothing).

Common rules, applied by Gateway.call and Gateway.relay (internal/application/usecases/gateway.go):

  • Allowlist first. A call off the closed list is refused before a request is built (ErrRouteNotAllowed, which answers /problems/route-not-allowed with status 500 because it is a coding error, not a client error).
  • Budget per class. The whole composition runs under one context deadline: query 2 s, command 5 s, confirm 15 s (BUDGET_*_MS). Expiry is ErrUpstreamTimeout (504); no retry.
  • Idempotency. Every edge POST/PATCH requires the page's Idempotency-Key (400 idempotency-key-required without it); every upstream command of such a request gets Idempotency-Key = MintKey(route, subject, pageKey); queries carry none. The provider's Idempotency-Replayed: true is relayed and its 409 idempotency-key-reused passes through.
  • Pass-through. The upstream reply (status, content type, body) is relayed untouched, so an upstream 4xx/409/422 reaches the page as is. Only "no answer" becomes an edge error: ErrUpstreamUnavailable (503), a canceled context (499).
  • Ids (ValidID): 1 to 128 characters of [A-Za-z0-9._-] starting with an alphanumeric, so an id cannot alter the upstream path. A bad id is ErrInvalid (422 validation-failed).
  • Cookie. A use case that learns an id (new cart, shopper, checkout session) returns the new State and the server sets sf_state; an upstream failure never changes it.

Triggers are the public routes of apis/openapi.yaml; the upstream column uses the call the BFF actually makes (see Integration).

Catalogue​

#Use case (file)TriggerInputsInvariants checked at the edgeUpstream and classEvents
1SearchProducts (catalog.go)GET /productsqueryonly q, categoryId, inStock, priceMin, priceMax, sort, after, limit are forwarded, the rest is droppedproduct-discovery GET /products, querynone
2ListCategories (catalog.go)GET /categoriesnonenothing from the query is forwarded; the tree is relayed unchanged (ADR 0004)product-discovery GET /categories, querynone
3GetProduct (catalog.go)GET /products/{id}product idvalid idproduct-discovery GET /products/{id}, querynone
4DeliveryDates (catalog.go, dates.go)POST /products/{id}/delivery-date-quotesproduct id, {postalPrefix}strict body (no unknown field, no trailing data); postalPrefix 1 to 10 letters, digits, spaces or dashes; valid iddelivery-promise POST /delivery-quotes for one unit (required), then pricing GET /delivery-charge-schedules/current (optional), one query budget for bothnone
5CreateCart (carts.go)POST /cartsoptional bodyan empty body adopts the cookie's shopper ({"shopperId":...}) or {}; a body is forwarded as isshopping-cart POST /carts, command; stores cartId in the cookie if validnone
6GetCart (carts.go)GET /carts/{id}cart idvalid idshopping-cart GET /carts/{id}, querynone
7ListCarts (carts.go)GET /cartsoptional shopperId, stateshopper defaults to the cookie's; a valid shopper is required (validation-failed); only shopperId and state forwardedshopping-cart GET /carts, querynone
8AddLine (carts.go)POST /carts/{id}/linescart id, bodyvalid idshopping-cart POST /carts/{id}/lines, commandnone
9SetLineQuantity (carts.go)PATCH /carts/{id}/lines/{item}cart id, item id, bodyboth ids validshopping-cart PUT /carts/{id}/lines/{item}, commandnone
10RemoveLine (carts.go)DELETE /carts/{id}/lines/{item}cart id, item idboth ids validshopping-cart DELETE /carts/{id}/lines/{item}, commandnone
11SetDestination (carts.go)PUT /carts/{id}/destinationcart id, bodyvalid idshopping-cart PUT /carts/{id}/destination, commandnone
12QuoteDelivery (carts.go)POST /carts/{id}/delivery-quotescart idvalid id; the cart service calls delivery-promise itself, hence the command budgetshopping-cart POST /carts/{id}/delivery-quotes, commandnone
13SelectDelivery (carts.go)PUT /carts/{id}/delivery-selectioncart id, bodyvalid idshopping-cart PUT /carts/{id}/delivery-selection, commandnone
14OpenCheckout (checkout.go)POST /checkoutoptional {cartId?, shipToAddressId}strict body; shopper required in the cookie (session-required, 422); cartId defaults to the cookie's; cartId and shipToAddressId valid idscheckout POST /checkout-sessions with {cartId, shopperId, shipToAddressId}, command; stores sessionId as k in the cookienone
15GetCheckout (checkout.go)GET /checkout/{id}session idvalid id; the page polls it while a placement is in doubt (202)checkout GET /checkout-sessions/{id}, querynone
16ConfirmCheckout (checkout.go)POST /checkout/{id}/confirmationssession id, {paymentMethodToken}valid id; no card-shaped key (pan, cvc, expiry, ...); strict body; token 1 to 256 characters and not a Luhn-valid 13 to 19 digit numbercheckout POST /checkout-sessions/{id}/confirmations, confirm class (15 s)none
17ListOrders (orders.go)GET /ordersoptional shopperId, after, limitshopper defaults to the cookie's, else session-required (422)customer-ordering GET /orders, querynone
18GetOrder (orders.go)GET /orders/{id}order idvalid idcustomer-ordering GET /orders/{id} (required), /orders/{id}/tracking and the cookie's checkout session (both optional), one query budgetnone
19CancelOrder (orders.go)POST /orders/{id}/cancellationsorder id, {reason}valid id; strict body; reason 1 to 200 characterscustomer-ordering POST /orders/{id}/cancellations, commandnone
20RegisterShopper (shoppers.go)POST /shoppersbody (e-mail, optional addresses)body passed through untouched, never loggedcustomer-ordering POST /shoppers, command; stores shopperId in the cookienone
21Login (shoppers.go)POST /sessions{email}strict body; e-mail 1 to 254 characters; no password, no authenticationcustomer-ordering GET /shoppers?email=, query; stores shopperId in the cookienone
22Me (shoppers.go)GET /menoneshopper required in the cookie (session-required)customer-ordering GET /shoppers/{id}, querynone
23AddAddress (shoppers.go)POST /me/addressesaddress bodyshopper required in the cookie; address passed through, not stored or loggedcustomer-ordering POST /shoppers/{id}/addresses, commandnone

The two composed reads (4 and 18) use the query budget although 4 is a POST; use case 21 is a read that the page treats as a sign-in.

Domain operations behind them​

The invariants live in internal/domain/composition, not in the adapters.

OperationRule
Allowed(Call)A call is permitted only when (service, method, path) matches a template of the closed list; {} is one path segment that must itself be a valid id.
Budgets.For(Class)Query, Command or Confirm duration; an unknown class gets the command budget; non-positive budgets are rejected at config load.
MintKey(route, subject, clientKey)sf- plus the first 40 hex characters of SHA-256 over null-separated route, subject and the page's Idempotency-Key (1 to 128 visible ASCII characters, ValidClientKey). No window and no body digest: the provider fingerprints the body.
State.Encode / DecodeStateQuery-string cookie value with c, s, k; malformed input and ids failing ValidID decode to an empty field, never an error.
IsCardFieldName, LooksLikeCardNumberJSON keys only a raw card would carry; 13 to 19 digits passing the Luhn check, spaces and dashes ignored.

Errors and the problem types they become​

Mapped in internal/adapters/inbound/http/problems.go (writeError); the symptom-first view is Troubleshooting.

ErrorProblem type (status)
composition.ErrInvalid (detail is a fixed sentence)/problems/validation-failed (422)
composition.ErrNoSession/problems/session-required (422)
composition.ErrUpstreamTimeout/problems/gateway-timeout (504)
composition.ErrUpstreamUnavailable/problems/upstream-unavailable (503)
context.Canceled/problems/client-closed-request (499)
composition.ErrRouteNotAllowed/problems/route-not-allowed (500)
anything else/problems/internal-error (500)
malformed route or NUL in input (middleware)/problems/malformed-request (400), /problems/resource-not-found (404), method-not-allowed (405), /problems/payload-too-large (413)

Details never carry request or upstream data.

Where the code differs from the ADR text​

  • ADR 0002 A9 says tracing (otelhttp) "is not wired yet"; it is (internal/adapters/outbound/rest/client.go, and ADR 0003 adopts the fleet observability standard).
  • ErrRouteNotAllowed answers its own problem type, route-not-allowed, but keeps status 500: the allowlist guards the calls the BFF builds itself (ids are validated before they reach a path), so a refusal can only come from a code change that calls a route missing from the allowlist. A 403 or 404 would blame the client and hide the bug from the 5xx alerts.