fix(consulta) corrigir o erro na parte de cancelar consulta

This commit is contained in:
Jonas Francisco 2025-11-08 01:36:44 -03:00
parent 68c38dba88
commit ca282e721e

View File

@ -867,17 +867,33 @@ export default function PacientePage() {
try { try {
const ok = typeof window !== 'undefined' ? window.confirm('Deseja realmente cancelar esta consulta?') : true const ok = typeof window !== 'undefined' ? window.confirm('Deseja realmente cancelar esta consulta?') : true
if (!ok) return if (!ok) return
// call API to delete
// 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) await deletarAgendamento(consulta.id)
// Mark as deleted in cache so it won't appear again } catch (delErr) {
addDeletedAppointmentId(consulta.id) // Re-throw original patch error if both fail
// remove from local list throw patchErr || delErr
}
}
// remove from local list so UI updates immediately
setAppointments((prev) => { setAppointments((prev) => {
if (!prev) return prev if (!prev) return prev
return prev.filter((a: any) => String(a.id) !== String(consulta.id)) return prev.filter((a: any) => String(a.id) !== String(consulta.id))
}) })
// if modal open for this appointment, close it // if modal open for this appointment, close it
if (selectedAppointment && String(selectedAppointment.id) === String(consulta.id)) setSelectedAppointment(null) 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.' }) setToast({ type: 'success', msg: 'Consulta cancelada.' })
} catch (err: any) { } catch (err: any) {
console.error('[Consultas] falha ao cancelar agendamento', err) console.error('[Consultas] falha ao cancelar agendamento', err)