fix(principal): integra auth, agenda e laudos com a api

This commit is contained in:
EdilbertoC
2026-04-28 10:22:54 -03:00
parent d576fb9784
commit 7199c107f2
20 changed files with 1121 additions and 331 deletions

View File

@@ -1,33 +1,22 @@
import { apiConfig, apiHeaders } from '../config/api.js'
import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
export const patientRepository = {
// 1. Listar pacientes
async getAll() {
const response = await fetch(`${apiConfig.restUrl}/patients?select=*`, { headers: apiHeaders })
const response = await fetch(`${apiConfig.restUrl}/patients?select=*`, { headers: getAuthenticatedHeaders() })
if (!response.ok) throw new Error('Erro ao buscar pacientes')
return response.json()
},
async getById(patientId) {
const patients = await this.getAll()
return patients.find((p) => String(p.id) === String(patientId)) || null
const patient = patients.find((p) => String(p.id) === String(patientId)) || null
return patient ? mapPatientToDetail(patient) : null
},
async getDirectoryRows() {
const patients = await this.getAll()
return patients.map((patient) => ({
...patient,
name: patient.full_name,
phone: patient.phone_mobile,
detailId: patient.id,
insurance: 'Particular',
city: 'Recife',
state: 'PE',
vip: false,
lastVisitIso: null,
lastVisit: 'Ainda nao houve atendimento',
nextVisit: 'Nenhum atendimento agendado',
}))
return patients.map(mapPatientToDirectory)
},
// 2. Criar paciente (direto)
@@ -43,7 +32,7 @@ export const patientRepository = {
const response = await fetch(`${apiConfig.restUrl}/patients`, {
method: 'POST',
headers: { ...apiHeaders, Prefer: 'return=representation' },
headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }),
body: JSON.stringify(body),
})
@@ -69,7 +58,7 @@ export const patientRepository = {
const response = await fetch(`${apiConfig.functionsUrl}/create-patient`, {
method: 'POST',
headers: apiHeaders,
headers: getAuthenticatedHeaders(),
body: JSON.stringify(body),
})
@@ -93,7 +82,7 @@ export const patientRepository = {
const response = await fetch(`${apiConfig.restUrl}/patients?id=eq.${patientId}`, {
method: 'PATCH',
headers: { ...apiHeaders, Prefer: 'return=representation' },
headers: getAuthenticatedHeaders({ Prefer: 'return=representation' }),
body: JSON.stringify(body),
})
@@ -105,10 +94,62 @@ export const patientRepository = {
async remove(patientId) {
const response = await fetch(`${apiConfig.restUrl}/patients?id=eq.${patientId}`, {
method: 'DELETE',
headers: apiHeaders,
headers: getAuthenticatedHeaders(),
})
if (!response.ok) throw new Error('Erro ao deletar paciente')
return true
},
}
function mapPatientToDirectory(patient) {
return {
...patient,
name: patient.name || patient.full_name || patient.nome || 'Paciente',
phone: patient.phone || patient.phone_mobile || patient.telefone || '',
detailId: patient.id,
insurance: patient.insurance || patient.convenio || 'Particular',
city: patient.city || patient.cidade || 'Recife',
state: patient.state || patient.uf || 'PE',
vip: Boolean(patient.vip),
lastVisitIso: patient.lastVisitIso || patient.last_visit_iso || null,
lastVisit: patient.lastVisit || patient.last_visit || 'Ainda nao houve atendimento',
nextVisit: patient.nextVisit || patient.next_visit || 'Nenhum atendimento agendado',
}
}
function mapPatientToDetail(patient) {
const directory = mapPatientToDirectory(patient)
return {
...directory,
age: patient.age || patient.idade || calculateAge(patient.birth_date),
document: patient.document || patient.cpf || 'CPF nao informado',
plan: directory.insurance,
condition: patient.condition || patient.condicao || 'Sem condicao principal',
status: patient.status || 'Acompanhamento',
risk: patient.risk || patient.risco || 'Baixo',
email: patient.email || '',
address: patient.address || patient.endereco || 'Endereco nao informado',
team: patient.team || patient.equipe || [],
notes: patient.notes || patient.observacoes || [],
exams: patient.exams || patient.exames || [],
}
}
function calculateAge(birthDate) {
if (!birthDate) return 0
const birth = new Date(birthDate)
if (Number.isNaN(birth.getTime())) return 0
const today = new Date()
let age = today.getFullYear() - birth.getFullYear()
const monthDiff = today.getMonth() - birth.getMonth()
if (monthDiff < 0 || (monthDiff === 0 && today.getDate() < birth.getDate())) {
age -= 1
}
return age
}