fix: listagem e criação de consultas
This commit is contained in:
parent
c5461858b0
commit
2ad8e8ae27
@ -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<ConsultaModalProps> = ({
|
||||
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<ConsultaModalProps> = ({
|
||||
} 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<ConsultaModalProps> = ({
|
||||
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<ConsultaModalProps> = ({
|
||||
<option value="">Selecione...</option>
|
||||
{pacientes.map((p) => (
|
||||
<option key={p.id} value={p.id}>
|
||||
{p.nome}
|
||||
{p.full_name}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
@ -250,7 +277,7 @@ const ConsultaModal: React.FC<ConsultaModalProps> = ({
|
||||
<option value="">Selecione...</option>
|
||||
{medicos.map((m) => (
|
||||
<option key={m.id} value={m.id}>
|
||||
{m.nome} - {m.especialidade}
|
||||
{m.full_name} - {m.specialty}
|
||||
</option>
|
||||
))}
|
||||
</select>
|
||||
|
||||
@ -44,6 +44,7 @@ export interface CreateAppointmentInput {
|
||||
appointment_type?: AppointmentType;
|
||||
chief_complaint?: string;
|
||||
patient_notes?: string;
|
||||
notes?: string;
|
||||
insurance_provider?: string;
|
||||
}
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user