Skip to main content

Application use cases

Every use case lives in internal/application/usecases and is wired once in the composition roots (cmd/facility/main.go newServer, and cmd/mcp/main.go for the read subset). No use case is Kafka-triggered: facility-layout consumes no topic in its OLTP path. One use case is timer-driven, PublishSiteCapabilityHeartbeats, run by the loop in internal/adapters/inbound/heartbeat/heartbeat.go (see Scheduled). The other background work (outbox relay, housekeeping sweeper, analytics projection) lives in adapters, not in use cases.

Writes run inside atomically(ctx, UnitOfWork, …). Each write saves the aggregate and enqueues its event(s) into outbox_events in one transaction (ADR-0018). The UnitOfWork field is nil only in unit tests on in-memory test doubles, where the two steps run back to back; no binary builds a use case that way (Postgres and Kafka are the only runtime, ADR 0039). Every event below is published to both warehouse.facility.events and warehouse.facility.analytics with the CloudEvents type com.warehouse.wms.facility-layout.<entity>.<EventName> (ADR-0024).

Commands (write use cases)​

Use caseTriggerInputsInvariants checked (→ problem slug)Event raised
RegisterSitePOST /sites (Idempotency-Key)siteCode, namevalid site code (invalid-site-code); unique code (duplicate-site-code)SiteRegistered (…site.SiteRegistered)
RegisterZonePOST /sites/{siteCode}/zones (Idempotency-Key)site code, area code, zone code, temperature class, hazmat, optional bayPitchM/levelPitchMsite exists (site-not-found) and is active (site-not-active); valid codes; known temperature class; pitches both-or-neither and > 0 (invalid-pitch); unique zone (duplicate-zone)ZoneRegistered
RegisterAislePOST /zones/{zoneId}/aisles (Idempotency-Key)zone id, aisle code, sequence hint, directionzone exists and is active; sequence hint ≥ 0; known direction; unique aisle in the zone (duplicate-aisle)AisleRegistered
RegisterLocationTypePOST /location-types (Idempotency-Key)name, role (default Storage), default capacityknown role (unknown-location-role); capacity required (> 0) for roles that require it (Storage, Staging, Drop, Consolidation), optional for Dock/Yard/WorkCenter/QC/Shipping (may be absent); unique name (duplicate-location-type)LocationTypeRegistered
DefinePlacementRulePOST /placement-rules (Idempotency-Key)rule id, location type, effect, zone predicatelocation type exists (location-type-not-found); known effect (unknown-placement-effect); predicate constrains something (empty-zone-predicate); unique id (duplicate-placement-rule)PlacementRuleDefined
RegisterLocationSlotPOST /locations (Idempotency-Key); also called once per row by ImportFacilityLayoutlocation code, location type, optional capacity override, dockFlow, activitieswell-formed code (malformed-location-code); site, zone and aisle exist and are active; location type exists; code unique (duplicate-location-code); placement rules evaluated at registration (placement-rule-violated, ADR-0003); Dock needs dockFlow, WorkCenter needs ≥ 1 activity, others none (ADR-0016)LocationSlotRegistered. Records facility.location_slot.registrations{outcome}.
DecommissionLocationSlotPOST /locations/{locationCode}/decommissionings (no Idempotency-Key)location codeslot exists (location-slot-not-found); not already decommissioned (already-decommissioned, one-way per ADR-0005); version check (concurrent-modification, ADR-0025)LocationSlotDecommissioned
ImportFacilityLayoutPOST /location-imports (no Idempotency-Key)rows of site/area/zone/aisle/bay/level/position plus location type, optional functional attributes, capacity and geometrynon-empty (empty-import). Each row is processed independently, not as one transaction: it creates missing site, zone and aisle parents (it never mutates existing ones; inactive parents reject the row) and each step (parent, slot, geometry) commits in its own unit of work, so a row refused at the slot step keeps the parents it created. Each row then runs RegisterLocationSlot and, if X/Y/Z are given, SetLocationGeometry. Partial success: one bad row does not abort the rest (ADR-0006).Per row: SiteRegistered/ZoneRegistered/AisleRegistered for new parents, LocationSlotRegistered, optionally LocationGeometryUpdated. Then exactly one FacilityLayoutImported with rowsSubmitted, slotsImported and rowsRejected.
SetLocationGeometryPUT /locations/{locationCode}/geometryposition (x, y, z), dimensions (w, d, h), optional pick sequenceslot exists; z ≥ 0 (invalid-z); dimensions > 0 (invalid-dimensions); pick sequence ≥ 0; slot version check (concurrent-modification)LocationGeometryUpdated
SetAisleGeometryPUT /zones/{zoneId}/aisles/{aisleCode}/geometrycentreline segmentaisle exists; both endpoints real (segment-endpoints-not-real); start ≠ end (segment-start-end-equal)AisleGeometryUpdated
RegisterFixedStructurePOST /sites/{siteCode}/structures (Idempotency-Key)id, site code, kind, footprint, labelsite exists; known kind (unknown-fixed-structure-kind); real footprint (empty-fixed-structure-footprint); unique idFixedStructureRegistered
RegisterCrossAislePOST /zones/{zoneId}/cross-aisles (Idempotency-Key)zone id, from aisle, to aisle, at bayzone exists; both aisles belong to the zone (cross-aisle-aisle-mismatch); distinct aisles (cross-aisle-same-aisle); unique (duplicate-cross-aisle)CrossAisleRegistered
ChangeSiteTransferCapabilityPUT /sites/{siteCode}/transfer-capability (no Idempotency-Key: a repeat is a no-op)site code, transferOriginEnabled, transferDestinationEnabled (both required, missing-transfer-capability-field)site exists (site-not-found); site active (site-not-active); site version check (concurrent-modification, ADR-0025 extended by ADR-0035). An identical snapshot after revision 1 saves and publishes nothing.SiteCapabilityChanged with the new per-site capability_revision (1, 2, 3, …)
ChangeSiteRolePUT /sites/{siteCode}/role (no Idempotency-Key: a repeat is a no-op)site code, siteRole (required; one of the 15 roles, never UNSPECIFIED: invalid-site-role)site exists (site-not-found); site active (site-not-active); site version check (concurrent-modification). The current role saves and publishes nothing; the transfer capability is never touched (ADR-0037).SiteRoleChanged (siteRole, previousSiteRole)
ListSiteRoleTransferDefaultsGET /site-roles/transfer-defaultsnoneread-only: the domain's role -> default transfer capability table (ADR-0037)none

