diff --git a/docs/evidence/anne-self-audit.md b/docs/evidence/anne-self-audit.md index 03f9887..ef0d88c 100644 --- a/docs/evidence/anne-self-audit.md +++ b/docs/evidence/anne-self-audit.md @@ -128,16 +128,17 @@ The implementation strictly respects the Scope Containment Invariant: --- -## 6. Addendum: Post-Review Revision (Targeted 1-Commit Fix) +## 6. Addendum: Post-Review Revision & Multi-Attendee 409 Server Reconciliation -Following Project Owner review of PR #9, two targeted ergonomic and security enhancements were implemented: +Following Project Owner review and QA verification of PR #9, targeted enhancements were implemented: 1. **Finding A: HTML Sanitization (`escapeHtml`)**: - Introduced and exported `escapeHtml(str: string): string` in `src/components/WorkshopRoster.ts` (matching the pattern in `ClientDirectory.ts` and `IntakeModal.ts`). - Sanitized all dynamic string interpolations (`attendee.client_name`, `attendee.client_phone`, `attendee.booking_code`, `session.service_name`, date strings, etc.) across visible text nodes and attribute values (`aria-label`, `title`, `href`, `data-booking-id`). -2. **Finding B: Out-of-Band 409 Capacity Counter Synchronization**: - - Implemented `reconcileAlreadyCheckedInState()` in `src/components/WorkshopRoster.ts`. - - When an `ALREADY_CHECKED_IN` 409 conflict occurs for an attendee who was locally unrecorded (`!attendee.attended`), `session.attended_count` is now incremented by 1 and `updateCapacityDisplay(session, sessionIdx)` is invoked, ensuring header capacity counters remain strictly synchronized even during out-of-band check-ins across devices. +2. **Finding B & Case 2B: Authoritative Server Reconciliation on 409 Conflict**: + - Implemented `reconcileSessionsWithServer()` in `src/components/WorkshopRoster.ts`. + - When an `ALREADY_CHECKED_IN` 409 conflict occurs, the client now performs an authoritative refresh from `fetchRosters({ date_from, date_to })`, reconciling all sessions and attendees against server state and re-rendering the view. + - This ensures that if multiple attendees were checked in out-of-band across devices (Case 2B), the capacity counter immediately reflects the exact server count (e.g. `2 presentes`) and all checked-in attendees render with `ASISTIÓ`. If the refresh fetch fails, it gracefully falls back to local row reconciliation via `reconcileAlreadyCheckedInState()`. 3. **Verification**: - - Added unit test suites in `tests/roster.test.ts` validating HTML escaping (XSS vectors, attributes, plain strings) and 409 state reconciliation (incrementing when locally unrecorded, idempotent when already recorded). - - Test suite passes 100%: 33/33 tests passing (17 utils + 16 roster). + - Added unit test suites in `tests/roster.test.ts` validating HTML escaping (XSS vectors, attributes, plain strings), local state reconciliation, and authoritative multi-attendee out-of-band reconciliation (Case 2B). + - Test suite passes 100%: 34/34 tests passing (17 utils + 17 roster). diff --git a/src/components/WorkshopRoster.ts b/src/components/WorkshopRoster.ts index c1acbf1..8f46a14 100644 --- a/src/components/WorkshopRoster.ts +++ b/src/components/WorkshopRoster.ts @@ -130,6 +130,40 @@ export function reconcileAlreadyCheckedInState( return false; } +/** + * Reconcile local session state with authoritative server sessions. + * Matches sessions by service_id and scheduled_at to update counts and attendee records. + */ +export function reconcileSessionsWithServer( + currentSessions: RosterSession[], + freshSessions: RosterSession[], +): RosterSession[] { + if (!freshSessions || freshSessions.length === 0) { + return currentSessions; + } + return freshSessions.map((fresh) => { + const existing = currentSessions.find( + (curr) => + curr.service_id === fresh.service_id && + curr.scheduled_at === fresh.scheduled_at, + ); + if (!existing) return fresh; + return { + ...existing, + ...fresh, + attendees: fresh.attendees.map((freshAtt) => { + const existingAtt = existing.attendees.find( + (a) => a.booking_id === freshAtt.booking_id, + ); + return { + ...existingAtt, + ...freshAtt, + }; + }), + }; + }); +} + /** * Disambiguate HTTP 409 Conflict based on response detail */ @@ -561,12 +595,28 @@ export function renderWorkshopRoster( const conflictType = disambiguate409Error(detail); if (conflictType === "ALREADY_CHECKED_IN") { - // 409 Case A: Reconcile row to ASISTIÓ, disable button, informative toast - const incremented = reconcileAlreadyCheckedInState(session, attendee); - if (incremented) { - updateCapacityDisplay(session, sessionIdx); + // 409 Case A: Authoritative server state reconciliation + try { + const freshSessions = await fetchRosters({ + date_from: selectedDate, + date_to: selectedDate, + }); + cachedSessions = reconcileSessionsWithServer( + cachedSessions, + freshSessions, + ); + if (contentEl) { + renderSessions(contentEl, cachedSessions); + } + } catch { + // Fallback to local row reconciliation if refresh fetch fails + const incremented = reconcileAlreadyCheckedInState(session, attendee); + if (incremented) { + updateCapacityDisplay(session, sessionIdx); + } + updateRowAttendanceState(rowEl, attendee); } - updateRowAttendanceState(rowEl, attendee); + showToast("El asistente ya contaba con asistencia registrada", "info"); onStateChange?.(); } else { diff --git a/tests/roster.test.ts b/tests/roster.test.ts index 65185d6..a44c8a2 100644 --- a/tests/roster.test.ts +++ b/tests/roster.test.ts @@ -5,6 +5,7 @@ import { escapeHtml, formatRosterCapacity, reconcileAlreadyCheckedInState, + reconcileSessionsWithServer, renderAttendanceStatusBadge, renderPaymentStatusPill, } from "../src/components/WorkshopRoster"; @@ -169,5 +170,99 @@ describe("Workshop Roster Utilities", () => { expect(session.attended_count).toBe(1); // Unchanged expect(attendee.attended).toBe(true); }); + + it("reconciles multiple out-of-band check-ins authoritatively from server response (Case 2B)", () => { + const staleLocalSession: RosterSession = { + service_id: "srv-123", + service_name: "Círculo de Cacao", + scheduled_at: "2026-10-26T18:00:00Z", + max_capacity: 15, + occupied_count: 2, + attended_count: 0, + attendees: [ + { + booking_id: "bk-1001", + booking_code: "ADS-1001", + client_id: "cl-1", + client_name: "Elena Gómez", + client_phone: "525511112222", + booking_status: "RESERVED", + payment_arrangement: "PREPAID_DEFAULT", + payment_status: "ACKNOWLEDGED", + amount_mxn: 350, + attended: false, + }, + { + booking_id: "bk-1002", + booking_code: "ADS-1002", + client_id: "cl-2", + client_name: "Carlos Méndez", + client_phone: "525533334444", + booking_status: "RESERVED", + payment_arrangement: "PREPAID_DEFAULT", + payment_status: "AWAITING_ACKNOWLEDGMENT", + amount_mxn: 350, + attended: false, + }, + ], + }; + + const authoritativeServerSession: RosterSession = { + service_id: "srv-123", + service_name: "Círculo de Cacao", + scheduled_at: "2026-10-26T18:00:00Z", + max_capacity: 15, + occupied_count: 2, + attended_count: 2, + attendees: [ + { + booking_id: "bk-1001", + booking_code: "ADS-1001", + client_id: "cl-1", + client_name: "Elena Gómez", + client_phone: "525511112222", + booking_status: "COMPLETED", + payment_arrangement: "PREPAID_DEFAULT", + payment_status: "ACKNOWLEDGED", + amount_mxn: 350, + attended: true, + }, + { + booking_id: "bk-1002", + booking_code: "ADS-1002", + client_id: "cl-2", + client_name: "Carlos Méndez", + client_phone: "525533334444", + booking_status: "COMPLETED", + payment_arrangement: "PREPAID_DEFAULT", + payment_status: "ACKNOWLEDGED", + amount_mxn: 350, + attended: true, + }, + ], + }; + + const reconciled = reconcileSessionsWithServer( + [staleLocalSession], + [authoritativeServerSession], + ); + + expect(reconciled).toHaveLength(1); + expect(reconciled[0].occupied_count).toBe(2); + expect(reconciled[0].attended_count).toBe(2); + + const elena = reconciled[0].attendees.find( + (a) => a.booking_code === "ADS-1001", + ); + const carlos = reconciled[0].attendees.find( + (a) => a.booking_code === "ADS-1002", + ); + + expect(elena?.attended).toBe(true); + expect(elena?.booking_status).toBe("COMPLETED"); + + expect(carlos?.attended).toBe(true); + expect(carlos?.booking_status).toBe("COMPLETED"); + }); }); });