fix(attendance): gate webhook emission and response on actual saga outcome#3868
Open
Srejoye wants to merge 1 commit into
Open
fix(attendance): gate webhook emission and response on actual saga outcome#3868Srejoye wants to merge 1 commit into
Srejoye wants to merge 1 commit into
Conversation
…tcome
After AttendanceService.recordAttendance(), the route unconditionally called emitWebhookEvent('attendance.recorded', ...) and returned a hardcoded { alreadyRecorded: false }, regardless of whether the saga's write_attendance step had short-circuited as a duplicate (ctx._alreadyRecorded = true). The publishEvent SSE call already correctly gated on sagaResult.success && !sagaResult.context?._alreadyRecorded; the webhook dispatch and HTTP response did not.
Derive alreadyRecorded from the saga result and apply the same gate to the webhook, and use it (instead of a hardcoded false) for the response body and status code (200 for a duplicate, 201 for a new record).
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
In
app/api/attendance/record/route.js, after callingAttendanceService.recordAttendance(...), the route unconditionally:emitWebhookEvent("attendance.recorded", {...})with no check onsagaResult.successorsagaResult.context?._alreadyRecorded.jsonSuccess({ alreadyRecorded: false }, 201)— a hardcodedfalse, never derived from the saga result.The saga in
lib/services/attendanceService.jsalready tracks idempotency:write_attendancesetsctx._alreadyRecorded = trueand skips the MongoDB write/XP award when a document already exists for thatuserId_datekey. ThepublishEventSSE call already correctly gates onsagaResult.success && !sagaResult.context?._alreadyRecorded— the webhook dispatch and HTTP response did not apply the same gate.This meant a duplicate submission (retry, double-tap, re-marking an already-checked-in student) fired the external
attendance.recordedwebhook a second time for an event that didn't actually happen, and API consumers had no way to tell a genuine new record from a no-op duplicate from the response body.Fix
alreadyRecordedfromsagaResult.context?._alreadyRecorded.emitWebhookEventwhensagaResult.success && !alreadyRecorded— the same condition already used forpublishEvent.{ alreadyRecorded }(no longer hardcoded) with status200for a duplicate or201for a genuinely new record.Tests
Added
app/api/attendance/record/__tests__/webhook-gating.test.js:{ alreadyRecorded: false }/ 201.{ alreadyRecorded: true }/ 200.The pre-existing duplicate-check-in assertion in
app/api/attendance/record/route.test.js, which expectedalreadyRecorded: true/ 200 and was failing against the hardcoded response, now passes as well.Closes #3803