fix(api): simplifica consultas de agendamentos e medicos

This commit is contained in:
EdilbertoC
2026-04-28 10:52:29 -03:00
parent 767f226952
commit d496494b3e
8 changed files with 92 additions and 56 deletions

4
openapi.json Normal file
View File

@@ -0,0 +1,4 @@
{
"message": "Invalid API key",
"hint": "Only the `service_role` API key can be used for this endpoint."
}

View File

@@ -1,50 +1,30 @@
import { apiConfig, apiEndpoint, getAuthenticatedHeaders } from '../config/api.js'
import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
import { appointmentMapper } from '../mappers/appointmentMapper.js'
import { fetchJsonWithFallback, normalizeCollection, normalizeItem } from './repositoryUtils.js'
export const appointmentRepository = {
async getAll() {
const data = await fetchJsonWithFallback(
[
{
url: apiEndpoint('/agendamentos'),
options: { headers: getAuthenticatedHeaders() },
},
{
url: `${apiConfig.restUrl}/appointments?select=*,patients(full_name),doctors(name)`,
options: { headers: getAuthenticatedHeaders() },
},
],
'Erro ao buscar agendamentos.',
)
const response = await fetch(`${apiConfig.restUrl}/appointments?select=*,patients(full_name),doctors(full_name)`, {
headers: getAuthenticatedHeaders()
})
return normalizeCollection(data, ['agendamentos', 'appointments', 'data']).map(appointmentMapper.toUi)
if (!response.ok) throw new Error('Erro ao buscar agendamentos.')
const data = await response.json()
return (Array.isArray(data) ? data : []).map(appointmentMapper.toUi)
},
async create(uiData) {
const data = await fetchJsonWithFallback(
[
{
url: apiEndpoint('/agendamentos'),
options: {
method: 'POST',
headers: getAuthenticatedHeaders(),
body: JSON.stringify(appointmentMapper.toApi(uiData)),
},
},
{
url: `${apiConfig.restUrl}/appointments`,
options: {
const response = await fetch(`${apiConfig.restUrl}/appointments`, {
method: 'POST',
headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }),
body: JSON.stringify(appointmentMapper.toApi(uiData, 'supabase')),
},
},
],
'Falha ao criar o agendamento.',
)
})
return appointmentMapper.toUi(normalizeItem(data, ['agendamento', 'appointment', 'data']))
if (!response.ok) throw new Error('Falha ao criar o agendamento.')
const data = await response.json()
const item = Array.isArray(data) ? data[0] : data
return appointmentMapper.toUi(item)
},
getTodayTimeline() {

View File

@@ -1,23 +1,15 @@
import { apiConfig, apiEndpoint, getAuthenticatedHeaders } from '../config/api.js'
import { fetchJsonWithFallback, normalizeCollection } from './repositoryUtils.js'
import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
export const professionalRepository = {
async getAll() {
const data = await fetchJsonWithFallback(
[
{
url: apiEndpoint('/listar-medicos'),
options: { headers: getAuthenticatedHeaders() },
},
{
url: `${apiConfig.restUrl}/doctors`,
options: { headers: getAuthenticatedHeaders() },
},
],
'Erro ao buscar medicos.',
)
const response = await fetch(`${apiConfig.restUrl}/doctors`, {
headers: getAuthenticatedHeaders()
})
return normalizeCollection(data, ['medicos', 'doctors', 'professionals', 'data']).map(mapProfessional)
if (!response.ok) throw new Error('Erro ao buscar medicos.')
const data = await response.json()
return (Array.isArray(data) ? data : []).map(mapProfessional)
},
getCoverageMap() {

11
test.mjs Normal file
View File

@@ -0,0 +1,11 @@
import { apiConfig } from './src/config/api.js';
async function test() {
const url = `${apiConfig.restUrl}/appointments?select=*,patients(full_name),doctors(name)`;
const res = await fetch(url, { headers: { apikey: apiConfig.anonKey }});
const text = await res.text();
console.log('Status:', res.status);
console.log('Response:', text);
}
test().catch(console.error);

11
test2.mjs Normal file
View File

@@ -0,0 +1,11 @@
const url = "https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/appointments?select=*,patients(full_name),doctors(name)";
const key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
async function test() {
const res = await fetch(url, { headers: { apikey: key, Authorization: "Bearer " + key }});
const text = await res.text();
console.log('Status:', res.status);
console.log('Response:', text);
}
test().catch(console.error);

14
test3.mjs Normal file
View File

@@ -0,0 +1,14 @@
const url1 = "https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/doctors?select=*&limit=1";
const url2 = "https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/patients?select=*&limit=1";
const url3 = "https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/appointments?select=*&limit=1";
const key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
async function test() {
const reqs = [url1, url2, url3].map(u => fetch(u, { headers: { apikey: key, Authorization: "Bearer " + key }}));
const res = await Promise.all(reqs);
for(const r of res) {
console.log(r.url, await r.text());
}
}
test().catch(console.error);

13
test4.mjs Normal file
View File

@@ -0,0 +1,13 @@
const url = "https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/";
const key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
async function test() {
const res = await fetch(url, { headers: { apikey: key }});
const json = await res.json();
console.log("Doctors columns:", Object.keys(json.definitions.doctors.properties));
console.log("Patients columns:", Object.keys(json.definitions.patients.properties));
console.log("Appointments columns:", Object.keys(json.definitions.appointments.properties));
}
test().catch(console.error);

11
test5.mjs Normal file
View File

@@ -0,0 +1,11 @@
import fs from 'fs';
const url = "https://yuanqfswhberkoevtmfr.supabase.co/rest/v1/";
const key = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6Inl1YW5xZnN3aGJlcmtvZXZ0bWZyIiwicm9sZSI6ImFub24iLCJpYXQiOjE3NTQ5NTQzNjksImV4cCI6MjA3MDUzMDM2OX0.g8Fm4XAvtX46zifBZnYVH4tVuQkqUH6Ia9CXQj4DztQ";
async function test() {
const res = await fetch(url, { headers: { apikey: key }});
const json = await res.json();
fs.writeFileSync('openapi.json', JSON.stringify(json, null, 2));
}
test().catch(console.error);