feature/patiente-medical-assignment #45
@ -1,158 +0,0 @@
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { createClient } from '@supabase/supabase-js';
|
||||
|
||||
/**
|
||||
* Endpoint server-side para atribuir roles aos usuários
|
||||
* Usa SUPABASE_SERVICE_ROLE_KEY para realizar operações administrativas
|
||||
*
|
||||
* POST /api/assign-role
|
||||
* Body: { user_id: string, role: string }
|
||||
*/
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
// 1. Verificar autenticação do requisitante
|
||||
const authHeader = request.headers.get('authorization');
|
||||
if (!authHeader) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Unauthorized', message: 'Token de autenticação não fornecido' },
|
||||
{ status: 401 }
|
||||
);
|
||||
}
|
||||
|
||||
// 2. Extrair dados do body
|
||||
const body = await request.json();
|
||||
const { user_id, role } = body;
|
||||
|
||||
if (!user_id || !role) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Bad Request', message: 'user_id e role são obrigatórios' },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 3. Validar role
|
||||
const validRoles = ['admin', 'gestor', 'medico', 'secretaria', 'user'];
|
||||
if (!validRoles.includes(role)) {
|
||||
return NextResponse.json(
|
||||
{ error: 'Bad Request', message: `Role inválido. Valores aceitos: ${validRoles.join(', ')}` },
|
||||
{ status: 400 }
|
||||
);
|
||||
}
|
||||
|
||||
// 4. Obter service role key do ambiente
|
||||
const supabaseUrl = process.env.NEXT_PUBLIC_SUPABASE_URL;
|
||||
const serviceRoleKey = process.env.SUPABASE_SERVICE_ROLE_KEY;
|
||||
|
||||
if (!supabaseUrl || !serviceRoleKey) {
|
||||
console.error('❌ [ASSIGN-ROLE] SUPABASE_SERVICE_ROLE_KEY não configurada');
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Server Configuration Error',
|
||||
message: 'Service role key não configurada no servidor. Entre em contato com o administrador do sistema.',
|
||||
hint: 'Configure SUPABASE_SERVICE_ROLE_KEY nas variáveis de ambiente do servidor'
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
// 5. Criar cliente Supabase com service role key
|
||||
const supabaseAdmin = createClient(supabaseUrl, serviceRoleKey, {
|
||||
auth: {
|
||||
autoRefreshToken: false,
|
||||
persistSession: false,
|
||||
},
|
||||
});
|
||||
|
||||
// 6. Verificar se o usuário existe
|
||||
const { data: userData, error: userError } = await supabaseAdmin.auth.admin.getUserById(user_id);
|
||||
|
||||
if (userError || !userData) {
|
||||
console.error('❌ [ASSIGN-ROLE] Usuário não encontrado:', userError);
|
||||
return NextResponse.json(
|
||||
{ error: 'Not Found', message: 'Usuário não encontrado no sistema de autenticação' },
|
||||
{ status: 404 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`🔐 [ASSIGN-ROLE] Atribuindo role "${role}" ao usuário ${user_id}`);
|
||||
|
||||
// 7. Inserir role na tabela user_roles
|
||||
const { data: roleData, error: roleError } = await supabaseAdmin
|
||||
.from('user_roles')
|
||||
.insert({
|
||||
user_id: user_id,
|
||||
role: role,
|
||||
created_at: new Date().toISOString(),
|
||||
})
|
||||
.select()
|
||||
.single();
|
||||
|
||||
if (roleError) {
|
||||
// Verificar se é erro de duplicação (usuário já tem esse role)
|
||||
if (roleError.code === '23505') {
|
||||
console.log(`⚠️ [ASSIGN-ROLE] Usuário já possui o role "${role}"`);
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: `Usuário já possui o role "${role}"`,
|
||||
user_id,
|
||||
role,
|
||||
already_exists: true
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
}
|
||||
|
||||
console.error('❌ [ASSIGN-ROLE] Erro ao inserir role:', roleError);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Database Error',
|
||||
message: `Erro ao atribuir role: ${roleError.message}`,
|
||||
code: roleError.code,
|
||||
details: roleError.details
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
|
||||
console.log(`✅ [ASSIGN-ROLE] Role "${role}" atribuído com sucesso ao usuário ${user_id}`);
|
||||
|
||||
// 8. Retornar sucesso
|
||||
return NextResponse.json(
|
||||
{
|
||||
success: true,
|
||||
message: `Role "${role}" atribuído com sucesso`,
|
||||
data: roleData,
|
||||
user_id,
|
||||
role,
|
||||
},
|
||||
{ status: 200 }
|
||||
);
|
||||
|
||||
} catch (error: any) {
|
||||
console.error('❌ [ASSIGN-ROLE] Erro inesperado:', error);
|
||||
return NextResponse.json(
|
||||
{
|
||||
error: 'Internal Server Error',
|
||||
message: 'Erro inesperado ao atribuir role',
|
||||
details: error?.message || String(error)
|
||||
},
|
||||
{ status: 500 }
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// Método OPTIONS para CORS (se necessário)
|
||||
export async function OPTIONS(request: NextRequest) {
|
||||
return NextResponse.json(
|
||||
{},
|
||||
{
|
||||
status: 200,
|
||||
headers: {
|
||||
'Access-Control-Allow-Origin': '*',
|
||||
'Access-Control-Allow-Methods': 'POST, OPTIONS',
|
||||
'Access-Control-Allow-Headers': 'Content-Type, Authorization',
|
||||
},
|
||||
}
|
||||
);
|
||||
}
|
||||
@ -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, buscarPacientesPorIds, buscarMedicoPorId, buscarMedicosPorIds, type Paciente, buscarRelatorioPorId } from "@/lib/api";
|
||||
import { buscarPacientes, listarPacientes, buscarPacientePorId, buscarPacientesPorIds, buscarMedicoPorId, buscarMedicosPorIds, buscarMedicos, type Paciente, buscarRelatorioPorId, atualizarMedico } from "@/lib/api";
|
||||
import { useReports } from "@/hooks/useReports";
|
||||
import { CreateReportData } from "@/types/report-types";
|
||||
import { Button } from "@/components/ui/button";
|
||||
@ -48,17 +48,9 @@ const FullCalendar = dynamic(() => import("@fullcalendar/react"), {
|
||||
ssr: false,
|
||||
});
|
||||
|
||||
const pacientes = [
|
||||
{ nome: "Ana Souza", cpf: "123.456.789-00", idade: 42, statusLaudo: "Finalizado" },
|
||||
{ nome: "Bruno Lima", cpf: "987.654.321-00", idade: 33, statusLaudo: "Pendente" },
|
||||
{ nome: "Carla Menezes", cpf: "111.222.333-44", idade: 67, statusLaudo: "Rascunho" },
|
||||
];
|
||||
// pacientes will be loaded inside the component (hooks must run in component body)
|
||||
|
||||
const medico = {
|
||||
nome: "Dr. Carlos Andrade",
|
||||
identificacao: "CRM 000000 • Cardiologia e Dermatologia",
|
||||
fotoUrl: "",
|
||||
}
|
||||
// removed static medico placeholder; will load real profile for logged-in user
|
||||
|
||||
|
||||
const colorsByType = {
|
||||
@ -119,18 +111,112 @@ const ProfissionalPage = () => {
|
||||
|
||||
// Estados para o perfil do médico
|
||||
const [isEditingProfile, setIsEditingProfile] = useState(false);
|
||||
const [doctorId, setDoctorId] = useState<string | null>(null);
|
||||
// Removemos o placeholder extenso — inicializamos com valores minimalistas e vazios.
|
||||
const [profileData, setProfileData] = useState({
|
||||
nome: "Dr. Carlos Andrade",
|
||||
email: user?.email || "carlos.andrade@hospital.com",
|
||||
telefone: "(11) 99999-9999",
|
||||
endereco: "Rua das Flores, 123 - Centro",
|
||||
cidade: "São Paulo",
|
||||
cep: "01234-567",
|
||||
crm: "CRM 000000",
|
||||
especialidade: "Cardiologia e Dermatologia",
|
||||
biografia: "Médico especialista em cardiologia e dermatologia com mais de 15 anos de experiência em tratamentos clínicos e cirúrgicos."
|
||||
nome: '',
|
||||
email: user?.email || '',
|
||||
telefone: '',
|
||||
endereco: '',
|
||||
cidade: '',
|
||||
cep: '',
|
||||
crm: '',
|
||||
especialidade: '',
|
||||
// biografia field removed — not present in Medico records
|
||||
fotoUrl: ''
|
||||
});
|
||||
|
||||
// pacientes carregados dinamicamente (hooks devem ficar dentro do componente)
|
||||
const [pacientes, setPacientes] = useState<any[]>([]);
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
(async () => {
|
||||
try {
|
||||
if (!user || !user.id) {
|
||||
if (mounted) setPacientes([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const assignmentsMod = await import('@/lib/assignment');
|
||||
if (!assignmentsMod || typeof assignmentsMod.listAssignmentsForUser !== 'function') {
|
||||
if (mounted) setPacientes([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const assignments = await assignmentsMod.listAssignmentsForUser(user.id || '');
|
||||
const patientIds = Array.isArray(assignments) ? assignments.map((a:any) => String(a.patient_id)).filter(Boolean) : [];
|
||||
if (!patientIds.length) {
|
||||
if (mounted) setPacientes([]);
|
||||
return;
|
||||
}
|
||||
|
||||
const patients = await buscarPacientesPorIds(patientIds);
|
||||
const normalized = (patients || []).map((p: any) => ({
|
||||
...p,
|
||||
nome: p.full_name ?? (p as any).nome ?? '',
|
||||
cpf: p.cpf ?? '',
|
||||
idade: getPatientAge(p) // preencher idade para a tabela de pacientes
|
||||
}));
|
||||
if (mounted) setPacientes(normalized);
|
||||
} catch (err) {
|
||||
console.warn('[ProfissionalPage] falha ao carregar pacientes atribuídos:', err);
|
||||
if (mounted) setPacientes([]);
|
||||
}
|
||||
})();
|
||||
return () => { mounted = false; };
|
||||
}, [user?.id, doctorId]);
|
||||
|
||||
// Carregar perfil do médico correspondente ao usuário logado
|
||||
useEffect(() => {
|
||||
let mounted = true;
|
||||
(async () => {
|
||||
try {
|
||||
if (!user || !user.email) return;
|
||||
// Tenta buscar médicos pelo email do usuário (buscarMedicos lida com queries por email)
|
||||
const docs = await buscarMedicos(user.email);
|
||||
if (!mounted) return;
|
||||
if (Array.isArray(docs) && docs.length > 0) {
|
||||
// preferir registro cujo user_id bate com user.id
|
||||
let chosen = docs.find(d => String((d as any).user_id) === String(user.id)) || docs[0];
|
||||
if (chosen) {
|
||||
// store the doctor's id so we can update it later
|
||||
try { setDoctorId((chosen as any).id ?? null); } catch {};
|
||||
// Especialidade pode vir como 'specialty' (inglês), 'especialidade' (pt),
|
||||
// ou até uma lista/array. Normalizamos para string.
|
||||
const rawSpecialty = (chosen as any).specialty ?? (chosen as any).especialidade ?? (chosen as any).especialidades ?? (chosen as any).especiality;
|
||||
let specialtyStr = '';
|
||||
if (Array.isArray(rawSpecialty)) {
|
||||
specialtyStr = rawSpecialty.join(', ');
|
||||
} else if (rawSpecialty) {
|
||||
specialtyStr = String(rawSpecialty);
|
||||
}
|
||||
|
||||
// Foto pode vir como 'foto_url' ou 'fotoUrl' ou 'avatar_url'
|
||||
const foto = (chosen as any).foto_url || (chosen as any).fotoUrl || (chosen as any).avatar_url || '';
|
||||
|
||||
setProfileData((prev) => ({
|
||||
...prev,
|
||||
nome: (chosen as any).full_name || (chosen as any).nome_social || prev.nome || user?.email?.split('@')[0] || '',
|
||||
email: (chosen as any).email || user?.email || prev.email,
|
||||
telefone: (chosen as any).phone_mobile || (chosen as any).celular || (chosen as any).telefone || (chosen as any).phone || (chosen as any).mobile || (user as any)?.user_metadata?.phone || prev.telefone,
|
||||
endereco: (chosen as any).street || (chosen as any).endereco || prev.endereco,
|
||||
cidade: (chosen as any).city || (chosen as any).cidade || prev.cidade,
|
||||
cep: (chosen as any).cep || prev.cep,
|
||||
crm: (chosen as any).crm ? `CRM ${(chosen as any).crm}` : (prev.crm || ''),
|
||||
especialidade: specialtyStr || prev.especialidade || '',
|
||||
// biografia removed: prefer to ignore observacoes/curriculo_url here
|
||||
// (if needed elsewhere, render directly from chosen.observacoes)
|
||||
fotoUrl: foto || prev.fotoUrl || ''
|
||||
}));
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[ProfissionalPage] falha ao carregar perfil do médico pelo email:', e);
|
||||
}
|
||||
})();
|
||||
return () => { mounted = false; };
|
||||
}, [user?.id, user?.email]);
|
||||
|
||||
|
||||
|
||||
// Estados para campos principais da consulta
|
||||
@ -251,8 +337,37 @@ const ProfissionalPage = () => {
|
||||
};
|
||||
|
||||
const handleSaveProfile = () => {
|
||||
(async () => {
|
||||
if (!doctorId) {
|
||||
alert('Não foi possível localizar o registro do médico para atualizar.');
|
||||
setIsEditingProfile(false);
|
||||
return;
|
||||
}
|
||||
|
||||
// Build payload mapping UI fields to DB columns
|
||||
const payload: any = {};
|
||||
if (profileData.email) payload.email = profileData.email;
|
||||
if (profileData.telefone) payload.phone_mobile = profileData.telefone;
|
||||
if (profileData.endereco) payload.street = profileData.endereco;
|
||||
if (profileData.cidade) payload.city = profileData.cidade;
|
||||
if (profileData.cep) payload.cep = profileData.cep;
|
||||
if (profileData.especialidade) payload.specialty = profileData.especialidade || profileData.especialidade;
|
||||
if (profileData.fotoUrl) payload.foto_url = profileData.fotoUrl;
|
||||
|
||||
// Don't allow updating full_name or crm from this UI
|
||||
|
||||
try {
|
||||
const updated = await atualizarMedico(doctorId, payload as any);
|
||||
console.debug('[ProfissionalPage] médico atualizado:', updated);
|
||||
alert('Perfil atualizado com sucesso!');
|
||||
} catch (err: any) {
|
||||
console.error('[ProfissionalPage] falha ao atualizar médico:', err);
|
||||
// Mostrar mensagem amigável (o erro já é tratado em lib/api)
|
||||
alert(err?.message || 'Falha ao atualizar perfil. Verifique logs.');
|
||||
} finally {
|
||||
setIsEditingProfile(false);
|
||||
}
|
||||
})();
|
||||
};
|
||||
|
||||
const handleCancelEdit = () => {
|
||||
@ -756,7 +871,23 @@ const ProfissionalPage = () => {
|
||||
const reportsMod = await import('@/lib/reports');
|
||||
if (typeof reportsMod.listarRelatoriosPorPacientes === 'function') {
|
||||
const batch = await reportsMod.listarRelatoriosPorPacientes(patientIds);
|
||||
if (mounted) setLaudos(batch || []);
|
||||
// Enrich reports with paciente objects so UI shows name/cpf immediately
|
||||
const enriched = await (async (reportsArr: any[]) => {
|
||||
if (!reportsArr || !reportsArr.length) return reportsArr;
|
||||
const pids = reportsArr.map(r => String(getReportPatientId(r))).filter(Boolean);
|
||||
if (!pids.length) return reportsArr;
|
||||
try {
|
||||
const patients = await buscarPacientesPorIds(pids);
|
||||
const map = new Map((patients || []).map((p: any) => [String(p.id), p]));
|
||||
return reportsArr.map(r => {
|
||||
const pid = String(getReportPatientId(r));
|
||||
return { ...r, paciente: r.paciente ?? map.get(pid) ?? r.paciente };
|
||||
});
|
||||
} catch (e) {
|
||||
return reportsArr;
|
||||
}
|
||||
})(batch);
|
||||
if (mounted) setLaudos(enriched || []);
|
||||
} else {
|
||||
// fallback: 请求 por paciente individual
|
||||
const allReports: any[] = [];
|
||||
@ -768,7 +899,20 @@ const ProfissionalPage = () => {
|
||||
console.warn('[LaudoManager] falha ao carregar relatórios para paciente', pid, err);
|
||||
}
|
||||
}
|
||||
if (mounted) setLaudos(allReports);
|
||||
// enrich fallback results too
|
||||
const enrichedAll = await (async (reportsArr: any[]) => {
|
||||
if (!reportsArr || !reportsArr.length) return reportsArr;
|
||||
const pids = reportsArr.map(r => String(getReportPatientId(r))).filter(Boolean);
|
||||
if (!pids.length) return reportsArr;
|
||||
try {
|
||||
const patients = await buscarPacientesPorIds(pids);
|
||||
const map = new Map((patients || []).map((p: any) => [String(p.id), p]));
|
||||
return reportsArr.map(r => ({ ...r, paciente: r.paciente ?? map.get(String(getReportPatientId(r))) ?? r.paciente }));
|
||||
} catch (e) {
|
||||
return reportsArr;
|
||||
}
|
||||
})(allReports);
|
||||
if (mounted) setLaudos(enrichedAll);
|
||||
}
|
||||
} catch (err) {
|
||||
console.warn('[LaudoManager] erro ao carregar relatórios em batch, tentando por paciente individual', err);
|
||||
@ -781,7 +925,19 @@ const ProfissionalPage = () => {
|
||||
console.warn('[LaudoManager] falha ao carregar relatórios para paciente', pid, e);
|
||||
}
|
||||
}
|
||||
if (mounted) setLaudos(allReports);
|
||||
const enrichedAll = await (async (reportsArr: any[]) => {
|
||||
if (!reportsArr || !reportsArr.length) return reportsArr;
|
||||
const pids = reportsArr.map(r => String(getReportPatientId(r))).filter(Boolean);
|
||||
if (!pids.length) return reportsArr;
|
||||
try {
|
||||
const patients = await buscarPacientesPorIds(pids);
|
||||
const map = new Map((patients || []).map((p: any) => [String(p.id), p]));
|
||||
return reportsArr.map(r => ({ ...r, paciente: r.paciente ?? map.get(String(getReportPatientId(r))) ?? r.paciente }));
|
||||
} catch (e) {
|
||||
return reportsArr;
|
||||
}
|
||||
})(allReports);
|
||||
if (mounted) setLaudos(enrichedAll);
|
||||
}
|
||||
} catch (e) {
|
||||
console.warn('[LaudoManager] erro ao carregar laudos para pacientes atribuídos:', e);
|
||||
@ -1135,11 +1291,11 @@ const ProfissionalPage = () => {
|
||||
<div className="mt-8 text-center border-t pt-4">
|
||||
<div className="h-16 mb-2"></div>
|
||||
{(() => {
|
||||
const signatureName = laudo?.created_by_name ?? laudo?.createdByName ?? ((laudo?.created_by && user?.id && laudo.created_by === user.id) ? 'Squad-20' : medico.nome ?? 'Squad-20');
|
||||
const signatureName = laudo?.created_by_name ?? laudo?.createdByName ?? ((laudo?.created_by && user?.id && laudo.created_by === user.id) ? profileData.nome : (laudo?.created_by_name || profileData.nome));
|
||||
return (
|
||||
<>
|
||||
<p className="text-sm font-semibold">{signatureName}</p>
|
||||
<p className="text-xs text-muted-foreground">CRM 000000 - {laudo.especialidade}</p>
|
||||
<p className="text-xs text-muted-foreground">{profileData.crm || 'CRM não informado'} - {laudo.especialidade}</p>
|
||||
<p className="text-xs text-muted-foreground mt-1">Data: {formatReportDate(getReportDate(laudo))}</p>
|
||||
</>
|
||||
);
|
||||
@ -2255,20 +2411,7 @@ const ProfissionalPage = () => {
|
||||
)}
|
||||
</div>
|
||||
|
||||
<div className="space-y-2">
|
||||
<Label htmlFor="biografia">Biografia</Label>
|
||||
{isEditingProfile ? (
|
||||
<Textarea
|
||||
id="biografia"
|
||||
value={profileData.biografia}
|
||||
onChange={(e) => handleProfileChange('biografia', e.target.value)}
|
||||
rows={4}
|
||||
placeholder="Descreva sua experiência profissional..."
|
||||
/>
|
||||
) : (
|
||||
<p className="p-2 bg-muted/50 rounded min-h-[100px] text-foreground">{profileData.biografia}</p>
|
||||
)}
|
||||
</div>
|
||||
{/* Biografia removida: não é um campo no registro de médico */}
|
||||
</div>
|
||||
</div>
|
||||
|
||||
@ -2312,16 +2455,14 @@ const ProfissionalPage = () => {
|
||||
<TableHead>Nome</TableHead>
|
||||
<TableHead>CPF</TableHead>
|
||||
<TableHead>Idade</TableHead>
|
||||
<TableHead>Status do Laudo</TableHead>
|
||||
</TableRow>
|
||||
</TableHeader>
|
||||
<TableBody>
|
||||
{pacientes.map((paciente) => (
|
||||
<TableRow key={paciente.cpf}>
|
||||
<TableRow key={paciente.id ?? paciente.cpf}>
|
||||
<TableCell>{paciente.nome}</TableCell>
|
||||
<TableCell>{paciente.cpf}</TableCell>
|
||||
<TableCell>{paciente.idade}</TableCell>
|
||||
<TableCell>{paciente.statusLaudo}</TableCell>
|
||||
<TableCell>{getPatientAge(paciente) ? `${getPatientAge(paciente)} anos` : '-'}</TableCell>
|
||||
</TableRow>
|
||||
))}
|
||||
</TableBody>
|
||||
@ -2346,15 +2487,15 @@ const ProfissionalPage = () => {
|
||||
<header className="bg-card shadow-md rounded-lg border border-border p-4 mb-6 flex items-center justify-between">
|
||||
<div className="flex items-center gap-4">
|
||||
<Avatar className="h-12 w-12">
|
||||
<AvatarImage src={medico.fotoUrl} alt={medico.nome} />
|
||||
<AvatarImage src={(profileData as any).fotoUrl || undefined} alt={profileData.nome} />
|
||||
<AvatarFallback className="bg-muted">
|
||||
<User className="h-5 w-5" />
|
||||
</AvatarFallback>
|
||||
</Avatar>
|
||||
<div className="min-w-0">
|
||||
<p className="text-sm text-muted-foreground truncate">Conta do profissional</p>
|
||||
<h2 className="text-lg font-semibold leading-none truncate">{medico.nome}</h2>
|
||||
<p className="text-sm text-muted-foreground truncate">{medico.identificacao}</p>
|
||||
<h2 className="text-lg font-semibold leading-none truncate">{profileData.nome}</h2>
|
||||
<p className="text-sm text-muted-foreground truncate">{(profileData.crm ? profileData.crm : '') + (profileData.especialidade ? ` • ${profileData.especialidade}` : '')}</p>
|
||||
{user?.email && (
|
||||
<p className="text-xs text-muted-foreground truncate">Logado como: {user.email}</p>
|
||||
)}
|
||||
|
||||
@ -338,6 +338,7 @@ export async function buscarPacientes(termo: string): Promise<Paciente[]> {
|
||||
|
||||
const searchTerm = termo.toLowerCase().trim();
|
||||
const digitsOnly = searchTerm.replace(/\D/g, '');
|
||||
const q = encodeURIComponent(searchTerm);
|
||||
|
||||
// Monta queries para buscar em múltiplos campos
|
||||
const queries = [];
|
||||
@ -371,7 +372,11 @@ export async function buscarPacientes(termo: string): Promise<Paciente[]> {
|
||||
// Executa as buscas e combina resultados únicos
|
||||
for (const query of queries) {
|
||||
try {
|
||||
const url = `${ENV_CONFIG.SUPABASE_URL}/rest/v1/patients?${query}&limit=10`;
|
||||
const [key, val] = String(query).split('=');
|
||||
const params = new URLSearchParams();
|
||||
if (key && typeof val !== 'undefined') params.set(key, val);
|
||||
params.set('limit', '10');
|
||||
const url = `${REST}/patients?${params.toString()}`;
|
||||
const headers = baseHeaders();
|
||||
const masked = (headers['Authorization'] as string | undefined) ? `${String(headers['Authorization']).slice(0,6)}...${String(headers['Authorization']).slice(-6)}` : null;
|
||||
console.debug('[buscarPacientes] URL:', url);
|
||||
@ -412,8 +417,14 @@ export async function buscarPacientePorId(id: string | number): Promise<Paciente
|
||||
// Se for string e não numérico, talvez foi passado um nome — tentar por full_name / social_name
|
||||
if (typeof id === 'string' && isNaN(Number(id))) {
|
||||
const q = encodeURIComponent(String(id));
|
||||
const url = `${ENV_CONFIG.SUPABASE_URL}/rest/v1/patients?full_name=ilike.*${q}*&limit=5`;
|
||||
const alt = `${ENV_CONFIG.SUPABASE_URL}/rest/v1/patients?social_name=ilike.*${q}*&limit=5`;
|
||||
const params = new URLSearchParams();
|
||||
params.set('full_name', `ilike.*${String(id)}*`);
|
||||
params.set('limit', '5');
|
||||
const url = `${REST}/patients?${params.toString()}`;
|
||||
const altParams = new URLSearchParams();
|
||||
altParams.set('social_name', `ilike.*${String(id)}*`);
|
||||
altParams.set('limit', '5');
|
||||
const alt = `${REST}/patients?${altParams.toString()}`;
|
||||
console.debug('[buscarPacientePorId] tentando por nome URL:', url);
|
||||
const arr2 = await fetchWithFallback<Paciente[]>(url, headers, [alt]);
|
||||
if (arr2 && arr2.length) return arr2[0];
|
||||
@ -521,9 +532,14 @@ export async function buscarPacientesPorIds(ids: Array<string | number>): Promis
|
||||
// fazemos uma requisição por nome usando ilike para cada nome.
|
||||
for (const name of names) {
|
||||
try {
|
||||
const q = encodeURIComponent(name);
|
||||
const url = `${ENV_CONFIG.SUPABASE_URL}/rest/v1/patients?full_name=ilike.*${q}*&limit=100`;
|
||||
const alt = `${ENV_CONFIG.SUPABASE_URL}/rest/v1/patients?social_name=ilike.*${q}*&limit=100`;
|
||||
const params = new URLSearchParams();
|
||||
params.set('full_name', `ilike.*${name}*`);
|
||||
params.set('limit', '100');
|
||||
const url = `${REST}/patients?${params.toString()}`;
|
||||
const altParams = new URLSearchParams();
|
||||
altParams.set('social_name', `ilike.*${name}*`);
|
||||
altParams.set('limit', '100');
|
||||
const alt = `${REST}/patients?${altParams.toString()}`;
|
||||
const headers = baseHeaders();
|
||||
console.debug('[buscarPacientesPorIds] URL (patient by name):', url);
|
||||
const arr = await fetchWithFallback<Paciente[]>(url, headers, [alt]);
|
||||
@ -650,13 +666,15 @@ export async function buscarMedicos(termo: string): Promise<Medico[]> {
|
||||
|
||||
const searchTerm = termo.toLowerCase().trim();
|
||||
const digitsOnly = searchTerm.replace(/\D/g, '');
|
||||
// Do not pre-encode the searchTerm here; we'll let URLSearchParams handle encoding
|
||||
const q = searchTerm;
|
||||
|
||||
// Monta queries para buscar em múltiplos campos
|
||||
const queries = [];
|
||||
|
||||
// Busca por ID se parece com UUID
|
||||
if (searchTerm.includes('-') && searchTerm.length > 10) {
|
||||
queries.push(`id=eq.${searchTerm}`);
|
||||
queries.push(`id=eq.${encodeURIComponent(searchTerm)}`);
|
||||
}
|
||||
|
||||
// Busca por CRM (com e sem formatação)
|
||||
@ -666,29 +684,43 @@ export async function buscarMedicos(termo: string): Promise<Medico[]> {
|
||||
|
||||
// Busca por nome (usando ilike para busca case-insensitive)
|
||||
if (searchTerm.length >= 2) {
|
||||
queries.push(`full_name=ilike.*${searchTerm}*`);
|
||||
queries.push(`nome_social=ilike.*${searchTerm}*`);
|
||||
queries.push(`full_name=ilike.*${q}*`);
|
||||
queries.push(`nome_social=ilike.*${q}*`);
|
||||
}
|
||||
|
||||
// Busca por email se contém @
|
||||
if (searchTerm.includes('@')) {
|
||||
queries.push(`email=ilike.*${searchTerm}*`);
|
||||
// Quando o usuário pesquisa por email (contendo '@'), limitar as queries apenas ao campo email.
|
||||
// Em alguns esquemas de banco / views, buscar por outros campos com um email pode provocar
|
||||
// erros de requisição (400) dependendo das colunas e políticas. Reduzimos o escopo para evitar 400s.
|
||||
queries.length = 0; // limpar queries anteriores
|
||||
queries.push(`email=ilike.*${q}*`);
|
||||
}
|
||||
|
||||
// Busca por especialidade
|
||||
if (searchTerm.length >= 2) {
|
||||
queries.push(`specialty=ilike.*${searchTerm}*`);
|
||||
queries.push(`specialty=ilike.*${q}*`);
|
||||
}
|
||||
|
||||
// debug: mostrar queries construídas
|
||||
console.debug('[buscarMedicos] queries construídas:', queries);
|
||||
|
||||
const results: Medico[] = [];
|
||||
const seenIds = new Set<string>();
|
||||
|
||||
// Executa as buscas e combina resultados únicos
|
||||
for (const query of queries) {
|
||||
try {
|
||||
const url = `${REST}/doctors?${query}&limit=10`;
|
||||
// Build the URL safely using URLSearchParams so special characters (like @) are encoded correctly
|
||||
// query is like 'nome_social=ilike.*something*' -> split into key/value
|
||||
const [key, val] = String(query).split('=');
|
||||
const params = new URLSearchParams();
|
||||
if (key && typeof val !== 'undefined') params.set(key, val);
|
||||
params.set('limit', '10');
|
||||
const url = `${REST}/doctors?${params.toString()}`;
|
||||
const headers = baseHeaders();
|
||||
const masked = (headers['Authorization'] as string | undefined) ? `${String(headers['Authorization']).slice(0,6)}...${String(headers['Authorization']).slice(-6)}` : null;
|
||||
console.debug('[buscarMedicos] URL params:', params.toString());
|
||||
console.debug('[buscarMedicos] URL:', url);
|
||||
console.debug('[buscarMedicos] Headers (masked):', { ...headers, Authorization: masked ? '<<masked>>' : undefined });
|
||||
const res = await fetch(url, { method: 'GET', headers });
|
||||
@ -734,9 +766,19 @@ export async function buscarMedicoPorId(id: string | number): Promise<Medico | n
|
||||
// tentar por full_name usando ilike para evitar 400 com espaços/caracteres
|
||||
try {
|
||||
const q = encodeURIComponent(sId);
|
||||
const url = `${REST}/doctors?full_name=ilike.*${q}*&limit=5`;
|
||||
const alt = `${REST}/doctors?nome_social=ilike.*${q}*&limit=5`;
|
||||
const arr = await fetchWithFallback<Medico[]>(url, baseHeaders(), [alt, `${REST}/doctors?social_name=ilike.*${q}*&limit=5`]);
|
||||
const params = new URLSearchParams();
|
||||
params.set('full_name', `ilike.*${q}*`);
|
||||
params.set('limit', '5');
|
||||
const url = `${REST}/doctors?${params.toString()}`;
|
||||
const altParams = new URLSearchParams();
|
||||
altParams.set('nome_social', `ilike.*${q}*`);
|
||||
altParams.set('limit', '5');
|
||||
const alt = `${REST}/doctors?${altParams.toString()}`;
|
||||
const socialAltParams = new URLSearchParams();
|
||||
socialAltParams.set('social_name', `ilike.*${sId}*`);
|
||||
socialAltParams.set('limit', '5');
|
||||
const socialAlt = `${REST}/doctors?${socialAltParams.toString()}`;
|
||||
const arr = await fetchWithFallback<Medico[]>(url, baseHeaders(), [alt, socialAlt]);
|
||||
if (arr && arr.length > 0) return arr[0];
|
||||
} catch (e) {
|
||||
// ignore and try next
|
||||
@ -813,12 +855,21 @@ export async function buscarMedicosPorIds(ids: Array<string | number>): Promise<
|
||||
// Evitar in.(...) com aspas — fazer uma requisição por nome usando ilike
|
||||
for (const name of names) {
|
||||
try {
|
||||
const q = encodeURIComponent(name);
|
||||
const url = `${REST}/doctors?full_name=ilike.*${q}*&limit=200`;
|
||||
const alt = `${REST}/doctors?nome_social=ilike.*${q}*&limit=200`;
|
||||
const params = new URLSearchParams();
|
||||
params.set('full_name', `ilike.*${name}*`);
|
||||
params.set('limit', '200');
|
||||
const url = `${REST}/doctors?${params.toString()}`;
|
||||
const altParams = new URLSearchParams();
|
||||
altParams.set('nome_social', `ilike.*${name}*`);
|
||||
altParams.set('limit', '200');
|
||||
const alt = `${REST}/doctors?${altParams.toString()}`;
|
||||
const headers = baseHeaders();
|
||||
console.debug('[buscarMedicosPorIds] URL (doctor by name):', url);
|
||||
const arr = await fetchWithFallback<Medico[]>(url, headers, [alt, `${REST}/doctors?social_name=ilike.*${q}*&limit=200`]);
|
||||
const socialAltParams = new URLSearchParams();
|
||||
socialAltParams.set('social_name', `ilike.*${name}*`);
|
||||
socialAltParams.set('limit', '200');
|
||||
const socialAlt = `${REST}/doctors?${socialAltParams.toString()}`;
|
||||
const arr = await fetchWithFallback<Medico[]>(url, headers, [alt, socialAlt]);
|
||||
if (arr && arr.length) results.push(...arr);
|
||||
} catch (e) {
|
||||
// ignore
|
||||
@ -1546,9 +1597,14 @@ export async function buscarPerfilPorId(id: string | number): Promise<Profile> {
|
||||
|
||||
// 2) tentar por full_name quando for string legível
|
||||
if (typeof id === 'string' && isNaN(Number(id))) {
|
||||
const q = encodeURIComponent(String(id));
|
||||
const url = `${REST}/profiles?full_name=ilike.*${q}*&limit=5`;
|
||||
const alt = `${REST}/profiles?email=ilike.*${q}*&limit=5`;
|
||||
const params = new URLSearchParams();
|
||||
params.set('full_name', `ilike.*${String(id)}*`);
|
||||
params.set('limit', '5');
|
||||
const url = `${REST}/profiles?${params.toString()}`;
|
||||
const altParams = new URLSearchParams();
|
||||
altParams.set('email', `ilike.*${String(id)}*`);
|
||||
altParams.set('limit', '5');
|
||||
const alt = `${REST}/profiles?${altParams.toString()}`;
|
||||
const arr2 = await fetchWithFallback<Profile[]>(url, headers, [alt]);
|
||||
if (arr2 && arr2.length) return arr2[0];
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user