122 lines
4.2 KiB
JavaScript
122 lines
4.2 KiB
JavaScript
const axios = require('axios');
|
|
|
|
const SUPABASE_URL = 'https://yuanqfswhberkoevtmfr.supabase.co';
|
|
const SUPABASE_ANON_KEY = 'eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ';
|
|
|
|
async function createAppointment() {
|
|
try {
|
|
console.log('🔐 Fazendo login como Aurora...');
|
|
|
|
// Login como Aurora
|
|
const loginResponse = await axios.post(
|
|
`${SUPABASE_URL}/auth/v1/token?grant_type=password`,
|
|
{
|
|
email: 'aurora-nascimento94@gmx.com',
|
|
password: 'auroranasc94'
|
|
},
|
|
{
|
|
headers: {
|
|
'apikey': SUPABASE_ANON_KEY,
|
|
'Content-Type': 'application/json'
|
|
}
|
|
}
|
|
);
|
|
|
|
const auroraToken = loginResponse.data.access_token;
|
|
console.log('✅ Login realizado como Aurora');
|
|
|
|
// Buscar o patient_id da Aurora
|
|
console.log('\n👤 Buscando dados da paciente...');
|
|
const patientResponse = await axios.get(
|
|
`${SUPABASE_URL}/rest/v1/patients?user_id=eq.${loginResponse.data.user.id}`,
|
|
{
|
|
headers: {
|
|
'apikey': SUPABASE_ANON_KEY,
|
|
'Authorization': `Bearer ${auroraToken}`
|
|
}
|
|
}
|
|
);
|
|
|
|
if (!patientResponse.data || patientResponse.data.length === 0) {
|
|
throw new Error('Paciente não encontrada');
|
|
}
|
|
|
|
const patientId = patientResponse.data[0].id;
|
|
console.log(`✅ Patient ID: ${patientId}`);
|
|
|
|
// Buscar disponibilidade do Dr. Fernando para segunda-feira
|
|
console.log('\n📅 Buscando disponibilidade do Dr. Fernando...');
|
|
const availabilityResponse = await axios.get(
|
|
`${SUPABASE_URL}/rest/v1/doctor_availability?doctor_id=eq.6dad001d-229b-40b5-80f3-310243c4599c&weekday=eq.monday`,
|
|
{
|
|
headers: {
|
|
'apikey': SUPABASE_ANON_KEY,
|
|
'Authorization': `Bearer ${auroraToken}`
|
|
}
|
|
}
|
|
);
|
|
|
|
if (!availabilityResponse.data || availabilityResponse.data.length === 0) {
|
|
throw new Error('Disponibilidade não encontrada');
|
|
}
|
|
|
|
const availability = availabilityResponse.data[0];
|
|
console.log(`✅ Disponibilidade encontrada: ${availability.start_time} - ${availability.end_time}`);
|
|
|
|
// Criar consulta para próxima segunda-feira às 10:00
|
|
const today = new Date();
|
|
const daysUntilMonday = (1 - today.getDay() + 7) % 7 || 7; // Próxima segunda
|
|
const appointmentDate = new Date(today);
|
|
appointmentDate.setDate(today.getDate() + daysUntilMonday);
|
|
appointmentDate.setHours(10, 0, 0, 0);
|
|
|
|
const scheduledAt = appointmentDate.toISOString();
|
|
|
|
console.log(`\n📝 Criando consulta para ${scheduledAt}...`);
|
|
|
|
const appointmentData = {
|
|
patient_id: patientId,
|
|
doctor_id: '6dad001d-229b-40b5-80f3-310243c4599c',
|
|
scheduled_at: scheduledAt,
|
|
duration_minutes: 30,
|
|
appointment_type: 'presencial',
|
|
chief_complaint: 'Consulta de rotina'
|
|
};
|
|
|
|
const appointmentResponse = await axios.post(
|
|
`${SUPABASE_URL}/rest/v1/appointments`,
|
|
appointmentData,
|
|
{
|
|
headers: {
|
|
'apikey': SUPABASE_ANON_KEY,
|
|
'Authorization': `Bearer ${auroraToken}`,
|
|
'Content-Type': 'application/json',
|
|
'Prefer': 'return=representation'
|
|
}
|
|
}
|
|
);
|
|
|
|
console.log('\n✅ Consulta criada com sucesso!');
|
|
console.log('\n📋 Detalhes da consulta:');
|
|
console.log(` - Paciente: Aurora Sabrina Clara Nascimento`);
|
|
console.log(` - Médico: Dr. Fernando Pirichowski`);
|
|
console.log(` - Data/Hora: ${scheduledAt}`);
|
|
console.log(` - Duração: 30 minutos`);
|
|
console.log(` - Tipo: presencial`);
|
|
|
|
if (appointmentResponse.data && appointmentResponse.data.length > 0) {
|
|
console.log(` - ID da consulta: ${appointmentResponse.data[0].id}`);
|
|
console.log(` - Order Number: ${appointmentResponse.data[0].order_number}`);
|
|
console.log(` - Status: ${appointmentResponse.data[0].status}`);
|
|
}
|
|
|
|
} catch (error) {
|
|
console.error('❌ Erro ao criar consulta:', error.response?.data || error.message);
|
|
if (error.response?.data) {
|
|
console.error('Detalhes:', JSON.stringify(error.response.data, null, 2));
|
|
}
|
|
}
|
|
}
|
|
|
|
createAppointment();
|