Skip to content

fix(coverage): make coverage transitions atomic, preserve original requester, and reject canceled shifts - #218

Draft
theosiemensrhodes wants to merge 1 commit into
fix/neuron-n04-assignment-historyfrom
fix/neuron-n03-coverage-transitions
Draft

theosiemensrhodes wants to merge 1 commit into
fix/neuron-n04-assignment-historyfrom
fix/neuron-n03-coverage-transitions

Conversation

@theosiemensrhodes

Copy link
Copy Markdown
Collaborator

Summary

Makes coverage and shift attendance transitions atomic per shift, preserves the original requester across coverer unassignment and replacement, and rejects attendance and coverage mutations on canceled shifts. Stacked on top of #216 (fix/neuron-n04-assignment-history) and consumes its shared effective-assignment contract (isVolunteerEffectivelyAssignedToShift in src/server/services/entity/shiftAssignment.ts).

Changes

  • Per-shift serialization & conditional state transitions (src/server/services/entity/coverageService.ts):
    • createCoverageRequest, cancelCoverageRequest, fulfillCoverageRequest, and unassignCoverage each run inside a transaction that locks the target shift row (FOR UPDATE) before inspecting current coverage state or effective roster membership.
    • fulfillCoverageRequest checks effective shift membership via isVolunteerEffectivelyAssignedToShift(tx, shiftId, coveredByVolunteerUserId) under the shift lock, prevents self-fulfillment, and updates conditionally (status = 'open' AND covered_by_volunteer_user_id IS NULL), emitting notifications only after commit. This guarantees at most one competing claimant succeeds and prevents a volunteer from occupying multiple coverage slots on the same shift.
    • cancelCoverageRequest updates conditionally (status = 'open' AND covered_by_volunteer_user_id IS NULL) so withdrawal and fulfillment serialize cleanly.
    • unassignCoverage preserves the original requestingVolunteerUserId (fixing A -> B -> unassign -> C producing A + C on the roster), verifies the request is resolved and the outgoing coverer is still effectively assigned (preventing double-roster corruption after an onward handoff), updates conditionally (status = 'resolved' AND covered_by_volunteer_user_id = ...), and withdraws any orphaned open outgoing request from the outgoing coverer.
  • Canceled-shift enforcement & shift-level serialization (src/server/services/entity/shiftService.ts):
    • checkIn, cancelShift, and assertValidShift reject canceled shifts and lock the shift row (FOR UPDATE) inside a transaction so attendance, cancellation, and coverage mutations serialize per shift.
    • checkIn checks effective roster membership under the shift lock via isVolunteerEffectivelyAssignedToShift(tx, shiftId, volunteerId) and preserves unpublished-term visibility checks.
    • cancelShift uses a conditional update (canceled = false) so concurrent cancellations remain idempotent and do not duplicate cancellation notifications.
  • Regression & concurrency tests (src/test/integration/coverage-transitions.test.ts):
    • A -> B -> unassign -> C original-absence preservation, resulting effective roster, check-in eligibility, requester withdrawal after unassign, and onward-transfer / orphaned-request edge cases.
    • Multi-connection PostgreSQL concurrency tests (postgres pool max: 10) for competing claimants on one open request, single volunteer claiming two open slots on the same shift, fulfill vs. withdraw races, concurrent unassignments, and shift cancellation vs. fulfillment races.
    • Canceled-shift rejection for checkIn (regular volunteer and admin override), createCoverageRequest, cancelCoverageRequest, fulfillCoverageRequest, and unassignCoverage.

Validation

  • pnpm test:run src/test/integration/coverage-transitions.test.ts src/test/integration/shift-assignment.test.ts src/test/integration/shift-service.test.ts src/test/unit/shift-assignment.test.ts on a unique disposable database (neuron_test_n03_coverage, dropped after run): 4 files, 27/27 tests passed.
  • SKIP_ENV_VALIDATION=1 pnpm exec next typegen && pnpm typecheck: passed (0 errors).
  • pnpm exec prettier --check on all modified/added files: passed.
  • Note: repo-wide eslint currently fails on main due to a pre-existing @eslint/eslintrc circular JSON error with eslint-config-next, and existing src/test/integration/coverage-service.test.ts uses hardcoded 2026-07-15 dates that are in the past relative to the local clock.

@theosiemensrhodes
theosiemensrhodes marked this pull request as draft November 23, 2026 20:26
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