diff --git a/susconecta/app/profissional/page.tsx b/susconecta/app/profissional/page.tsx index e26d086..6c2559b 100644 --- a/susconecta/app/profissional/page.tsx +++ b/susconecta/app/profissional/page.tsx @@ -4,7 +4,7 @@ import SignatureCanvas from "react-signature-canvas"; import Link from "next/link"; import ProtectedRoute from "@/components/ProtectedRoute"; import { useAuth } from "@/hooks/useAuth"; -import { buscarPacientes, listarPacientes, buscarPacientePorId, type Paciente } from "@/lib/api"; +import { buscarPacientes, listarPacientes, buscarPacientePorId, buscarPacientesPorIds, buscarMedicoPorId, buscarMedicosPorIds, type Paciente, buscarRelatorioPorId } from "@/lib/api"; import { useReports } from "@/hooks/useReports"; import { CreateReportData } from "@/types/report-types"; import { Button } from "@/components/ui/button"; @@ -149,6 +149,8 @@ const ProfissionalPage = () => { hide_date: true, hide_signature: true }); + + const [events, setEvents] = useState([ @@ -398,14 +400,7 @@ const ProfissionalPage = () => { > - +
{todayEvents.length} consulta{todayEvents.length !== 1 ? 's' : ''} agendada{todayEvents.length !== 1 ? 's' : ''} @@ -504,20 +499,18 @@ const ProfissionalPage = () => { const { reports, loadReports, loading: reportsLoading, createNewReport, updateExistingReport } = useReports(); const [laudos, setLaudos] = useState([]); - const [selectedRange, setSelectedRange] = useState<'todos'|'hoje'|'semana'|'mes'|'custom'>('mes'); + const [selectedRange, setSelectedRange] = useState<'todos'|'semana'|'mes'|'custom'>('mes'); const [startDate, setStartDate] = useState(null); const [endDate, setEndDate] = useState(null); // helper to check if a date string is in range - const isInRange = (dateStr: string | undefined, range: 'todos'|'hoje'|'semana'|'mes'|'custom') => { + const isInRange = (dateStr: string | undefined, range: 'todos'|'semana'|'mes'|'custom') => { if (range === 'todos') return true; if (!dateStr) return false; const d = new Date(dateStr); if (isNaN(d.getTime())) return false; const now = new Date(); - if (range === 'hoje') { - return d.toDateString() === now.toDateString(); - } + if (range === 'semana') { const start = new Date(now); start.setDate(now.getDate() - now.getDay()); // sunday start @@ -537,12 +530,7 @@ const ProfissionalPage = () => { setEndDate(null); return; } - if (selectedRange === 'hoje') { - const iso = now.toISOString().slice(0,10); - setStartDate(iso); - setEndDate(iso); - return; - } + if (selectedRange === 'semana') { const start = new Date(now); start.setDate(now.getDate() - now.getDay()); // sunday @@ -590,14 +578,6 @@ const ProfissionalPage = () => { > Todos - + +
+ + ); + } + // carregar laudos ao montar useEffect(() => { let mounted = true; @@ -683,13 +802,8 @@ const ProfissionalPage = () => {
- - - - + {/* Search input integrado com busca por ID */} +
@@ -707,11 +821,6 @@ const ProfissionalPage = () => {
{/* Filtros e pesquisa removidos por solicitação */} - -
@@ -760,7 +869,6 @@ const ProfissionalPage = () => {
- {getReportPatientId(laudo) || '-'}
{getReportPatientName(laudo) || '—'}
{getReportPatientCpf(laudo) ? `CPF: ${getReportPatientCpf(laudo)}` : ''}
@@ -818,7 +926,7 @@ const ProfissionalPage = () => {
{getReportPatientName(laudo) || '—'}
-
{getReportPatientCpf(laudo) ? `CPF: ${getReportPatientCpf(laudo)}` : getReportPatientId(laudo) || '-'}
+
{getReportPatientCpf(laudo) ? `CPF: ${getReportPatientCpf(laudo)}` : ''}
diff --git a/susconecta/lib/api.ts b/susconecta/lib/api.ts index ab1e1a3..0d7361c 100644 --- a/susconecta/lib/api.ts +++ b/susconecta/lib/api.ts @@ -397,6 +397,70 @@ export async function buscarPacientePorId(id: string | number): Promise { + const sId = String(id); + const headers = baseHeaders(); + + // 1) tenta por id (UUID ou campo id) + try { + const urlById = `${REST}/reports?id=eq.${encodeURIComponent(sId)}`; + console.debug('[buscarRelatorioPorId] tentando por id URL:', urlById); + const arr = await fetchWithFallback(urlById, headers); + if (arr && arr.length) return arr[0]; + } catch (e) { + console.warn('[buscarRelatorioPorId] falha ao buscar por id:', e); + } + + // 2) tenta por order_number (caso o usuário cole um código legível) + try { + const urlByOrder = `${REST}/reports?order_number=eq.${encodeURIComponent(sId)}`; + console.debug('[buscarRelatorioPorId] tentando por order_number URL:', urlByOrder); + const arr2 = await fetchWithFallback(urlByOrder, headers); + if (arr2 && arr2.length) return arr2[0]; + } catch (e) { + console.warn('[buscarRelatorioPorId] falha ao buscar por order_number:', e); + } + + // 3) tenta por patient_id (caso o usuário passe um patient_id em vez do report id) + try { + const urlByPatient = `${REST}/reports?patient_id=eq.${encodeURIComponent(sId)}`; + console.debug('[buscarRelatorioPorId] tentando por patient_id URL:', urlByPatient); + const arr3 = await fetchWithFallback(urlByPatient, headers); + if (arr3 && arr3.length) return arr3[0]; + } catch (e) { + console.warn('[buscarRelatorioPorId] falha ao buscar por patient_id:', e); + } + + // Não encontrado + throw new Error('404: Relatório não encontrado'); +} + + // Buscar vários pacientes por uma lista de IDs (usa query in.(...)) export async function buscarPacientesPorIds(ids: Array): Promise { if (!ids || !ids.length) return [];