Skip to content

refactor(attendance): extract check-in/check-out logic into service layer#361

Open
LoopyB wants to merge 1 commit into
mainfrom
louisa/feature/attendance-service-refactor
Open

refactor(attendance): extract check-in/check-out logic into service layer#361
LoopyB wants to merge 1 commit into
mainfrom
louisa/feature/attendance-service-refactor

Conversation

@LoopyB
Copy link
Copy Markdown
Collaborator

@LoopyB LoopyB commented May 9, 2026

Summary

This PR refactors the attendance check-in/check-out workflow by extracting attendance business logic from shiftattendance.controller.js into a dedicated service layer.

The goal is to improve backend maintainability, testability, and future readiness for payroll, reporting, guard reliability metrics, and AI-assisted guard recommendation logic.

Changes

  • Added src/services/attendance.service.js
  • Moved attendance business rules out of the controller and into service functions
  • Added service-level helpers for:
    • attendance coordinate validation
    • distance calculation using the Haversine formula
    • assigned shift validation
    • shift radius verification
    • check-in handling
    • check-out handling
    • attendance history retrieval
    • payroll-ready attendance facts
  • Updated shiftattendance.controller.js so it now acts as a thin request/response layer
  • Removed duplicated controller-side attendance logic
  • Removed controller-side distance calculation logic
  • Removed debug logging from checkout
  • Preserved existing API response messages and status-code behaviour
  • Updated attendance controller test data so latitude/longitude are stored on Shift.location
  • Updated test setup to use the stored acceptedBy field rather than the virtual assignedGuard alias
  • Added focused service-level Jest tests for attendance service logic

Architectural impact

This supports the backend service-layer pattern by separating HTTP controller concerns from attendance domain logic.

Before this refactor, the attendance controller was responsible for request handling, coordinate validation, shift lookup, guard assignment validation, duplicate check-in prevention, location/radius checks, attendance persistence, and attendance history retrieval.

After this refactor, the controller delegates business logic to the attendance service and remains focused on:

  • reading request parameters/body
  • calling the appropriate service function
  • returning the HTTP response

This makes the attendance workflow easier to test, maintain, and extend.

Future readiness

This refactor creates a cleaner foundation for future backend features, including:

  • payroll calculation support using actual check-in/check-out records
  • attendance reporting
  • guard reliability scoring
  • attendance history analysis
  • AI-assisted guard recommendation features that may use reliability, punctuality, location verification, and attendance history as future recommendation signals

Testing

Passed

Ran ESLint successfully on:

  • src/services/attendance.service.js
  • src/controllers/shiftattendance.controller.js
  • tests/attendance.controller.test.js
  • tests/services/attendance.service.test.js

Added and ran focused service-level Jest tests:

npm test -- tests/services/attendance.service.test.js

Result:

PASS tests/services/attendance.service.test.js

Test Suites: 1 passed, 1 total
Tests: 9 passed, 9 total

Full test suite note

I also ran:

npm test

The full backend test suite currently fails due to existing repo-wide test issues unrelated to this attendance service refactor.

Observed existing issues include:

  • Jest/ESM app-loading failures involving import.meta.url in src/app.js
  • broken/missing imports in some existing tests
  • unrelated controller test expectation failures
  • controller-level tests requiring additional environment/database configuration

The new attendance service unit tests pass independently, and ESLint passes on all files touched by this PR.

Attendance controller test note

I also attempted the attendance controller test using Node VM modules:

node --experimental-vm-modules node_modules/jest/bin/jest.js tests/attendance.controller.test.js

This progressed past the Jest/ESM import.meta.url issue, but the test then failed because process.env.MONGO_URI was not configured for a test database environment.

Because this PR is scoped to the attendance service-layer refactor, it adds and verifies focused service-level unit coverage rather than attempting to fix the wider Jest/Mongo integration test setup.

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.

1 participant