161 lines
4.7 KiB
JavaScript
161 lines
4.7 KiB
JavaScript
/**
|
||
* Verificar se um usuário/paciente foi criado na API
|
||
*/
|
||
|
||
import fetch from "node-fetch";
|
||
|
||
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
||
const SUPABASE_ANON_KEY =
|
||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
||
|
||
// Pegar email da linha de comando ou usar um padrão
|
||
const emailToSearch = process.argv[2] || "paciente.teste";
|
||
|
||
console.log("\n🔍 VERIFICANDO CRIAÇÃO DE USUÁRIO\n");
|
||
console.log("=".repeat(60));
|
||
console.log(`Buscando por: ${emailToSearch}`);
|
||
console.log("=".repeat(60));
|
||
|
||
async function checkProfiles() {
|
||
console.log("\n📋 Verificando tabela profiles...");
|
||
|
||
try {
|
||
const response = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/profiles?email=ilike.*${emailToSearch}*&select=*`,
|
||
{
|
||
method: "GET",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
const data = await response.json();
|
||
|
||
if (!response.ok) {
|
||
console.log(" ⚠️ Erro ao acessar profiles:", data);
|
||
return [];
|
||
}
|
||
|
||
console.log(` ✅ Encontrados ${data.length} registro(s) em profiles`);
|
||
data.forEach((profile, i) => {
|
||
console.log(`\n 👤 Usuário ${i + 1}:`);
|
||
console.log(` ID: ${profile.id}`);
|
||
console.log(` Nome: ${profile.full_name || profile.name}`);
|
||
console.log(` Email: ${profile.email}`);
|
||
console.log(
|
||
` Telefone: ${profile.phone_mobile || profile.phone || "N/A"}`
|
||
);
|
||
console.log(` Criado em: ${profile.created_at}`);
|
||
});
|
||
|
||
return data;
|
||
} catch (error) {
|
||
console.error(" ❌ Erro:", error.message);
|
||
return [];
|
||
}
|
||
}
|
||
|
||
async function checkPatients() {
|
||
console.log("\n📋 Verificando tabela patients...");
|
||
|
||
try {
|
||
const response = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/patients?email=ilike.*${emailToSearch}*&select=*`,
|
||
{
|
||
method: "GET",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
const data = await response.json();
|
||
|
||
if (!response.ok) {
|
||
console.log(" ⚠️ Erro ao acessar patients:", data);
|
||
return [];
|
||
}
|
||
|
||
console.log(` ✅ Encontrados ${data.length} registro(s) em patients`);
|
||
data.forEach((patient, i) => {
|
||
console.log(`\n 🏥 Paciente ${i + 1}:`);
|
||
console.log(` ID: ${patient.id}`);
|
||
console.log(` Nome: ${patient.full_name}`);
|
||
console.log(` Email: ${patient.email}`);
|
||
console.log(` CPF: ${patient.cpf || "N/A"}`);
|
||
console.log(` Telefone: ${patient.phone_mobile || "N/A"}`);
|
||
console.log(` Criado em: ${patient.created_at}`);
|
||
});
|
||
|
||
return data;
|
||
} catch (error) {
|
||
console.error(" ❌ Erro:", error.message);
|
||
return [];
|
||
}
|
||
}
|
||
|
||
async function checkUsers() {
|
||
console.log(
|
||
"\n📋 Tentando verificar auth.users (pode falhar por permissões)..."
|
||
);
|
||
|
||
try {
|
||
const response = await fetch(`${SUPABASE_URL}/auth/v1/admin/users`, {
|
||
method: "GET",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${SUPABASE_ANON_KEY}`,
|
||
},
|
||
});
|
||
|
||
const data = await response.json();
|
||
|
||
if (!response.ok) {
|
||
console.log(" ⚠️ Sem permissão para acessar auth.users (normal)");
|
||
return [];
|
||
}
|
||
|
||
const filtered =
|
||
data.users?.filter((u) => u.email?.includes(emailToSearch)) || [];
|
||
console.log(
|
||
` ✅ Encontrados ${filtered.length} usuário(s) em auth.users`
|
||
);
|
||
|
||
return filtered;
|
||
} catch (error) {
|
||
console.log(" ⚠️ Sem acesso a auth.users (normal para anon key)");
|
||
return [];
|
||
}
|
||
}
|
||
|
||
async function run() {
|
||
const profiles = await checkProfiles();
|
||
const patients = await checkPatients();
|
||
await checkUsers();
|
||
|
||
console.log("\n" + "=".repeat(60));
|
||
console.log("📊 RESUMO");
|
||
console.log("=".repeat(60));
|
||
console.log(`Registros em profiles: ${profiles.length}`);
|
||
console.log(`Registros em patients: ${patients.length}`);
|
||
|
||
if (profiles.length > 0 && patients.length > 0) {
|
||
console.log("\n✅ SUCESSO! Usuário criado em ambas as tabelas!");
|
||
} else if (profiles.length > 0) {
|
||
console.log("\n⚠️ Usuário criado em profiles, mas não em patients");
|
||
} else if (patients.length > 0) {
|
||
console.log("\n⚠️ Registro em patients, mas não em profiles");
|
||
} else {
|
||
console.log("\n❌ Nenhum registro encontrado");
|
||
}
|
||
console.log("=".repeat(60));
|
||
}
|
||
|
||
run();
|