fix: listagem e criação de consultas
This commit is contained in:
parent
c5461858b0
commit
2ad8e8ae27
@ -8,7 +8,9 @@ import {
|
|||||||
type Patient,
|
type Patient,
|
||||||
type Doctor,
|
type Doctor,
|
||||||
} from "../../services";
|
} from "../../services";
|
||||||
|
import type { AppointmentStatus, AppointmentType } from "../../services/appointments/types";
|
||||||
import { useAuth } from "../../hooks/useAuth";
|
import { useAuth } from "../../hooks/useAuth";
|
||||||
|
import toast from "react-hot-toast";
|
||||||
|
|
||||||
// Type aliases para compatibilidade com código antigo
|
// Type aliases para compatibilidade com código antigo
|
||||||
type Consulta = Appointment & {
|
type Consulta = Appointment & {
|
||||||
@ -93,11 +95,11 @@ const ConsultaModal: React.FC<ConsultaModalProps> = ({
|
|||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (!isOpen) return;
|
if (!isOpen) return;
|
||||||
if (editing) {
|
if (editing) {
|
||||||
setPacienteId(editing.pacienteId);
|
setPacienteId(editing.patient_id || editing.pacienteId || "");
|
||||||
setMedicoId(editing.medicoId);
|
setMedicoId(editing.doctor_id || editing.medicoId || "");
|
||||||
// Convert ISO to local datetime-local value
|
// Convert ISO to local datetime-local value
|
||||||
try {
|
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)
|
const local = new Date(d.getTime() - d.getTimezoneOffset() * 60000)
|
||||||
.toISOString()
|
.toISOString()
|
||||||
.slice(0, 16);
|
.slice(0, 16);
|
||||||
@ -105,10 +107,10 @@ const ConsultaModal: React.FC<ConsultaModalProps> = ({
|
|||||||
} catch {
|
} catch {
|
||||||
setDataHora("");
|
setDataHora("");
|
||||||
}
|
}
|
||||||
setTipo(editing.tipo || "");
|
setTipo(editing.appointment_type || "");
|
||||||
setMotivo(editing.motivo || "");
|
setMotivo(editing.chief_complaint || "");
|
||||||
setObservacoes(editing.observacoes || "");
|
setObservacoes(editing.notes || editing.observacoes || "");
|
||||||
setStatus(editing.status || "agendada");
|
setStatus(editing.status || "confirmed");
|
||||||
} else {
|
} else {
|
||||||
setPacienteId(defaultPacienteId || "");
|
setPacienteId(defaultPacienteId || "");
|
||||||
setMedicoId(defaultMedicoId || "");
|
setMedicoId(defaultMedicoId || "");
|
||||||
@ -161,38 +163,63 @@ const ConsultaModal: React.FC<ConsultaModalProps> = ({
|
|||||||
try {
|
try {
|
||||||
// Convert local datetime back to ISO
|
// Convert local datetime back to ISO
|
||||||
const iso = new Date(dataHora).toISOString();
|
const iso = new Date(dataHora).toISOString();
|
||||||
|
|
||||||
if (editing) {
|
if (editing) {
|
||||||
const payload: ConsultaUpdate = {
|
// Atualizar consulta existente
|
||||||
dataHora: iso,
|
const payload = {
|
||||||
tipo: tipo || undefined,
|
scheduled_at: iso,
|
||||||
motivo: motivo || undefined,
|
chief_complaint: motivo || undefined,
|
||||||
observacoes: observacoes || undefined,
|
notes: observacoes || undefined,
|
||||||
status: status,
|
status: status as AppointmentStatus,
|
||||||
};
|
};
|
||||||
const resp = await consultasService.atualizar(editing.id, payload);
|
|
||||||
if (!resp.success || !resp.data) {
|
const updated = await appointmentService.update(editing.id, payload);
|
||||||
throw new Error(resp.error || "Falha ao atualizar consulta");
|
|
||||||
}
|
// Converter para formato esperado
|
||||||
onSaved(resp.data);
|
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 {
|
} else {
|
||||||
const payload: ConsultaCreate = {
|
// Criar nova consulta
|
||||||
pacienteId,
|
const appointmentType = (tipo && (tipo.toLowerCase().includes('telemedicina') || tipo.toLowerCase().includes('online')))
|
||||||
medicoId,
|
? 'telemedicina'
|
||||||
dataHora: iso,
|
: 'presencial';
|
||||||
tipo: tipo || undefined,
|
|
||||||
motivo: motivo || undefined,
|
const payload = {
|
||||||
observacoes: observacoes || undefined,
|
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) {
|
const created = await appointmentService.create(payload);
|
||||||
throw new Error(resp.error || "Falha ao criar consulta");
|
|
||||||
}
|
// Converter para formato esperado
|
||||||
onSaved(resp.data);
|
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();
|
onClose();
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
const msg = err instanceof Error ? err.message : "Erro ao salvar";
|
const msg = err instanceof Error ? err.message : "Erro ao salvar";
|
||||||
setError(msg);
|
setError(msg);
|
||||||
|
toast.error(msg);
|
||||||
|
console.error("Erro ao salvar consulta:", err);
|
||||||
} finally {
|
} finally {
|
||||||
setSaving(false);
|
setSaving(false);
|
||||||
}
|
}
|
||||||
@ -232,7 +259,7 @@ const ConsultaModal: React.FC<ConsultaModalProps> = ({
|
|||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{pacientes.map((p) => (
|
{pacientes.map((p) => (
|
||||||
<option key={p.id} value={p.id}>
|
<option key={p.id} value={p.id}>
|
||||||
{p.nome}
|
{p.full_name}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
@ -250,7 +277,7 @@ const ConsultaModal: React.FC<ConsultaModalProps> = ({
|
|||||||
<option value="">Selecione...</option>
|
<option value="">Selecione...</option>
|
||||||
{medicos.map((m) => (
|
{medicos.map((m) => (
|
||||||
<option key={m.id} value={m.id}>
|
<option key={m.id} value={m.id}>
|
||||||
{m.nome} - {m.especialidade}
|
{m.full_name} - {m.specialty}
|
||||||
</option>
|
</option>
|
||||||
))}
|
))}
|
||||||
</select>
|
</select>
|
||||||
|
|||||||
@ -44,6 +44,7 @@ export interface CreateAppointmentInput {
|
|||||||
appointment_type?: AppointmentType;
|
appointment_type?: AppointmentType;
|
||||||
chief_complaint?: string;
|
chief_complaint?: string;
|
||||||
patient_notes?: string;
|
patient_notes?: string;
|
||||||
|
notes?: string;
|
||||||
insurance_provider?: string;
|
insurance_provider?: string;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user