SiteCapabilityChanged: raised by ChangeSiteTransferCapability​

com.warehouse.wms.facility-layout.site.SiteCapabilityChanged is a full last-write-wins snapshot of a site's transfer capability (ADR-0035). Its wire data fields are site_code, transfer_origin_enabled, transfer_destination_enabled and capability_revision. They stay snake_case, while every other event payload is camelCase: recasing them would be a breaking .v2 for network-inventory-planning (ADR-0035, decision D2). The site aggregate owns the revision: the first change publishes revision 1 and every later real change adds exactly one. The sites.version guard makes two concurrent changes of one site serialise, so two published snapshots never share a revision. Consumers keep the highest capability_revision. A periodic heartbeat (ADR-0041, use case PublishSiteCapabilityHeartbeats, every SITE_CAPABILITY_HEARTBEAT_INTERVAL, default 5m) re-publishes the current values of every active site with a declared capability with a fresh time and capability_revision + 1, so a consumer's freshness window holds while nothing changes; a PUT of the value a site already holds is still a no-op. The event is keyed and subjected by the site code and travels through the outbox like every other event; the analytics projector ignores it.

Scheduled (timer, not a route)​

Use caseTriggerInputsInvariants checkedEvent raised
PublishSiteCapabilityHeartbeatsthe heartbeat.Loop in cmd/facility (ADR-0041): one pass at boot, then a wake-up every SITE_CAPABILITY_HEARTBEAT_INTERVAL / 5 (interval default 5m, 0 disables it)none; reads the active sites with a declared capability whose last heartbeat is at least one interval old (at most 500 per pass)the site is still due and qualifying when loaded; site version check (concurrent-modification, a loser is counted as skipped, not an error); a failure on one site does not stop the othersSiteCapabilityChanged with the current values, a fresh time and capability_revision + 1, one per due site, through the outbox in the same transaction as the site row

Queries (read use cases)​

Use caseREST triggerMCP toolInputsNot-found →
ListSitesGET /siteslist_sites (each entry also carries siteRole (ADR-0037), transferOriginEnabled, transferDestinationEnabled, capabilityRevision)nonen/a
GetSiteGET /sites/{siteCode}, GET /sites/{siteCode}/transfer-capabilityn/asite codesite-not-found
GetSiteLayoutGET /sites/{siteCode}/layout (format=json or svg, otherwise invalid-layout-format)get_site_layoutsite codesite-not-found
ListLocationsByRoleGET /sites/{siteCode}/locations?role=list_functional_locationssite code, rolesite-not-found
ListZonesGET /sites/{siteCode}/zonesn/asite codesite-not-found
ListFixedStructuresGET /sites/{siteCode}/structuresn/asite codesite-not-found
GetZoneGET /zones/{zoneId}n/azone idzone-not-found
GetZoneGridGET /zones/{zoneId}/gridget_zone_gridzone idzone-not-found
ListAisles / GetAisleGET /zones/{zoneId}/aisles, GET /zones/{zoneId}/aisles/{aisleCode}n/azone id (+ aisle code)zone-not-found / aisle-not-found
ListCrossAislesGET /zones/{zoneId}/cross-aislesn/azone idzone-not-found
GetZoneTravelGraphGET /zones/{zoneId}/travel-graphget_zone_travel_graphzone idzone-not-found
EstimateTravelDistanceGET /distance?from=&to=estimate_travel_distancetwo location codeslocation-slot-not-found. Two locations in different zones return 422 no-route-between-zones, and an unreachable waypoint returns 422 no-route.
ListLocationTypes / GetLocationTypeGET /location-types, GET /location-types/{name}n/anamelocation-type-not-found
ListPlacementRules / GetPlacementRuleGET /placement-rules, GET /placement-rules/{ruleId}n/arule idplacement-rule-not-found
GetLocationSlotGET /locations/{locationCode}n/alocation codelocation-slot-not-found
GetLocationClassificationGET /locations/{locationCode}/classification (ADR-0008)n/alocation codelocation-slot-not-found

The MCP binary also registers get_facility_catalog_growth_report when REPORTS_BASE_URL is set. That tool is not an application use case: it calls the facility-reports REST API (GET /reports/catalog-growth). The MCP server wires only read use cases, so it cannot mutate the map.

Analytics projection (adapter, not a use case)​

facility-projector applies eight of the events above (SiteRegistered, ZoneRegistered, AisleRegistered, LocationTypeRegistered, PlacementRuleDefined, LocationSlotRegistered, LocationSlotDecommissioned, FacilityLayoutImported) to catalog_growth_rollup. It works directly from the inbound Kafka adapter through the report.ProjectionStore port (internal/adapters/inbound/kafka/analytics_consumer.go), idempotently per CloudEvents id. See the Runbook.