Skip to main content

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 caseTriggerAggregateEvents raised
StartAssociateShiftPOST /associates/{id}/shift-startsAssociateShiftassociate.AssociateShiftStarted
CertifyAssociatePOST /associates/{id}/certificationsAssociateShiftassociate.AssociateCertified
StartBreakPOST /associates/{id}/break-startsAssociateShiftassociate.AssociateBreakStarted
EndBreakPOST /associates/{id}/break-endsAssociateShiftassociate.AssociateBreakEnded
AssignLaborPOST /associates/{id}/assignments (Idempotency-Key); MCP assign_laborLaborAssignment (+ AssociateShift hours)assignment.LaborAssigned, plus assignment.LaborReassigned when moving paths
EndAssociateShiftPOST /associates/{id}/shift-endsAssociateShift (+ LaborAssignment)associate.AssociateShiftEnded
ProposePathPlanPOST /paths/{pathId}/plan-proposals; MCP propose_path_headsnone (pure computation)shiftplan.ShiftPlanProposed
CommitShiftPlanPOST /shift-plans (Idempotency-Key)ShiftPlanshiftplan.ShiftPlanCommitted
GetStaffingGapGET /paths/{pathId}/staffing-gap, GET /sites/{siteCode}/shifts/{shiftId}/staffing-gap, deprecated GET /buildings/{buildingId}/shifts/{shiftId}/staffing-gap; MCP get_staffing_gapShiftPlan (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 version so 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. StartBreak rejects an associate already on break (ErrAlreadyOnBreak). EndBreak rejects 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_SHIFT and the process-path catalogue.
  • Invariants, in order:
    1. The associate exists.
    2. CanBeAssigned: the shift has not ended (ErrShiftEnded) and the associate is not on break (ErrOnBreak).
    3. Certification gate (ADR 0003, ADR 0009): the associate must hold the certification named after the path's catalogue family (MatchPrefix, so PICK, pick and pick-zone-a all require pick). Without a catalogue, the raw path id is the certification name (ErrCertificationRequired).
    4. 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.
    5. The closed interval's hours must fit under MAX_HOURS_PER_SHIFT (ErrMaxHoursExceeded).
  • Persists: the LaborAssignment, plus the AssociateShift when hours were logged. Both are saved in one unit of work.
  • Raises: LaborAssigned, or LaborReassigned (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 carries fromPathId == toPathId (LaborAssignment.AssignBy has no same-path check).
  • Metrics: every attempt adds one to workforce.labor_assignments with outcome and, on rejection, reason (Observability).
  • REST only: RequireIdempotencyKey guards the route (ADR 0027).

Shift planning​

ProposePathPlan​

  • Inputs: building/site id, path id, charge (units to clear), optional plannedRate (units per head per hour).
  • Behaviour (ADR 0012, ADR 0020):
    • When plannedRate is at most 0 and a MeasuredRateClient is wired, the rate becomes 3600 / meanActualSeconds from labor-performance and rateSource is measured. Otherwise rateSource is caller. 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 IdleShareClient is wired (only in LABOR_PERFORMANCE_MODE=kafka-cache) and the idle share is above IDLE_SHARE_TRIM_THRESHOLD (0.30), heads are trimmed to ceil(heads × (1 − share)), never below 1. The response then carries a trimReason.
  • Persists: nothing. A human still has to commit the plan.
  • Raises: ShiftPlanProposed (building, path, heads, resolved rate).
  • MCP difference: cmd/mcp wires this use case with no measured-rate or idle-share client, so propose_path_heads always uses the caller's rate and never trims.

CommitShiftPlan​

  • Inputs: building/site id, shift id, plan lines (path, planned heads, planned rate, planned hours), installedStations per path (supplied by the caller). Uses MAX_HOURS_PER_SHIFT.
  • Invariants:
    1. A catalogue is wired (ErrCommitShiftPlanNoCatalogue otherwise).
    2. Every line's path is in the catalogue (unknown-path-id).
    3. 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. Any ErrInstalledCapacityUnavailable fails the whole commit with 503. In permissive mode, every commit fails.
    4. 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).
  • Persists: the ShiftPlan. The integration publisher re-reads the saved plan while encoding, so Publish has 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 optional siteCode scopes 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: plannedHeads comes from the committed ShiftPlan, activeHeads from CountActiveByPath or CountActiveByPathAtSite, and understaffed = activeHeads < plannedHeads. observedIdlePct is 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 neither siteCode nor buildingId the read is 400 missing-building-id. When both are sent, buildingId is the plan key and siteCode the scope; a differing pair is accepted, not a 422 conflicting-site-and-building (ADR 0035, "Decided (2026-10-08)"; shared.ResolveGapLookup). The 422 rule applies to CommitShiftPlan and ProposePathPlan.
  • 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​

PortImplemented by
AssociateRepo, ShiftPlanRepo, AssignmentRepointernal/adapters/outbound/postgres (every binary); internal/adapters/outbound/memory only as a test double
EventPublisherpostgres.OutboxPublisher, relayed to Kafka (ADR 0038: no log or direct publisher); events.* only as test doubles
UnitOfWorkpostgres.UnitOfWork. When nil, saves and publish run back to back.
PathCataloguekafkacatalog
MeasuredRateClient, IdleShareClientlaborperformancecache (kafka-cache, both ports, the default); laborperformance (HTTP + breaker, the REST rollback)
InstalledCapacityClientfulfillmentexecution (permissive fail-loud, or HTTP + breaker)
LaborMetricstelemetry.LaborMetrics
Clockclock.System