179 lines
5.8 KiB
JavaScript
179 lines
5.8 KiB
JavaScript
// Script para testar patient_assignments do Fernando
|
||
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
||
const SUPABASE_ANON_KEY =
|
||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
||
|
||
// Credenciais do Fernando
|
||
const FERNANDO_EMAIL = "fernando.pirichowski@souunit.com.br";
|
||
const FERNANDO_PASSWORD = "fernando";
|
||
|
||
async function testarAtribuicoes() {
|
||
try {
|
||
console.log("\n🔐 === TESTE DE PATIENT_ASSIGNMENTS ===\n");
|
||
|
||
// 1. Login do Fernando
|
||
console.log("1️⃣ Fazendo login com Fernando...");
|
||
const loginResponse = await fetch(
|
||
`${SUPABASE_URL}/auth/v1/token?grant_type=password`,
|
||
{
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
apikey: SUPABASE_ANON_KEY,
|
||
},
|
||
body: JSON.stringify({
|
||
email: FERNANDO_EMAIL,
|
||
password: FERNANDO_PASSWORD,
|
||
}),
|
||
}
|
||
);
|
||
|
||
if (!loginResponse.ok) {
|
||
throw new Error(
|
||
`Erro no login: ${loginResponse.status} - ${await loginResponse.text()}`
|
||
);
|
||
}
|
||
|
||
const loginData = await loginResponse.json();
|
||
const accessToken = loginData.access_token;
|
||
const fernandoUserId = loginData.user.id;
|
||
|
||
console.log(`✅ Login realizado com sucesso!`);
|
||
console.log(` User ID: ${fernandoUserId}`);
|
||
console.log(` Email: ${loginData.user.email}`);
|
||
|
||
// 2. Buscar perfil do Fernando
|
||
console.log("\n2️⃣ Buscando perfil no profiles...");
|
||
const profileResponse = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/profiles?id=eq.${fernandoUserId}&select=*`,
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${accessToken}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
if (!profileResponse.ok) {
|
||
throw new Error(`Erro ao buscar perfil: ${profileResponse.status}`);
|
||
}
|
||
|
||
const profiles = await profileResponse.json();
|
||
if (profiles.length > 0) {
|
||
console.log(
|
||
`✅ Perfil encontrado: ${
|
||
profiles[0].full_name || profiles[0].name || "Sem nome"
|
||
}`
|
||
);
|
||
}
|
||
|
||
// 3. Buscar atribuições do Fernando
|
||
console.log("\n3️⃣ Buscando patient_assignments...");
|
||
console.log(` Query: user_id=eq.${fernandoUserId}&role=eq.medico`);
|
||
|
||
const assignmentsResponse = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/patient_assignments?user_id=eq.${fernandoUserId}&role=eq.medico&select=*`,
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${accessToken}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
if (!assignmentsResponse.ok) {
|
||
const errorText = await assignmentsResponse.text();
|
||
throw new Error(
|
||
`Erro ao buscar atribuições: ${assignmentsResponse.status} - ${errorText}`
|
||
);
|
||
}
|
||
|
||
const assignments = await assignmentsResponse.json();
|
||
console.log(`✅ ${assignments.length} atribuições encontradas!`);
|
||
|
||
if (assignments.length === 0) {
|
||
console.log(
|
||
"\n⚠️ Fernando NÃO tem atribuições na tabela patient_assignments!"
|
||
);
|
||
console.log(
|
||
" Isso significa que ele não conseguirá ver pacientes no painel médico."
|
||
);
|
||
console.log("\n💡 Solução:");
|
||
console.log(" 1. Criar atribuições manualmente no Supabase");
|
||
console.log(" 2. OU usar o script criar-atribuicao-fernando.js");
|
||
} else {
|
||
console.log("\n📋 Atribuições encontradas:");
|
||
assignments.forEach((a, i) => {
|
||
console.log(`\n ${i + 1}. Atribuição ID: ${a.id}`);
|
||
console.log(` Patient ID: ${a.patient_id}`);
|
||
console.log(` Role: ${a.role}`);
|
||
console.log(` Created At: ${a.created_at}`);
|
||
});
|
||
|
||
// 4. Buscar detalhes dos pacientes atribuídos
|
||
console.log("\n4️⃣ Buscando detalhes dos pacientes...");
|
||
|
||
for (let i = 0; i < assignments.length; i++) {
|
||
const assignment = assignments[i];
|
||
const patientResponse = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/patients?id=eq.${assignment.patient_id}&select=id,full_name,email,phone_mobile`,
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${accessToken}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
if (patientResponse.ok) {
|
||
const patients = await patientResponse.json();
|
||
if (patients.length > 0) {
|
||
const p = patients[0];
|
||
console.log(` ${i + 1}. ${p.full_name || "Sem nome"}`);
|
||
console.log(` Email: ${p.email || "N/A"}`);
|
||
console.log(` Tel: ${p.phone_mobile || "N/A"}`);
|
||
} else {
|
||
console.log(
|
||
` ${i + 1}. ⚠️ Paciente ${
|
||
assignment.patient_id
|
||
} não encontrado!`
|
||
);
|
||
}
|
||
}
|
||
}
|
||
}
|
||
|
||
// 5. Listar TODOS os pacientes (para referência)
|
||
console.log("\n5️⃣ Listando TODOS os pacientes (para referência)...");
|
||
const allPatientsResponse = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/patients?select=id,full_name&limit=10`,
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${accessToken}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
if (allPatientsResponse.ok) {
|
||
const allPatients = await allPatientsResponse.json();
|
||
console.log(
|
||
`📊 Total de pacientes no sistema: ${allPatients.length} (primeiros 10)`
|
||
);
|
||
allPatients.forEach((p, i) => {
|
||
console.log(` ${i + 1}. ${p.full_name} (${p.id})`);
|
||
});
|
||
}
|
||
|
||
console.log("\n✅ Teste concluído!");
|
||
} catch (error) {
|
||
console.error("\n❌ Erro no teste:", error);
|
||
if (error instanceof Error) {
|
||
console.error(" Mensagem:", error.message);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Executar
|
||
testarAtribuicoes();
|