189 lines
6.2 KiB
JavaScript
189 lines
6.2 KiB
JavaScript
import axios from "axios";
|
||
|
||
const SUPABASE_URL = "https://yuanqfswhberkoevtmfr.supabase.co";
|
||
const SUPABASE_ANON_KEY =
|
||
"eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
|
||
|
||
async function criarJuliaSecretaria() {
|
||
try {
|
||
console.log("═══════════════════════════════════════════════════");
|
||
console.log("🔐 CRIANDO JULIA CARVALHO - ROLE SECRETARIA");
|
||
console.log("═══════════════════════════════════════════════════\n");
|
||
|
||
// Login como admin
|
||
console.log("🔑 Login admin...");
|
||
const login = await axios.post(
|
||
`${SUPABASE_URL}/auth/v1/token?grant_type=password`,
|
||
{ email: "riseup@popcode.com.br", password: "riseup" },
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
"Content-Type": "application/json",
|
||
},
|
||
}
|
||
);
|
||
const adminToken = login.data.access_token;
|
||
console.log("✅ Admin logado!\n");
|
||
|
||
// Criar Julia
|
||
console.log("👤 Criando Julia...");
|
||
let juliaUserId;
|
||
let juliaToken;
|
||
|
||
try {
|
||
const signup = await axios.post(
|
||
`${SUPABASE_URL}/auth/v1/signup`,
|
||
{
|
||
email: "secretaria.mediconnect@gmail.com",
|
||
password: "secretaria@mediconnect",
|
||
data: { full_name: "Julia Carvalho", role: "secretaria" },
|
||
},
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
"Content-Type": "application/json",
|
||
},
|
||
}
|
||
);
|
||
juliaUserId = signup.data.user.id;
|
||
juliaToken = signup.data.access_token;
|
||
console.log("✅ Julia criada!");
|
||
} catch (err) {
|
||
if (err.response?.status === 422) {
|
||
console.log("⚠️ Julia já existe, fazendo login...");
|
||
const juliaLogin = await axios.post(
|
||
`${SUPABASE_URL}/auth/v1/token?grant_type=password`,
|
||
{
|
||
email: "secretaria.mediconnect@gmail.com",
|
||
password: "secretaria@mediconnect",
|
||
},
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
"Content-Type": "application/json",
|
||
},
|
||
}
|
||
);
|
||
juliaUserId = juliaLogin.data.user.id;
|
||
juliaToken = juliaLogin.data.access_token;
|
||
console.log("✅ Julia já existe!");
|
||
} else {
|
||
throw err;
|
||
}
|
||
}
|
||
|
||
console.log(` ID: ${juliaUserId}\n`);
|
||
|
||
// Criar/atualizar perfil
|
||
console.log("📋 Criando perfil...");
|
||
try {
|
||
await axios.post(
|
||
`${SUPABASE_URL}/rest/v1/profiles`,
|
||
{
|
||
id: juliaUserId,
|
||
email: "secretaria.mediconnect@gmail.com",
|
||
full_name: "Julia Carvalho",
|
||
is_admin: false,
|
||
is_secretary: true,
|
||
is_admin_or_manager: false,
|
||
},
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${adminToken}`,
|
||
"Content-Type": "application/json",
|
||
Prefer: "return=representation",
|
||
},
|
||
}
|
||
);
|
||
console.log("✅ Perfil criado!\n");
|
||
} catch (err) {
|
||
if (err.response?.status === 409) {
|
||
console.log("⚠️ Perfil existe, atualizando...");
|
||
await axios.patch(
|
||
`${SUPABASE_URL}/rest/v1/profiles?id=eq.${juliaUserId}`,
|
||
{
|
||
full_name: "Julia Carvalho",
|
||
is_admin: false,
|
||
is_secretary: true,
|
||
is_admin_or_manager: false,
|
||
},
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${adminToken}`,
|
||
"Content-Type": "application/json",
|
||
},
|
||
}
|
||
);
|
||
console.log("✅ Perfil atualizado!\n");
|
||
} else {
|
||
console.log(
|
||
"⚠️ Aviso perfil:",
|
||
err.response?.data?.message || err.message
|
||
);
|
||
}
|
||
}
|
||
|
||
// Adicionar role
|
||
console.log("🎭 Adicionando role secretaria...");
|
||
try {
|
||
await axios.post(
|
||
`${SUPABASE_URL}/rest/v1/user_roles`,
|
||
{ user_id: juliaUserId, role: "secretaria" },
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${adminToken}`,
|
||
"Content-Type": "application/json",
|
||
Prefer: "return=representation",
|
||
},
|
||
}
|
||
);
|
||
console.log("✅ Role adicionada!\n");
|
||
} catch (err) {
|
||
if (err.response?.status === 409) {
|
||
console.log("⚠️ Role já existe!\n");
|
||
} else {
|
||
console.log(
|
||
"⚠️ Aviso role:",
|
||
err.response?.data?.message || err.message
|
||
);
|
||
}
|
||
}
|
||
|
||
// Testar acesso
|
||
console.log("🏥 Testando acesso aos pacientes...");
|
||
const pacientes = await axios.get(
|
||
`${SUPABASE_URL}/rest/v1/patients?select=id,full_name,email&limit=3`,
|
||
{
|
||
headers: {
|
||
apikey: SUPABASE_ANON_KEY,
|
||
Authorization: `Bearer ${juliaToken}`,
|
||
},
|
||
}
|
||
);
|
||
console.log(`✅ Acesso OK! (${pacientes.data.length} pacientes)\n`);
|
||
|
||
console.log("═══════════════════════════════════════════════════");
|
||
console.log("✅ JULIA CRIADA COM SUCESSO!");
|
||
console.log("═══════════════════════════════════════════════════");
|
||
console.log("");
|
||
console.log("👤 Nome: Julia Carvalho");
|
||
console.log("📧 Email: secretaria.mediconnect@gmail.com");
|
||
console.log("🔑 Senha: secretaria@mediconnect");
|
||
console.log("🎭 Role: secretaria");
|
||
console.log("");
|
||
console.log("🌐 Login: http://localhost:5173/login-secretaria");
|
||
console.log("");
|
||
} catch (error) {
|
||
console.error("\n❌ ERRO:", error.response?.data || error.message);
|
||
if (error.code === "ENOTFOUND") {
|
||
console.log("\n⚠️ Problema de conexão com Supabase");
|
||
console.log("Use a página HTML: criar-julia.html");
|
||
}
|
||
}
|
||
}
|
||
|
||
criarJuliaSecretaria();
|