diff --git a/susconecta/app/paciente/page.tsx b/susconecta/app/paciente/page.tsx index a8b4890..d42f6c9 100644 --- a/susconecta/app/paciente/page.tsx +++ b/susconecta/app/paciente/page.tsx @@ -867,17 +867,33 @@ export default function PacientePage() { try { const ok = typeof window !== 'undefined' ? window.confirm('Deseja realmente cancelar esta consulta?') : true if (!ok) return - // call API to delete - await deletarAgendamento(consulta.id) - // Mark as deleted in cache so it won't appear again - addDeletedAppointmentId(consulta.id) - // remove from local list + + // Prefer PATCH to mark appointment as cancelled (safer under RLS) + try { + await atualizarAgendamento(consulta.id, { + cancelled_at: new Date().toISOString(), + status: 'cancelled', + cancellation_reason: 'Cancelado pelo paciente' + }) + } catch (patchErr) { + // Fallback: try hard delete if server allows it + try { + await deletarAgendamento(consulta.id) + } catch (delErr) { + // Re-throw original patch error if both fail + throw patchErr || delErr + } + } + + // remove from local list so UI updates immediately setAppointments((prev) => { if (!prev) return prev return prev.filter((a: any) => String(a.id) !== String(consulta.id)) }) // if modal open for this appointment, close it if (selectedAppointment && String(selectedAppointment.id) === String(consulta.id)) setSelectedAppointment(null) + // Optionally persist to deleted cache to help client-side filtering + try { addDeletedAppointmentId(consulta.id) } catch(e) {} setToast({ type: 'success', msg: 'Consulta cancelada.' }) } catch (err: any) { console.error('[Consultas] falha ao cancelar agendamento', err)