fix: ensure appointment is created before saving claim from claims page

- mutationFn now throws on non-ok responses so failed appointment
  creation (409 no slots, 400 validation) properly rejects instead of
  silently calling onSuccess with no id
- handleAppointmentSubmit rejects if server returns success but no id
- invalidate QK_APPOINTMENTS_BASE cache after successful appointment
  creation so the appointments page refreshes automatically

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Gitead
2026-06-03 00:15:44 -04:00
parent bcf4111cdf
commit 4274cd61dc

View File

@@ -113,9 +113,14 @@ export default function ClaimsPage() {
"/api/appointments/upsert", "/api/appointments/upsert",
appointment appointment
); );
return await res.json(); const data = await res.json();
if (!res.ok) {
throw new Error(data?.message || "Failed to create appointment");
}
return data;
}, },
onSuccess: (appointment) => { onSuccess: (appointment) => {
queryClient.invalidateQueries({ queryKey: QK_APPOINTMENTS_BASE });
toast({ toast({
title: "Appointment Scheduled", title: "Appointment Scheduled",
description: appointment.message || "Appointment created successfully.", description: appointment.message || "Appointment created successfully.",
@@ -319,14 +324,13 @@ export default function ClaimsPage() {
}, },
{ {
onSuccess: (appointment) => { onSuccess: (appointment) => {
if (!appointment?.id) {
reject(new Error("Appointment was created but returned no ID"));
return;
}
resolve(appointment.id); resolve(appointment.id);
}, },
onError: (error) => { onError: (error) => {
toast({
title: "Error",
description: "Could not schedule appointment",
variant: "destructive",
});
reject(error); reject(error);
}, },
} }