diff --git a/openapi.json b/openapi.json new file mode 100644 index 0000000..2e5402d --- /dev/null +++ b/openapi.json @@ -0,0 +1,4 @@ +{ + "message": "Invalid API key", + "hint": "Only the `service_role` API key can be used for this endpoint." +} \ No newline at end of file diff --git a/src/repositories/appointmentRepository.js b/src/repositories/appointmentRepository.js index e5f217b..63e486f 100644 --- a/src/repositories/appointmentRepository.js +++ b/src/repositories/appointmentRepository.js @@ -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.', - ) - - return normalizeCollection(data, ['agendamentos', 'appointments', 'data']).map(appointmentMapper.toUi) + const response = await fetch(`${apiConfig.restUrl}/appointments?select=*,patients(full_name),doctors(full_name)`, { + headers: getAuthenticatedHeaders() + }) + + 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: { - method: 'POST', - headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }), - body: JSON.stringify(appointmentMapper.toApi(uiData, 'supabase')), - }, - }, - ], - 'Falha ao criar o agendamento.', - ) + const response = await fetch(`${apiConfig.restUrl}/appointments`, { + method: 'POST', + headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }), + body: JSON.stringify(appointmentMapper.toApi(uiData, 'supabase')), + }) - 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() { diff --git a/src/repositories/professionalRepository.js b/src/repositories/professionalRepository.js index 11242f3..f490a9b 100644 --- a/src/repositories/professionalRepository.js +++ b/src/repositories/professionalRepository.js @@ -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.', - ) - - return normalizeCollection(data, ['medicos', 'doctors', 'professionals', 'data']).map(mapProfessional) + const response = await fetch(`${apiConfig.restUrl}/doctors`, { + headers: getAuthenticatedHeaders() + }) + + if (!response.ok) throw new Error('Erro ao buscar medicos.') + + const data = await response.json() + return (Array.isArray(data) ? data : []).map(mapProfessional) }, getCoverageMap() { diff --git a/test.mjs b/test.mjs new file mode 100644 index 0000000..cec8a82 --- /dev/null +++ b/test.mjs @@ -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); diff --git a/test2.mjs b/test2.mjs new file mode 100644 index 0000000..525ee66 --- /dev/null +++ b/test2.mjs @@ -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); diff --git a/test3.mjs b/test3.mjs new file mode 100644 index 0000000..627c69c --- /dev/null +++ b/test3.mjs @@ -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); diff --git a/test4.mjs b/test4.mjs new file mode 100644 index 0000000..52c1521 --- /dev/null +++ b/test4.mjs @@ -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); diff --git a/test5.mjs b/test5.mjs new file mode 100644 index 0000000..de45f95 --- /dev/null +++ b/test5.mjs @@ -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);