Application use cases
The application layer (internal/application/usecases) has nine use
cases. Each is one struct that depends only on domain packages and
internal/application/ports. Every use case that publishes wraps its saves
and its Publish in the shared UnitOfWork (atomically(...)), so with
Postgres and EVENT_PUBLISHER=kafka the state change and its outbox_events
rows commit together (ADR 0016).
None is triggered by Kafka or a scheduler. The only inbound Kafka consumer
(the analytics projector) writes the read model and calls no use case. The
triggers are REST (cmd/workforce) and MCP (cmd/mcp).
All event types share the prefix com.warehouse.wes.workforce-management.
(internal/adapters/kafka/cloudevents/types.go). Each event goes to both
warehouse.workforce.events and warehouse.workforce.analytics (see
Events).
Summary
| Use case | Trigger | Aggregate | Events raised |
|---|---|---|---|
StartAssociateShift | POST /associates/{id}/shift-starts | AssociateShift | associate.AssociateShiftStarted |
CertifyAssociate | POST /associates/{id}/certifications | AssociateShift | associate.AssociateCertified |
StartBreak | POST /associates/{id}/break-starts | AssociateShift | associate.AssociateBreakStarted |
EndBreak | POST /associates/{id}/break-ends | AssociateShift | associate.AssociateBreakEnded |
AssignLabor | POST /associates/{id}/assignments (Idempotency-Key); MCP assign_labor | LaborAssignment (+ AssociateShift hours) | assignment.LaborAssigned, plus assignment.LaborReassigned when moving paths |
EndAssociateShift | POST /associates/{id}/shift-ends | AssociateShift (+ LaborAssignment) | associate.AssociateShiftEnded |
ProposePathPlan | POST /paths/{pathId}/plan-proposals; MCP propose_path_heads | none (pure computation) | shiftplan.ShiftPlanProposed |
CommitShiftPlan | POST /shift-plans (Idempotency-Key) | ShiftPlan | shiftplan.ShiftPlanCommitted |
GetStaffingGap | GET /paths/{pathId}/staffing-gap, GET /sites/{siteCode}/shifts/{shiftId}/staffing-gap, deprecated GET /buildings/{buildingId}/shifts/{shiftId}/staffing-gap; MCP get_staffing_gap | ShiftPlan (read) | shiftplan.PathUnderstaffed, once per understaffed path |
The fourth MCP tool, get_workforce_labor_report, calls no use case. It is a
REST client of cmd/workforce-reports (see
Runbook).
HTTP status codes for the errors below are listed in Errors
and Troubleshooting.
Associate shift lifecycle
StartAssociateShift
- Inputs: associate id, the list of certifications, an optional
siteCode(ExecuteAtSite, ADR 0034). The site code is accepted as given. Nothing checks it against facility-layout. - Behaviour: idempotent by associate id. A second call replaces the roster entry (certifications and site), carrying over the stored row's
versionso the version-guarded save (ADR 0021) accepts it. - Invariants: non-empty associate id. Any repository error other than "not found" fails the call.
- Raises:
AssociateShiftStarted.
CertifyAssociate
- Inputs: associate id, certification (non-empty).
- Invariants: the associate exists (
resource-not-found) and the shift has not ended (ErrShiftEnded). - Raises:
AssociateCertified.
StartBreak / EndBreak
- Inputs: associate id.
- Invariants: the shift has not ended.
StartBreakrejects an associate already on break (ErrAlreadyOnBreak).EndBreakrejects one not on break (ErrNotOnBreak). - Raises:
AssociateBreakStarted/AssociateBreakEnded.
EndAssociateShift
- Inputs: associate id. Uses
MAX_HOURS_PER_SHIFT. - Behaviour: if the associate has an active assignment, it is ended first and its hours are logged against the shift. Then the shift is closed. Ending an already-ended shift is a no-op that raises nothing.
- Invariants: the associate exists. Logging the closed assignment's hours must not exceed
MAX_HOURS_PER_SHIFT(ErrMaxHoursExceeded). - Raises:
AssociateShiftEnded. Closing the assignment raises no assignment event.
Labor assignment
AssignLabor
- Inputs: associate id, path id. Uses
MAX_HOURS_PER_SHIFTand the process-path catalogue. - Invariants, in order:
- The associate exists.
CanBeAssigned: the shift has not ended (ErrShiftEnded) and the associate is not on break (ErrOnBreak).- Certification gate (ADR 0003, ADR 0009): the associate must hold the certification named after the path's catalogue family (
MatchPrefix, soPICK,pickandpick-zone-aall requirepick). Without a catalogue, the raw path id is the certification name (ErrCertificationRequired). - Single active assignment: an existing active assignment is closed and moved into history. The associate is never double-booked, and a second call is never rejected for it.
- The closed interval's hours must fit under
MAX_HOURS_PER_SHIFT(ErrMaxHoursExceeded).
- Persists: the
LaborAssignment, plus theAssociateShiftwhen hours were logged. Both are saved in one unit of work. - Raises:
LaborAssigned, orLaborReassigned(from path → to path) whenever an active assignment was closed to open the new one. That includes assigning the path the associate is already on: the interval is closed and reopened and the event carriesfromPathId == toPathId(LaborAssignment.AssignByhas no same-path check). - Metrics: every attempt adds one to
workforce.labor_assignmentswithoutcomeand, on rejection,reason(Observability). - REST only:
RequireIdempotencyKeyguards the route (ADR 0027).
Shift planning
ProposePathPlan
- Inputs: building/site id, path id,
charge(units to clear), optionalplannedRate(units per head per hour). - Behaviour (ADR 0012, ADR 0020):
- When
plannedRateis at most 0 and aMeasuredRateClientis wired, the rate becomes3600 / meanActualSecondsfrom labor-performance andrateSourceismeasured. OtherwiserateSourceiscaller. When the measured rate is unavailable, the rate stays at 0 and 0 heads are proposed (fail-open). heads = shiftplan.ProposedHeads(charge, rate).- When an
IdleShareClientis wired (only inLABOR_PERFORMANCE_MODE=kafka-cache) and the idle share is aboveIDLE_SHARE_TRIM_THRESHOLD(0.30), heads are trimmed toceil(heads × (1 − share)), never below 1. The response then carries atrimReason.
- When
- Persists: nothing. A human still has to commit the plan.
- Raises:
ShiftPlanProposed(building, path, heads, resolved rate). - MCP difference:
cmd/mcpwires this use case with no measured-rate or idle-share client, sopropose_path_headsalways uses the caller's rate and never trims.
CommitShiftPlan
- Inputs: building/site id, shift id, plan lines (path, planned heads, planned rate, planned hours),
installedStationsper path (supplied by the caller). UsesMAX_HOURS_PER_SHIFT. - Invariants:
- A catalogue is wired (
ErrCommitShiftPlanNoCatalogueotherwise). - Every line's path is in the catalogue (
unknown-path-id). - Live installed-capacity ceiling (ADR 0014): for each path, the use case looks up the catalogue's
requiredCapabilities, asks fulfillment-execution how many stations have each capability (once per capability per commit), and takes the minimum. A path with no capabilities gets ceiling 0. AnyErrInstalledCapacityUnavailablefails the whole commit with 503. Inpermissivemode, every commit fails. - Domain checks in
shiftplan.CommitShiftPlan: at least one line (ErrNoPathPlans), installed stations given for every path (ErrMissingInstalledStations), planned heads within installed stations (ErrPlannedHeadsExceedInstalled), within the live ceiling (ErrExceedsInstalledCapacity), and planned hours within heads ×MAX_HOURS_PER_SHIFT(ErrPlannedHoursExceedCapacity).
- A catalogue is wired (
- Persists: the
ShiftPlan. The integration publisher re-reads the saved plan while encoding, soPublishhas to run inside the same unit of work. - Raises:
ShiftPlanCommitted. - REST only:
RequireIdempotencyKey. There is no MCP tool for commits.
GetStaffingGap
- Inputs: building/site id, shift id, and either one path (
Execute/ExecuteForSite) or every line of the plan (ExecuteAll/ExecuteAllForSite, ADR 0029). An optionalsiteCodescopes the active-head count to associates whose active shift is at that site (ADR 0034, ADR 0035). Without it, the count covers every site. - Computation per line:
plannedHeadscomes from the committedShiftPlan,activeHeadsfromCountActiveByPathorCountActiveByPathAtSite, andunderstaffed = activeHeads < plannedHeads.observedIdlePctis filled only when an idle-share client is wired. Otherwise it is null, never 0. - Invariants: a committed plan exists for building + shift (
resource-not-found). With neithersiteCodenorbuildingIdthe read is400 missing-building-id. When both are sent,buildingIdis the plan key andsiteCodethe scope; a differing pair is accepted, not a422 conflicting-site-and-building(ADR 0035, "Decided (2026-10-08)";shared.ResolveGapLookup). The422rule applies toCommitShiftPlanandProposePathPlan. - Raises:
PathUnderstaffed(path, planned, active, site) for each understaffed line. The list variants publish every event in one unit of work. Note that this read raises events: every query of an understaffed path publishes again.
Ports used
| Port | Implemented by |
|---|---|
AssociateRepo, ShiftPlanRepo, AssignmentRepo | internal/adapters/outbound/postgres (every binary); internal/adapters/outbound/memory only as a test double |
EventPublisher | postgres.OutboxPublisher, relayed to Kafka (ADR 0038: no log or direct publisher); events.* only as test doubles |
UnitOfWork | postgres.UnitOfWork. When nil, saves and publish run back to back. |
PathCatalogue | kafkacatalog |
MeasuredRateClient, IdleShareClient | laborperformancecache (kafka-cache, both ports, the default); laborperformance (HTTP + breaker, the REST rollback) |
InstalledCapacityClient | fulfillmentexecution (permissive fail-loud, or HTTP + breaker) |
LaborMetrics | telemetry.LaborMetrics |
Clock | clock.System |