From 2ad8e8ae2740bd678773a0fae226cf56496e47ad Mon Sep 17 00:00:00 2001 From: Pedro Araujo da Silveira Date: Wed, 29 Oct 2025 17:50:57 -0300 Subject: [PATCH] =?UTF-8?q?fix:=20listagem=20e=20cria=C3=A7=C3=A3o=20de=20?= =?UTF-8?q?consultas?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../components/consultas/ConsultaModal.tsx | 91 ++++++++++++------- .../src/services/appointments/types.ts | 1 + 2 files changed, 60 insertions(+), 32 deletions(-) diff --git a/MEDICONNECT 2/src/components/consultas/ConsultaModal.tsx b/MEDICONNECT 2/src/components/consultas/ConsultaModal.tsx index ee4d537de..b9e3aeae3 100644 --- a/MEDICONNECT 2/src/components/consultas/ConsultaModal.tsx +++ b/MEDICONNECT 2/src/components/consultas/ConsultaModal.tsx @@ -8,7 +8,9 @@ import { type Patient, type Doctor, } from "../../services"; +import type { AppointmentStatus, AppointmentType } from "../../services/appointments/types"; import { useAuth } from "../../hooks/useAuth"; +import toast from "react-hot-toast"; // Type aliases para compatibilidade com código antigo type Consulta = Appointment & { @@ -93,11 +95,11 @@ const ConsultaModal: React.FC = ({ useEffect(() => { if (!isOpen) return; if (editing) { - setPacienteId(editing.pacienteId); - setMedicoId(editing.medicoId); + setPacienteId(editing.patient_id || editing.pacienteId || ""); + setMedicoId(editing.doctor_id || editing.medicoId || ""); // Convert ISO to local datetime-local value try { - const d = new Date(editing.dataHora); + const d = new Date(editing.scheduled_at || editing.dataHora || ""); const local = new Date(d.getTime() - d.getTimezoneOffset() * 60000) .toISOString() .slice(0, 16); @@ -105,10 +107,10 @@ const ConsultaModal: React.FC = ({ } catch { setDataHora(""); } - setTipo(editing.tipo || ""); - setMotivo(editing.motivo || ""); - setObservacoes(editing.observacoes || ""); - setStatus(editing.status || "agendada"); + setTipo(editing.appointment_type || ""); + setMotivo(editing.chief_complaint || ""); + setObservacoes(editing.notes || editing.observacoes || ""); + setStatus(editing.status || "confirmed"); } else { setPacienteId(defaultPacienteId || ""); setMedicoId(defaultMedicoId || ""); @@ -161,38 +163,63 @@ const ConsultaModal: React.FC = ({ try { // Convert local datetime back to ISO const iso = new Date(dataHora).toISOString(); + if (editing) { - const payload: ConsultaUpdate = { - dataHora: iso, - tipo: tipo || undefined, - motivo: motivo || undefined, - observacoes: observacoes || undefined, - status: status, + // Atualizar consulta existente + const payload = { + scheduled_at: iso, + chief_complaint: motivo || undefined, + notes: observacoes || undefined, + status: status as AppointmentStatus, }; - const resp = await consultasService.atualizar(editing.id, payload); - if (!resp.success || !resp.data) { - throw new Error(resp.error || "Falha ao atualizar consulta"); - } - onSaved(resp.data); + + const updated = await appointmentService.update(editing.id, payload); + + // Converter para formato esperado + const result: Consulta = { + ...updated, + pacienteId: updated.patient_id, + medicoId: updated.doctor_id, + dataHora: updated.scheduled_at, + }; + + toast.success("Consulta atualizada com sucesso!"); + onSaved(result); } else { - const payload: ConsultaCreate = { - pacienteId, - medicoId, - dataHora: iso, - tipo: tipo || undefined, - motivo: motivo || undefined, - observacoes: observacoes || undefined, + // Criar nova consulta + const appointmentType = (tipo && (tipo.toLowerCase().includes('telemedicina') || tipo.toLowerCase().includes('online'))) + ? 'telemedicina' + : 'presencial'; + + const payload = { + patient_id: pacienteId, + doctor_id: medicoId, + scheduled_at: iso, + appointment_type: appointmentType as AppointmentType, + chief_complaint: motivo || undefined, + patient_notes: observacoes || undefined, + duration_minutes: 30, }; - const resp = await consultasService.criar(payload); - if (!resp.success || !resp.data) { - throw new Error(resp.error || "Falha ao criar consulta"); - } - onSaved(resp.data); + + const created = await appointmentService.create(payload); + + // Converter para formato esperado + const result: Consulta = { + ...created, + pacienteId: created.patient_id, + medicoId: created.doctor_id, + dataHora: created.scheduled_at, + }; + + toast.success("Consulta criada com sucesso!"); + onSaved(result); } onClose(); } catch (err) { const msg = err instanceof Error ? err.message : "Erro ao salvar"; setError(msg); + toast.error(msg); + console.error("Erro ao salvar consulta:", err); } finally { setSaving(false); } @@ -232,7 +259,7 @@ const ConsultaModal: React.FC = ({ {pacientes.map((p) => ( ))} @@ -250,7 +277,7 @@ const ConsultaModal: React.FC = ({ {medicos.map((m) => ( ))} diff --git a/MEDICONNECT 2/src/services/appointments/types.ts b/MEDICONNECT 2/src/services/appointments/types.ts index 018dfdd70..6d0b0ca87 100644 --- a/MEDICONNECT 2/src/services/appointments/types.ts +++ b/MEDICONNECT 2/src/services/appointments/types.ts @@ -44,6 +44,7 @@ export interface CreateAppointmentInput { appointment_type?: AppointmentType; chief_complaint?: string; patient_notes?: string; + notes?: string; insurance_provider?: string; }