fix(gro-50): add portal confirm/cancel tests and fix ConfirmationSection state
- Add test coverage for POST /portal/appointments/:id/confirm endpoint - Add test coverage for POST /portal/appointments/:id/cancel endpoint - Fix ConfirmationSection not updating local status after successful confirm - Remove unused onCancel prop from ConfirmationSection call site - Fix Appointments.test.tsx missing confirmationStatus field Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -31,6 +31,10 @@ const APPOINTMENT = {
|
|||||||
endTime: futureDate(),
|
endTime: futureDate(),
|
||||||
customerNotes: null,
|
customerNotes: null,
|
||||||
confirmationToken: "secret-token-leak-test",
|
confirmationToken: "secret-token-leak-test",
|
||||||
|
status: "scheduled" as const,
|
||||||
|
confirmationStatus: "pending" as const,
|
||||||
|
confirmedAt: null,
|
||||||
|
cancelledAt: null,
|
||||||
};
|
};
|
||||||
|
|
||||||
let selectSessionRow: Record<string, unknown> | null = null;
|
let selectSessionRow: Record<string, unknown> | null = null;
|
||||||
@@ -247,3 +251,173 @@ describe("PATCH /portal/appointments/:id/notes", () => {
|
|||||||
expect(res.status).toBe(400);
|
expect(res.status).toBe(400);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ─── POST /portal/appointments/:id/confirm ────────────────────────────────────
|
||||||
|
|
||||||
|
function jsonPost(path: string, headers?: Record<string, string>) {
|
||||||
|
return app.request(path, {
|
||||||
|
method: "POST",
|
||||||
|
headers,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
describe("POST /portal/appointments/:id/confirm", () => {
|
||||||
|
it("confirms a pending appointment and returns updated status", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, confirmationStatus: "pending" };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const body = await res.json();
|
||||||
|
expect(body.confirmationStatus).toBe("confirmed");
|
||||||
|
expect(body).toHaveProperty("confirmedAt");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 401 without X-Impersonation-Session-Id header", async () => {
|
||||||
|
const res = await jsonPost(`/portal/appointments/${APPOINTMENT_ID}/confirm`);
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 401 with expired session", async () => {
|
||||||
|
selectSessionRow = EXPIRED_SESSION;
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 403 when appointment belongs to a different client", async () => {
|
||||||
|
selectSessionRow = { ...ACTIVE_SESSION, clientId: "different-client-id" };
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(403);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 422 when appointment is in the past", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, startTime: pastDate() };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 422 when appointment is not pending confirmation", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, confirmationStatus: "confirmed" };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 422 when cancelling an already-cancelled appointment", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, status: "cancelled", confirmationStatus: "cancelled" };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 404 when appointment not found", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = null;
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/nonexistent-id/confirm`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
// ─── POST /portal/appointments/:id/cancel ─────────────────────────────────────
|
||||||
|
|
||||||
|
describe("POST /portal/appointments/:id/cancel", () => {
|
||||||
|
it("cancels a pending appointment and returns updated status", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, confirmationStatus: "pending" };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(200);
|
||||||
|
const body = await res.json();
|
||||||
|
expect(body.status).toBe("cancelled");
|
||||||
|
expect(body.confirmationStatus).toBe("cancelled");
|
||||||
|
expect(body).toHaveProperty("cancelledAt");
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 401 without X-Impersonation-Session-Id header", async () => {
|
||||||
|
const res = await jsonPost(`/portal/appointments/${APPOINTMENT_ID}/cancel`);
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 401 with expired session", async () => {
|
||||||
|
selectSessionRow = EXPIRED_SESSION;
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(401);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 403 when appointment belongs to a different client", async () => {
|
||||||
|
selectSessionRow = { ...ACTIVE_SESSION, clientId: "different-client-id" };
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(403);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 422 when appointment is in the past", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, startTime: pastDate() };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 422 when appointment is already cancelled", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, status: "cancelled", confirmationStatus: "cancelled" };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 422 when appointment is already completed", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = { ...APPOINTMENT, status: "completed" };
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/${APPOINTMENT_ID}/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(422);
|
||||||
|
});
|
||||||
|
|
||||||
|
it("returns 404 when appointment not found", async () => {
|
||||||
|
selectSessionRow = ACTIVE_SESSION;
|
||||||
|
selectAppointmentRow = null;
|
||||||
|
const res = await jsonPost(
|
||||||
|
`/portal/appointments/nonexistent-id/cancel`,
|
||||||
|
{ "X-Impersonation-Session-Id": SESSION_ID }
|
||||||
|
);
|
||||||
|
expect(res.status).toBe(404);
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -18,6 +18,7 @@ const UPCOMING_APPT: Appointment = {
|
|||||||
status: "confirmed",
|
status: "confirmed",
|
||||||
notes: "",
|
notes: "",
|
||||||
customerNotes: "",
|
customerNotes: "",
|
||||||
|
confirmationStatus: "pending",
|
||||||
};
|
};
|
||||||
|
|
||||||
const PAST_APPT: Appointment = {
|
const PAST_APPT: Appointment = {
|
||||||
|
|||||||
@@ -172,7 +172,7 @@ function AppointmentCard({
|
|||||||
<CustomerNotesSection appointment={appt} sessionId={sessionId} />
|
<CustomerNotesSection appointment={appt} sessionId={sessionId} />
|
||||||
)}
|
)}
|
||||||
{isUpcoming(appt) && (
|
{isUpcoming(appt) && (
|
||||||
<ConfirmationSection appointment={appt} sessionId={sessionId} onCancel={() => {}} />
|
<ConfirmationSection appointment={appt} sessionId={sessionId} />
|
||||||
)}
|
)}
|
||||||
{appt.status !== "completed" && appt.status !== "cancelled" && !readOnly && (
|
{appt.status !== "completed" && appt.status !== "cancelled" && !readOnly && (
|
||||||
<div className="flex gap-2 mt-3">
|
<div className="flex gap-2 mt-3">
|
||||||
@@ -199,6 +199,8 @@ export function ConfirmationSection({ appointment: appt, sessionId }: { appointm
|
|||||||
const [confirming, setConfirming] = useState(false);
|
const [confirming, setConfirming] = useState(false);
|
||||||
const [confirmError, setConfirmError] = useState<string | null>(null);
|
const [confirmError, setConfirmError] = useState<string | null>(null);
|
||||||
const [confirmSuccess, setConfirmSuccess] = useState(false);
|
const [confirmSuccess, setConfirmSuccess] = useState(false);
|
||||||
|
// Local state mirrors confirmationStatus so the badge updates immediately after confirm
|
||||||
|
const [localStatus, setLocalStatus] = useState(appt.confirmationStatus);
|
||||||
|
|
||||||
async function handleConfirm() {
|
async function handleConfirm() {
|
||||||
if (!window.confirm("Confirm this appointment?")) return;
|
if (!window.confirm("Confirm this appointment?")) return;
|
||||||
@@ -217,6 +219,7 @@ export function ConfirmationSection({ appointment: appt, sessionId }: { appointm
|
|||||||
const err = await res.json().catch(() => ({ error: "Failed to confirm" }));
|
const err = await res.json().catch(() => ({ error: "Failed to confirm" }));
|
||||||
throw new Error(err.error || `HTTP ${res.status}`);
|
throw new Error(err.error || `HTTP ${res.status}`);
|
||||||
}
|
}
|
||||||
|
setLocalStatus("confirmed");
|
||||||
setConfirmSuccess(true);
|
setConfirmSuccess(true);
|
||||||
setTimeout(() => setConfirmSuccess(false), 2000);
|
setTimeout(() => setConfirmSuccess(false), 2000);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
@@ -226,9 +229,10 @@ export function ConfirmationSection({ appointment: appt, sessionId }: { appointm
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const statusLabel = appt.confirmationStatus === "confirmed"
|
const currentStatus = localStatus ?? appt.confirmationStatus;
|
||||||
|
const statusLabel = currentStatus === "confirmed"
|
||||||
? "✓ Confirmed"
|
? "✓ Confirmed"
|
||||||
: appt.confirmationStatus === "pending"
|
: currentStatus === "pending"
|
||||||
? "Pending confirmation"
|
? "Pending confirmation"
|
||||||
: "Cancelled";
|
: "Cancelled";
|
||||||
|
|
||||||
@@ -236,11 +240,11 @@ export function ConfirmationSection({ appointment: appt, sessionId }: { appointm
|
|||||||
<div className="mt-3 p-3 bg-stone-50 rounded-lg">
|
<div className="mt-3 p-3 bg-stone-50 rounded-lg">
|
||||||
<div className="flex items-center justify-between">
|
<div className="flex items-center justify-between">
|
||||||
<div className="flex items-center gap-2">
|
<div className="flex items-center gap-2">
|
||||||
<span className={`px-2 py-0.5 rounded-full text-xs font-medium ${CONFIRMATION_STATUS_COLORS[appt.confirmationStatus] || ""}`}>
|
<span className={`px-2 py-0.5 rounded-full text-xs font-medium ${CONFIRMATION_STATUS_COLORS[currentStatus] || ""}`}>
|
||||||
{statusLabel}
|
{statusLabel}
|
||||||
</span>
|
</span>
|
||||||
</div>
|
</div>
|
||||||
{!confirmSuccess && appt.confirmationStatus === "pending" && (
|
{!confirmSuccess && currentStatus === "pending" && (
|
||||||
<button
|
<button
|
||||||
onClick={handleConfirm}
|
onClick={handleConfirm}
|
||||||
disabled={confirming}
|
disabled={confirming}
|
||||||
|
|||||||
Reference in New Issue
Block a user