fix(GRO-2234): transparent re-mint on 401 for portal Book New submit
CI / Test (pull_request) Failing after 14m28s
CI / Lint & Typecheck (pull_request) Failing after 14m29s
CI / Build & Push Docker Image (pull_request) Has been skipped

A deliberately-paced Book New wizard could outlive the portal impersonation
session, so the final POST /api/portal/waitlist returned 401 and the UI showed
"Failed to book appointment. Please try again."

BookingFlow now retries once on a 401: it re-mints a fresh portal session via
POST /api/portal/session-from-auth (the customer's Better Auth cookie is still
valid) and resubmits the waitlist request with the new
X-Impersonation-Session-Id. Falls through to the existing error if no Better
Auth session is available (staff/dev impersonation paths).

- Appointments.tsx: remintPortalSession() helper; handleConfirmBooking submits
  via submitWaitlist(id) and retries once after a 401 re-mint.
- Test: first waitlist POST 401 -> re-mint -> retry with fresh id -> success;
  asserts exactly one re-mint and the header sequence.
- UAT_PLAYBOOK.md 5.12e: TC-WEB-5.12.25 slow-wizard submit succeeds.

Companion to groombook/api GRO-2234 (bounded sliding expiration).

Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
Flea Flicker
2026-06-08 18:55:00 +00:00
parent 3d0c3c551b
commit 915a310e0a
3 changed files with 131 additions and 14 deletions
+71
View File
@@ -801,4 +801,75 @@ describe("BookingFlow Book New funnel (GRO-2213)", () => {
expect(body.preferredTime).toBe("10:00:00");
expect(body.preferredDate).toBe("2026-06-09");
});
it("re-mints the portal session and retries once when waitlist returns 401 (GRO-2234)", async () => {
const calls = { waitlist: 0, remint: 0 };
const waitlistHeaders: string[] = [];
const routed = (input: RequestInfo | URL, init?: RequestInit) => {
const url = typeof input === "string" ? input : input.toString();
if (url.includes("/api/portal/pets")) {
return Promise.resolve({
ok: true,
json: async () => ({ pets: [{ id: "pet-1", name: "Buddy", breed: "Lab" }] }),
} as Response);
}
if (url.includes("/api/portal/services")) {
return Promise.resolve({
ok: true,
json: async () => ({
services: [{ id: "service-1", name: "Bath & Brush", isAddOn: false, duration: 60, price: 50 }],
}),
} as Response);
}
if (url.includes("/api/book/availability")) {
return Promise.resolve({
ok: true,
json: async () => ["2026-06-09T10:00:00.000Z"],
} as Response);
}
if (url.includes("/api/portal/session-from-auth")) {
calls.remint += 1;
return Promise.resolve({
ok: true,
json: async () => ({ sessionId: "fresh-session-id", clientId: "c1", clientName: "Jane" }),
} as Response);
}
if (url.includes("/api/portal/waitlist")) {
calls.waitlist += 1;
const headers = (init?.headers ?? {}) as Record<string, string>;
waitlistHeaders.push(headers["X-Impersonation-Session-Id"] ?? "");
// First attempt: session lapsed → 401. Retry after re-mint: success.
if (calls.waitlist === 1) {
return Promise.resolve({ ok: false, status: 401, json: async () => ({ error: "Unauthorized" }) } as Response);
}
return Promise.resolve({ ok: true, status: 201, json: async () => ({}) } as Response);
}
return Promise.resolve({ ok: true, json: async () => ({}) } as Response);
};
global.fetch = vi.fn().mockImplementation(routed as typeof fetch);
render(<BookingFlow onClose={() => {}} sessionId="stale-session-id" />);
await waitFor(() => expect(screen.getByText("Buddy")).toBeInTheDocument());
fireEvent.click(screen.getByText("Buddy"));
await waitFor(() => expect(screen.getByText("Bath & Brush")).toBeInTheDocument());
fireEvent.click(screen.getByText("Bath & Brush"));
fireEvent.click(screen.getByRole("button", { name: /^Next$/ }));
await waitFor(() => expect(screen.getByText("First Available")).toBeInTheDocument());
fireEvent.click(screen.getByRole("button", { name: /^Next$/ }));
await waitFor(() => expect(screen.getByLabelText(/date/i)).toBeInTheDocument());
fireEvent.change(screen.getByLabelText(/date/i), { target: { value: "2026-06-09" } });
await waitFor(() => expect(screen.getByText("10:00 AM")).toBeInTheDocument());
fireEvent.click(screen.getByText("10:00 AM"));
fireEvent.click(screen.getByRole("button", { name: /^Next$/ }));
await waitFor(() => expect(screen.getByText(/Review & Confirm/i)).toBeInTheDocument());
fireEvent.click(screen.getByRole("button", { name: /Confirm Booking/i }));
// Re-mint happened exactly once, waitlist retried with the fresh id, and the
// booking succeeded (no error surfaced).
await waitFor(() => expect(calls.waitlist).toBe(2));
expect(calls.remint).toBe(1);
expect(waitlistHeaders).toEqual(["stale-session-id", "fresh-session-id"]);
expect(screen.queryByText(/Failed to book appointment/i)).not.toBeInTheDocument();
});
});