Skip to content

feat(shifts): add fatigue monitoring for guard approval#349

Open
LoopyB wants to merge 4 commits intomainfrom
louisabest/feature/shift-fatigue-monitoring
Open

feat(shifts): add fatigue monitoring for guard approval#349
LoopyB wants to merge 4 commits intomainfrom
louisabest/feature/shift-fatigue-monitoring

Conversation

@LoopyB
Copy link
Copy Markdown
Collaborator

@LoopyB LoopyB commented May 7, 2026

Summary

Adds shift fatigue monitoring to detect overworking patterns before a guard is approved for a shift.

This implements a reusable fatigue monitoring service that calculates guard workload risk using:

  • shifts per week
  • hours per day
  • hours per week

If the selected guard exceeds the configured fatigue limits, shift approval is blocked and the API returns a structured fatigue assessment containing a fatigue score, warnings, and workload metrics.

Ticket Requirement Coverage

Shift Fatigue Monitoring

This PR addresses the ticket requirement to detect overworking patterns and enforce guard fatigue rules.

Implemented logic includes:

  • calculating total assigned/completed shifts for the guard within the target week
  • calculating total guard hours for the target day
  • calculating total guard hours for the target week
  • calculating a fatigueScore
  • returning fatigue warnings
  • blocking shift approval when fatigue rules are exceeded

Integration

The fatigue check is integrated into the existing approveShift flow before the guard is assigned to the shift.

Current flow:

  1. Validate shift ID and guard ID
  2. Confirm shift exists
  3. Confirm employer/admin permission
  4. Confirm shift is not already assigned/completed
  5. Confirm shift has not started
  6. Confirm guard applied for the shift
  7. Assess guard fatigue
  8. Block approval if fatigue rules are exceeded
  9. Otherwise approve guard as normal

Architecture Note

The fatigue rules are implemented in a dedicated service:

src/services/fatigue.service.js
ESLintcheck+unitTestsPassed

@uppalkrish
Copy link
Copy Markdown
Collaborator

please upload swagger screenshot

@uppalkrish
Copy link
Copy Markdown
Collaborator

swagger doc missing

@LoopyB
Copy link
Copy Markdown
Collaborator Author

LoopyB commented May 11, 2026

Updated the Swagger documentation and uploaded the screenshot.

The Swagger docs now show the fatigue monitoring response for PUT /api/v1/shifts/{id}/approve, including:

  • fatigueAssessment
  • fatigueScore
  • warnings
  • isFatigued
  • workload metrics

I also added fatigue enforcement to the shift creation path for pre-selected guardIds, so the implementation now covers both ticket integration points:

  • shift approval
  • shift creation when guards are pre-selected

I reran targeted ESLint and the fatigue unit tests after the updates:

  • npx eslint src/routes/shift.routes.js
  • npx eslint src/controllers/shift.controller.js
  • npx eslint src/services/fatigue.service.js
  • npx eslint src/middleware/logger.js
  • npx eslint src/tests/services/fatigue.service.test.js
  • npx jest src/tests/services/fatigue.service.test.js

Result: fatigue service tests passed 12/12.

swagger

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants