171 lines
4.9 KiB
JavaScript
171 lines
4.9 KiB
JavaScript
// Script para criar atribuições de pacientes para o Fernando
|
||
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
||
const SUPABASE_ANON_KEY =
|
||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
||
|
||
// Admin credentials
|
||
const ADMIN_EMAIL = "riseup@popcode.com.br";
|
||
const ADMIN_PASSWORD = "riseup";
|
||
|
||
// Fernando user ID (do teste anterior)
|
||
const FERNANDO_USER_ID = "be1e3cba-534e-48c3-9590-b7e55861cade";
|
||
|
||
// IDs dos pacientes (do teste anterior)
|
||
const PACIENTES = [
|
||
{
|
||
id: "27aff771-8297-4ab2-8886-de8cf09c3895",
|
||
nome: "Isaac Kauã Barrozo Oliveira",
|
||
},
|
||
{
|
||
id: "5236952f-efdd-4af6-b94b-0b28a89cb06c",
|
||
nome: "João Pedro Lima dos Santos",
|
||
},
|
||
{
|
||
id: "7ddbd1e2-1aee-4f7a-94f9-ee4c735ca276",
|
||
nome: "Gabriel Nascimento Correia",
|
||
},
|
||
{ id: "1f5ac462-faf1-4290-ac55-d1900afb074e", nome: "Danilo Santos" },
|
||
{
|
||
id: "cf835709-616f-428f-8055-1acf53ee24bb",
|
||
nome: "Jonas Francisco Nascimento Bonfim",
|
||
},
|
||
];
|
||
|
||
async function criarAtribuicoes() {
|
||
try {
|
||
console.log("\n🔐 === CRIAR ATRIBUIÇÕES PARA FERNANDO ===\n");
|
||
|
||
// 1. Login como admin
|
||
console.log("1️⃣ Fazendo login como admin...");
|
||
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: ADMIN_EMAIL,
|
||
password: ADMIN_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 adminUserId = loginData.user.id;
|
||
|
||
console.log(`✅ Login admin realizado!`);
|
||
console.log(` Admin User ID: ${adminUserId}`);
|
||
|
||
// 2. Criar atribuições para cada paciente
|
||
console.log(
|
||
`\n2️⃣ Criando atribuições para Fernando (${FERNANDO_USER_ID})...\n`
|
||
);
|
||
|
||
let sucessos = 0;
|
||
let erros = 0;
|
||
|
||
for (let i = 0; i < PACIENTES.length; i++) {
|
||
const paciente = PACIENTES[i];
|
||
console.log(
|
||
` [${i + 1}/${PACIENTES.length}] Atribuindo: ${paciente.nome}...`
|
||
);
|
||
|
||
try {
|
||
const atribuicao = {
|
||
patient_id: paciente.id,
|
||
user_id: FERNANDO_USER_ID,
|
||
role: "medico",
|
||
created_by: adminUserId,
|
||
};
|
||
|
||
const response = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/patient_assignments`,
|
||
{
|
||
method: "POST",
|
||
headers: {
|
||
"Content-Type": "application/json",
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${accessToken}`,
|
||
Prefer: "return=representation",
|
||
},
|
||
body: JSON.stringify(atribuicao),
|
||
}
|
||
);
|
||
|
||
if (!response.ok) {
|
||
const errorText = await response.text();
|
||
throw new Error(`${response.status} - ${errorText}`);
|
||
}
|
||
|
||
const result = await response.json();
|
||
console.log(
|
||
` ✅ Sucesso! Assignment ID: ${
|
||
result[0]?.id || result.id || "N/A"
|
||
}`
|
||
);
|
||
sucessos++;
|
||
} catch (error) {
|
||
console.error(
|
||
` ❌ Erro:`,
|
||
error instanceof Error ? error.message : error
|
||
);
|
||
erros++;
|
||
}
|
||
}
|
||
|
||
// 3. Verificar atribuições criadas
|
||
console.log(`\n3️⃣ Verificando atribuições criadas...\n`);
|
||
|
||
const verificarResponse = await fetch(
|
||
`${SUPABASE_URL}/rest/v1/patient_assignments?user_id=eq.${FERNANDO_USER_ID}&select=*`,
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${accessToken}`,
|
||
},
|
||
}
|
||
);
|
||
|
||
if (verificarResponse.ok) {
|
||
const assignments = await verificarResponse.json();
|
||
console.log(`✅ Total de atribuições do Fernando: ${assignments.length}`);
|
||
|
||
assignments.forEach((a, i) => {
|
||
console.log(` ${i + 1}. Patient: ${a.patient_id} | Role: ${a.role}`);
|
||
});
|
||
}
|
||
|
||
// 4. Resumo
|
||
console.log(`\n📊 === RESUMO ===`);
|
||
console.log(` ✅ Sucessos: ${sucessos}`);
|
||
console.log(` ❌ Erros: ${erros}`);
|
||
console.log(` 📋 Total tentados: ${PACIENTES.length}`);
|
||
|
||
if (sucessos > 0) {
|
||
console.log(
|
||
`\n🎉 Fernando agora pode ver ${sucessos} pacientes no painel médico!`
|
||
);
|
||
console.log(
|
||
` Faça login com: fernando.pirichowski@souunit.com.br / fernando`
|
||
);
|
||
}
|
||
} catch (error) {
|
||
console.error("\n❌ Erro geral:", error);
|
||
if (error instanceof Error) {
|
||
console.error(" Mensagem:", error.message);
|
||
}
|
||
}
|
||
}
|
||
|
||
// Executar
|
||
criarAtribuicoes();
|