develop #83
@ -5,7 +5,7 @@ import { useRouter } from 'next/navigation';
|
|||||||
import ProtectedRoute from '@/components/shared/ProtectedRoute';
|
import ProtectedRoute from '@/components/shared/ProtectedRoute';
|
||||||
import { useAuth } from '@/hooks/useAuth';
|
import { useAuth } from '@/hooks/useAuth';
|
||||||
import { useToast } from '@/hooks/use-toast';
|
import { useToast } from '@/hooks/use-toast';
|
||||||
import { listarPacientes, buscarMedicos } from '@/lib/api';
|
import { listarPacientes, buscarMedicos, getUserInfo } from '@/lib/api';
|
||||||
import { useReports } from '@/hooks/useReports';
|
import { useReports } from '@/hooks/useReports';
|
||||||
import { Button } from '@/components/ui/button';
|
import { Button } from '@/components/ui/button';
|
||||||
import { Input } from '@/components/ui/input';
|
import { Input } from '@/components/ui/input';
|
||||||
@ -186,25 +186,49 @@ export default function LaudosEditorPage() {
|
|||||||
// Se já temos um nome razoável, não sobrescrever
|
// Se já temos um nome razoável, não sobrescrever
|
||||||
if (solicitanteNome && solicitanteNome.trim().length > 1) return;
|
if (solicitanteNome && solicitanteNome.trim().length > 1) return;
|
||||||
if (!user) return;
|
if (!user) return;
|
||||||
// Buscar médicos por email (buscarMedicos aceita termos com @ e faz a busca por email)
|
|
||||||
if (user.email && user.email.includes('@')) {
|
// First try: query doctors index with any available identifier (email, id or username)
|
||||||
const docs = await buscarMedicos(user.email).catch(() => []);
|
try {
|
||||||
|
const term = (user.email && user.email.trim()) || user.name || user.id || '';
|
||||||
|
if (term && term.length > 1) {
|
||||||
|
const docs = await buscarMedicos(term).catch(() => []);
|
||||||
|
if (!mounted) return;
|
||||||
|
if (Array.isArray(docs) && docs.length > 0) {
|
||||||
|
const d = docs[0];
|
||||||
|
if (d && (d.full_name || (d as any).nome)) {
|
||||||
|
setSolicitanteNome((d.full_name as string) || ((d as any).nome as string) || user.name || user.email || '');
|
||||||
|
setSolicitanteId(user.id || solicitanteId);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// non-fatal, continue to next fallback
|
||||||
|
}
|
||||||
|
|
||||||
|
// Second try: fetch consolidated user-info (may contain profile.full_name)
|
||||||
|
try {
|
||||||
|
const info = await getUserInfo().catch(() => null);
|
||||||
if (!mounted) return;
|
if (!mounted) return;
|
||||||
if (Array.isArray(docs) && docs.length > 0) {
|
if (info && (info.profile as any)?.full_name) {
|
||||||
const d = docs[0];
|
const full = (info.profile as any).full_name as string;
|
||||||
// Preferir full_name do médico quando disponível
|
if (full && full.trim().length > 1) {
|
||||||
if (d && (d.full_name || (d as any).nome)) {
|
setSolicitanteNome(full);
|
||||||
setSolicitanteNome((d.full_name as string) || ((d as any).nome as string) || user.name || user.email || '');
|
setSolicitanteId(user.id || solicitanteId);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (err) {
|
||||||
|
// ignore and fallback
|
||||||
}
|
}
|
||||||
|
|
||||||
// Fallbacks: usar user.name se existir; caso contrário, email completo
|
// Final fallback: use name from auth user or email/username
|
||||||
setSolicitanteNome(user.name || user.email || '');
|
setSolicitanteNome(user.name || user.email || '');
|
||||||
|
setSolicitanteId(user.id || solicitanteId);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
// em caso de erro, manter o fallback
|
// em caso de erro, manter o fallback
|
||||||
setSolicitanteNome(user?.name || user?.email || '');
|
setSolicitanteNome(user?.name || user?.email || '');
|
||||||
|
setSolicitanteId(user?.id || solicitanteId);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user