Skip to main content

Use cases

There is one struct per use case in internal/application/usecases. Each depends only on the domain and on internal/application/ports. Writes run inside a UnitOfWork when Postgres is configured (ADR 0010). Without Postgres (UnitOfWork == nil) the same steps run one after another.

Use caseTriggerWrites?Events raised
DefineStandardREST POST /standardsyesLaborStandardDefined or LaborStandardRevised
RecordTaskPerformanceKafka TaskCompleted on warehouse.fulfillment.eventsyesTaskPerformanceRecorded
GetStandardREST GET /standards/{taskType}, MCP get_labor_standardno—
GetAssociateScorecardREST GET /associates/{associateId}/scorecard, MCP get_associate_scorecard, MCP resource scorecard://labor/{associateId}no—
GetTaskTypePerformanceREST GET /task-types/{taskType}/performance, MCP get_task_type_performanceno—
GetUtilizationREST GET /task-types/{taskType}/utilization, GET /associates/{associateId}/utilization, MCP get_task_type_utilizationno—

No use case is triggered by a scheduler. The housekeeping sweeper and the outbox relay are adapter-level jobs, not use cases (Runbook). The analytics projection (cmd/labor-projector) is an adapter over internal/analytics/report, also not an application use case.

DefineStandard​

internal/application/usecases/define_standard.go

  • Trigger: POST /standards on cmd/labor. Never exposed over MCP.
  • Inputs: taskType (PICK, PACK or SLAM, validated by shared.NewTaskType in the HTTP adapter, 400 otherwise), expectedSeconds, an optional travelComponentSeconds, an optional handlingUnit (EACH, CASE, PALLET) and, for CASE only, an optional teamLift {thresholdG, expectedSeconds} (ADR 0035), and an optional siteCode (1-32 characters, no whitespace, validated by shared.NewSiteCode, 400 invalid-site-code otherwise; absent = every site, ADR 0038). With a database, the Idempotency-Key header is required (ADR 0016).
  • Steps:
    1. Load the currently active standard for the scope: task type, handling unit and site (the task-type-only, site-less standard when neither is given).
    2. Mint a new StandardId.
    3. standard.NewForScope checks the invariants: expectedSeconds > 0, travelComponentSeconds >= 0, travel ≤ expected, and a teamLift only on CASE with a positive threshold and positive seconds. A violation returns 422 and records labor_performance.standards.defined{outcome="rejected"}.
    4. Atomically, close the prior standard of the same scope at now (Supersede; it is never overwritten), save the new one, and publish the event.
    5. Record outcome="accepted".
  • Concurrency invariants: the version column gives optimistic locking on Save (409 concurrent-modification), and the unique partial index allows one open standard per task_type, handling_unit and site_code (409 standard-conflict) (ADR 0022).
  • Events: LaborStandardDefined when no standard of the scope was active, otherwise LaborStandardRevised (carrying previous_expected_seconds). Both go to warehouse.labor-performance.analytics and, since ADR 0038, warehouse.labor-performance.events (key TaskType, the same data), with site_code only for a site-scoped standard.

RecordTaskPerformance​

internal/application/usecases/record_task_performance.go

  • Trigger: the Kafka consumer in cmd/labor, for com.warehouse.wes.fulfillment-execution.task.TaskCompleted only. There is deliberately no REST endpoint (ADR 0003).
  • Inputs: KafkaEventId (CloudEvents id), TaskId, AssociateId (may be empty), TaskType (ParseTaskTypeLenient: an unknown or absent value becomes ""), ActualSeconds (duration_seconds), and CompletedAt (CloudEvents time).
  • Steps, all in one unit of work:
    1. MarkProcessed(KafkaEventId). An id that was already processed returns (nil, nil), a silent no-op.
    2. Resolve the standard active as of CompletedAt (FindActiveAsOf), not the current one. If there is none, or the task type is unclassified, the standard is 0 (ADR 0004). When the request carries a handling unit, the standard for exactly (task type, unit) is tried first and the task-type-only standard is the fallback (most specific wins); a CASE standard with a team lift whose threshold is strictly below the declared WeightG scores the completion against the team-lift seconds (ADR 0035). Only site-less standards are ever resolved: a site-scoped standard never changes a score (ADR 0038).
    3. performance.NewWithHandling requires a non-empty event id and task id. EfficiencyPct = 100 × standard / actual, or nil when either is ≤ 0. The value is frozen for good.
    4. Look up the associate's previous completion (skipped for an empty associate), then save the TaskPerformance.
    5. Derive the idle gap: claimedAt = CompletedAt − ActualSeconds. The gap runs from the previous completion to claimedAt, capped at IDLE_GAP_CAP_SECONDS (default 3600, capped=true when hit), and is saved as an IdlePeriod. A gap ≤ 0 (out-of-order delivery) is logged and skipped, never an error (ADR 0014).
    6. Publish TaskPerformanceRecorded.
  • Failure: any error rolls everything back, including the processed_events claim, so the consumer's retry or the DLQ re-drive can process the event again.
  • Events: TaskPerformanceRecorded, to warehouse.labor-performance.analytics (key TaskType) and warehouse.labor-performance.events (key AssociateId) (ADR 0013).

GetStandard​

internal/application/usecases/get_standard.go

  • Inputs: taskType, and over REST an optional handlingUnit and an optional siteCode query.
  • Returns: the currently open LaborStandard, or ErrStandardNotFound (404 standard-not-found, or an MCP tool error). With a handling unit, ExecuteResolved returns the unit's own standard, else the task-type-only one (the MCP tool never passes a unit). Without a site the answer is always site-less; with one, ExecuteForSite returns exactly that site's standard and never falls back to the site-less one (ADR 0038).

GetAssociateScorecard​

internal/application/usecases/get_associate_scorecard.go

  • Inputs: associateId.
  • Rules: an empty id, or an associate with zero rows, returns ErrAssociateNotFound (404). An associate whose rows are all unscored gets a scorecard with meanEfficiencyPct: null, not a 404.
  • Computes: task count, mean efficiency over scored rows, a per-TaskType breakdown, and, from the 10 most recent rows, trend (ClassifyTrend: the recent mean against the all-time mean, ±5 percentage points, needs at least 3 scored tasks, otherwise INSUFFICIENT_DATA) and coachingFlag (DetectCoachingFlag: the last 3 scored tasks are all below 85%) (ADR 0005).

GetTaskTypePerformance​

internal/application/usecases/get_task_type_performance.go

  • Inputs: taskType (must be a known type).
  • Returns: always succeeds. taskCount, meanEfficiencyPct (scored rows only) and meanActualSeconds (measured rows, independent of any standard, ADR 0006). A type that has never been seen returns count 0 and null means.

GetUtilization​

internal/application/usecases/get_utilization.go

  • Inputs: a task type or an associate id, and a window. REST ?window= takes a Go duration and MCP windowSeconds takes seconds. Absent, unparsable or ≤ 0 means 1h.
  • ForTaskType: sums ActualSeconds and recorded (closed) idle gaps for the task type since now − window, and counts distinct associates. openGapSeconds is always 0.
  • ForAssociate: the same sums for one associate, plus the open gap: the time from the associate's last activity (last completion or last idle-gap end) to now, clamped to the window. It is computed at read time and never persisted.
  • Returns: utilizationPct = 100 × task / (task + idle + openGap), or null when there is nothing to divide.

Sequence: define, then score​

Source: internal/application/usecases/define_standard.go, internal/application/usecases/record_task_performance.go.