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

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.',
)
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() {

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.',
)
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() {