Skip to main content

Use cases

Every use case in internal/application/usecases, with what triggers it, what it takes, what it checks and what it raises. Write use cases share one shape (writer.go): inside one ports.UnitOfWork, load the SlotPlan, apply the aggregate command, save it guarded by the loaded version, and insert the raised events into the outbox. Consumer use cases share another (consumers.go, Intake): claim (consumer, CloudEvents id) in processed_events and apply the effect in the same unit of work.

Commands (REST)​

Use caseTriggerInputsChecksRaises
GeneratePlanPOST /slot-plans (wrapped by the Idempotency-Key middleware on Postgres)siteId? (default DEMAND_SITE_ID, then DEFAULT_SITE_ID), lookbackDays? (default LOOKBACK_DAYS, then 28)site id is a valid token (ErrNoSite when none at all); lookback 1 to 365; window [now - days, now); the computed plan passes every SlotPlan invariant (one slot per SKU, one SKU per slot, moves and unassigned consistent with the assignments)SlotPlanGenerated
ApprovePlanPOST /slot-plans/{planId}/approvalsplanIdthe id starts with plan-; the plan exists; it is Draft (ErrNotDraft before anything is touched); the site's current Approved plan, if any, is superseded and saved first with its own version guard (a lost race is ErrApprovedPlanConflict); the partial unique index allows one Approved plan per siteSlotPlanApproved (with supersedes_plan_id, the full map and the moves)
RejectPlanPOST /slot-plans/{planId}/rejectionsplanId, reason?the plan exists and is Draft; the trimmed reason has at most 500 charactersSlotPlanRejected

How GeneratePlan computes a plan​

  1. DemandLedger.Velocity: per SKU, the ACTIVE demand lines of the site due inside the window (picks = lines, units = their sum).
  2. ProfileDirectory.Many: handling tags, temperature class and effective unit size of those SKUs.
  3. SlotCatalogue.ForwardSlots: active Storage slots in zones of the site whose code is in FORWARD_ZONE_CODES.
  4. SlotPlanRepository.Current: the site's Approved plan, as the previous map for stickiness.
  5. planning.Planner.Plan (policy abc-velocity-v1, pure, deterministic): candidates are SKUs with picks, ordered picks desc, units desc, SKU asc; ABC class from the share of picks before each SKU (A below 80 %, B below 95 %, else C); each SKU keeps its previous slot if still eligible and free, else takes the best free eligible slot (slots ranked by location code, LexicalRanking); moves are the diff against the previous map.
  6. slotplan.Generate creates the Draft at version 1; save and enqueue.

Queries (REST)​

Use caseTriggerInputsChecksResult
GetPlanGET /slot-plans/{planId}, MCP get_slot_planplanIdvalid id; existsthe plan plus its execution progress (GetPlan.View, ADR 0010): moves total and executed, last executed at, per move executed flag and quantity, derived from slot_plan_move_executions
ListPlansGET /slot-planslimit? (1 to 500, default 100), cursor?, siteId?, state?each parameter (invalid-limit, invalid-cursor, invalid-site-id, invalid-state)a page newest first (generated_at, then id, descending) and nextCursor while more remain
ListForwardSlotsGET /forward-slotssiteId? (default site)site resolvablethe Approved plan's {sku, slot, abcClass} list; an empty list (not an error) when the site has none
ListSkuVelocityGET /sku-velocitysiteId?, limit?, windowDays? (1 to 365, default 28)site, limit and window (an invalid window reports invalid-limit)the velocity of the window, truncated to limit

Consumers (Kafka)​

All seven return an Outcome (applied, duplicate, stale, ignored, and for RecordRelocation also unmatched) that the adapter logs. A payload that can never be applied is wrapped in ErrInvalidEvent before anything is claimed; the adapter skips it, except the StockRelocated consumer, which dead-letters it at once. None of them raises a domain event or writes to the outbox.

Use caseTrigger (type on topic)Claim nameChecksEffect
ApplyDemandChangedSiteSkuDemandChanged on warehouse.order-management.eventsslotting-demandsite id and SKU tokens; source_order_id and line_no >= 1; due_at present; state is ACTIVE or REMOVED; ACTIVE needs demanded_units >= 1; a site other than DEMAND_SITE_ID (when set) is ignoredupsert of the line, last writer wins; REMOVED keeps the row inactive
ApplyProductClassifiedProductClassified on warehouse.product-master.eventsslotting-productSKU token; temperature class empty, Ambient, Chilled or Frozen; version >= 1replaces tags and temperature class when version is newer, else stale
ApplyPhysicalProfileProductDimensionsDeclared or ProductMeasured on warehouse.product-master.eventsslotting-productSKU token; version >= 1; effective volume and weight at least 1 when presentreplaces the effective unit size (or clears it when effective_source is none) when version is newer, else stale
ApplyZoneRegisteredZoneRegistered on warehouse.facility.eventsslotting-layouttemperature class known; zoneId, siteCode, zoneCode presentupsert of the zone
ApplyLocationSlotRegisteredLocationSlotRegistered on warehouse.facility.eventsslotting-layoutlocation code token; zoneId present; capacities not negative; empty role means Storageupsert of the slot; a decommissioned slot stays decommissioned
ApplyLocationSlotDecommissionedLocationSlotDecommissioned on warehouse.facility.eventsslotting-layoutlocation code tokenactive = false for good, also for a slot never seen before
RecordRelocation (ADR 0010)StockRelocated on warehouse.inventory.eventsslotting-relocationrelocation_id, site_code, sku, from_bin, to_bin present; quantity >= 1; occurred_at present (else ErrInvalidEvent, dead-lettered)matches the move (source_ref = <plan_id>:<move_seq> of an Approved or Superseded plan of the site whose move there is for the SKU; else the site's Approved plan's move for the SKU whose bins fit its kind) and records it in slot_plan_move_executions once per relocation_id (applied, or duplicate for a replayed relocation); no match is unmatched with a reason, claimed and counted, never dead-lettered. Proposals are not gated on it.

Analytics read side (not in this package)​

The projector and the reports binary (ADR 0006) do not go through internal/application/usecases: their logic is the self-contained region internal/analytics/report, behind its own Projection and Reader ports.

OperationTriggerInputsChecksEffect / result
Project an event (Projection.Apply)a SlotPlanGenerated, SlotPlanApproved or SlotPlanRejected on warehouse.slotting-optimization.analytics (cmd/slotting-projector)CloudEvents id, time, subject; data.plan_id, data.site_id, data.supersedes_plan_id (approvals)plan_id equals subject; id, time, plan and site present; only an approval may supersede (Event.Validate, else ErrInvalidEvent: dead-lettered)claims the id in analytics_processed_events and upserts slot_plan_facts in one transaction; a replayed id changes nothing
Slot-plan funnel (Reader.FunnelDays)GET /reports/slot-plan-funnelfrom?, to? (RFC 3339), siteId?, granularity?range valid, not empty, at most 366 days (invalid-report-range); granularity day (invalid-report-granularity)one row per UTC day of the range, zeros included: plans generated, approved, rejected, approval latency (count, p50, p95 in seconds) and supersessions
Freshness (Reader.LastEventAt)GET /reports/freshnessnoneas_of (newest applied event time) and lag_seconds (never negative), both null before the first event

Not use cases​

  • Supersede is not triggered on its own: it happens inside ApprovePlan and raises no event.
  • There is no scheduler: plans are generated only on request.
  • The outbox relay and the HTTP idempotency middleware are adapters, not use cases.