diff --git a/src/components/WorkshopRoster.ts b/src/components/WorkshopRoster.ts new file mode 100644 index 0000000..576b31d --- /dev/null +++ b/src/components/WorkshopRoster.ts @@ -0,0 +1,66 @@ +import { getCleanDigitsPhone } from "../utils"; + +/** + * Format workshop session capacity label according to spec AC-1: + * "Ocupación: {occupied_count} / {max_capacity} ({attended_count} presentes)" + */ +export function formatRosterCapacity( + occupiedCount: number, + maxCapacity: number, + attendedCount: number, +): string { + return `Ocupación: ${occupiedCount} / ${maxCapacity} (${attendedCount} presentes)`; +} + +/** + * Render attendance status pill according to section 2.2: + * REGISTRADO (Slate/Neutral) vs ASISTIÓ (Emerald/Green) + */ +export function renderAttendanceStatusBadge( + attended: boolean, + bookingStatus?: string, +): string { + if (attended || bookingStatus === "COMPLETED") { + return 'ASISTIÓ'; + } + return 'REGISTRADO'; +} + +/** + * Render payment status pill according to section 2.2: + * PAGADO (Emerald/Green) vs PENDIENTE (Amber/Orange) + */ +export function renderPaymentStatusPill(paymentStatus: string): string { + if (paymentStatus === "ACKNOWLEDGED") { + return 'PAGADO'; + } + return 'PENDIENTE'; +} + +/** + * Build 1-tap WhatsApp click-to-chat URL + */ +export function buildWhatsAppUrl(phone: string): string { + const clean = getCleanDigitsPhone(phone); + return `https://wa.me/${clean}`; +} + +export type ConflictType = + | "ALREADY_CHECKED_IN" + | "CAPACITY_EXCEEDED" + | "UNKNOWN"; + +/** + * Disambiguate HTTP 409 Conflict based on response detail + */ +export function disambiguate409Error(detail?: string): ConflictType { + if (!detail) return "UNKNOWN"; + const lower = detail.toLowerCase(); + if (lower.includes("already checked in")) { + return "ALREADY_CHECKED_IN"; + } + if (lower.includes("cupo máximo") || lower.includes("cupo maximo")) { + return "CAPACITY_EXCEEDED"; + } + return "UNKNOWN"; +} diff --git a/tests/roster.test.ts b/tests/roster.test.ts new file mode 100644 index 0000000..94740a6 --- /dev/null +++ b/tests/roster.test.ts @@ -0,0 +1,100 @@ +import { describe, expect, it } from "vitest"; +import { + buildWhatsAppUrl, + disambiguate409Error, + formatRosterCapacity, + renderAttendanceStatusBadge, + renderPaymentStatusPill, +} from "../src/components/WorkshopRoster"; + +describe("Workshop Roster Utilities", () => { + describe("Capacity Formatting", () => { + it("formats capacity counter accurately per spec", () => { + expect(formatRosterCapacity(2, 15, 0)).toBe( + "Ocupación: 2 / 15 (0 presentes)", + ); + expect(formatRosterCapacity(2, 15, 1)).toBe( + "Ocupación: 2 / 15 (1 presentes)", + ); + expect(formatRosterCapacity(3, 15, 3)).toBe( + "Ocupación: 3 / 15 (3 presentes)", + ); + }); + }); + + describe("Status Badges Rendering", () => { + it("renders REGISTRADO when attended is false", () => { + const badge = renderAttendanceStatusBadge(false, "RESERVED"); + expect(badge).toContain("REGISTRADO"); + expect(badge).toContain("status-roster-registered"); + }); + + it("renders ASISTIÓ when attended is true or status is COMPLETED", () => { + const badge1 = renderAttendanceStatusBadge(true, "COMPLETED"); + expect(badge1).toContain("ASISTIÓ"); + expect(badge1).toContain("status-roster-attended"); + + const badge2 = renderAttendanceStatusBadge(false, "COMPLETED"); + expect(badge2).toContain("ASISTIÓ"); + }); + + it("renders PAGADO when payment_status is ACKNOWLEDGED", () => { + const badge = renderPaymentStatusPill("ACKNOWLEDGED"); + expect(badge).toContain("PAGADO"); + expect(badge).toContain("status-roster-paid"); + }); + + it("renders PENDIENTE when payment_status is AWAITING_ACKNOWLEDGMENT", () => { + const badge = renderPaymentStatusPill("AWAITING_ACKNOWLEDGMENT"); + expect(badge).toContain("PENDIENTE"); + expect(badge).toContain("status-roster-pending"); + }); + }); + + describe("WhatsApp URL Generation", () => { + it("generates 1-tap wa.me link with 10-digit number", () => { + expect(buildWhatsAppUrl("5511112222")).toBe("https://wa.me/525511112222"); + }); + + it("generates wa.me link with already prefixed 12-digit number", () => { + expect(buildWhatsAppUrl("525533334444")).toBe( + "https://wa.me/525533334444", + ); + }); + + it("cleans formatting characters in phone number", () => { + expect(buildWhatsAppUrl("+52 55 9876 5432")).toBe( + "https://wa.me/525598765432", + ); + }); + }); + + describe("409 Conflict Disambiguation", () => { + it("identifies already checked in conflict", () => { + expect( + disambiguate409Error("This attendee has already checked in."), + ).toBe("ALREADY_CHECKED_IN"); + }); + + it("identifies capacity reached conflict", () => { + expect( + disambiguate409Error( + "El taller ha alcanzado su cupo máximo (15) para esta sesión.", + ), + ).toBe("CAPACITY_EXCEEDED"); + }); + + it("identifies capacity reached without accent if encountered", () => { + expect( + disambiguate409Error( + "El taller ha alcanzado su cupo maximo para esta sesión.", + ), + ).toBe("CAPACITY_EXCEEDED"); + }); + + it("falls back to UNKNOWN for other 409 or empty messages", () => { + expect(disambiguate409Error("Some generic conflict")).toBe("UNKNOWN"); + expect(disambiguate409Error(undefined)).toBe("UNKNOWN"); + }); + }); +});