Files
riseup_squad_03/src/repositories/appointmentRepository.js
letvb20-dot 94dab58d85 new file: public/favicon.svg
deleted:    src/assets/hero.png
modified:   src/components/AppShell.jsx
modified:   src/components/calendar/AgendaDailyView.jsx
modified:   src/components/calendar/AgendaMonthlyView.jsx
modified:   src/components/calendar/AgendaWeeklyView.jsx
modified:   src/hooks/useAgenda.js
modified:   src/index.css
modified:   src/mappers/appointmentMapper.js
modified:   src/mappers/reportMapper.js
modified:   src/pages/AgendaPage.jsx
modified:   src/pages/AuthPages.jsx
modified:   src/pages/HomePage.jsx
modified:   src/pages/MessagesPage.jsx
modified:   src/pages/PatientsPage.jsx
modified:   src/pages/ProfilePage.jsx
modified:   src/pages/ReportsPage.jsx
modified:   src/pages/SettingsPage.jsx
modified:   src/repositories/appointmentRepository.js
modified:   src/repositories/settingsRepository.js
2026-05-08 01:32:46 -03:00

49 lines
1.9 KiB
JavaScript

import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
import { appointmentMapper } from '../mappers/appointmentMapper.js'
import { getResponseError, normalizeItem } from './repositoryUtils.js'
export const appointmentRepository = {
async getAll({ doctorId } = {}) {
const doctorFilter = doctorId ? `&doctor_id=eq.${encodeURIComponent(doctorId)}` : ''
const response = await fetch(`${apiConfig.restUrl}/appointments?select=*,patients(full_name),doctors(full_name)${doctorFilter}`, {
headers: getAuthenticatedHeaders()
})
if (!response.ok) throw new Error(await getResponseError(response, 'Erro ao buscar agendamentos.'))
const data = await response.json()
return (Array.isArray(data) ? data : []).map(appointmentMapper.toUi)
},
async create(uiData) {
const response = await fetch(`${apiConfig.restUrl}/appointments`, {
method: 'POST',
headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }),
body: JSON.stringify(appointmentMapper.toApi(uiData, 'supabase')),
})
if (!response.ok) throw new Error(await getResponseError(response, 'Falha ao criar o agendamento.'))
const data = await response.json()
return appointmentMapper.toUi(normalizeItem(data))
},
async update(id, uiData) {
const response = await fetch(`${apiConfig.restUrl}/appointments?id=eq.${encodeURIComponent(id)}`, {
method: 'PATCH',
headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }),
body: JSON.stringify(appointmentMapper.toApi(uiData, 'supabase')),
})
if (!response.ok) throw new Error(await getResponseError(response, 'Falha ao atualizar o agendamento.'))
const data = await response.json()
return appointmentMapper.toUi(normalizeItem(data))
},
async cancel(id, uiData) {
return this.update(id, { ...uiData, status: 'Cancelada' })
},
}