Replace hardcoded user with live profile data

This commit is contained in:
EdilbertoC
2026-04-28 12:46:39 -03:00
parent d496494b3e
commit 000abb39ac
15 changed files with 993 additions and 358 deletions

View File

@@ -2,8 +2,10 @@ import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
import { appointmentMapper } from '../mappers/appointmentMapper.js'
export const appointmentRepository = {
async getAll() {
const response = await fetch(`${apiConfig.restUrl}/appointments?select=*,patients(full_name),doctors(full_name)`, {
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()
})
@@ -25,33 +27,5 @@ export const appointmentRepository = {
const data = await response.json()
const item = Array.isArray(data) ? data[0] : data
return appointmentMapper.toUi(item)
},
getTodayTimeline() {
return [
{ hour: '08:00', patient: 'Carla Mendes', type: 'Consulta inicial', status: 'Confirmada', patientId: 'carla-mendes' },
{ hour: '09:30', patient: 'Ana Souza', type: 'Retorno clinico', status: 'Em triagem', patientId: 'ana-souza' },
{ hour: '11:00', patient: 'Diego Alves', type: 'Acompanhamento', status: 'Aguardando', patientId: 'diego-alves' },
{ hour: '14:30', patient: 'Bruno Lima', type: 'Teleconsulta', status: 'Confirmada', patientId: 'bruno-lima' },
{ hour: '16:00', patient: 'Horario protegido', type: 'Revisao de laudos', status: 'Bloqueado', patientId: null },
]
},
getPredictiveQueueSummary() {
return [
{ label: 'Alta prioridade', value: 3, tone: 'red' },
{ label: 'A confirmar', value: 5, tone: 'amber' },
{ label: 'Teleconsultas', value: 6, tone: 'blue' },
]
},
getWeekDays() {
return [
{ label: 'Seg', day: '06', active: false, count: 6 },
{ label: 'Ter', day: '07', active: true, count: 18 },
{ label: 'Qua', day: '08', active: false, count: 12 },
{ label: 'Qui', day: '09', active: false, count: 9 },
{ label: 'Sex', day: '10', active: false, count: 15 },
]
},
}
}

View File

@@ -60,17 +60,24 @@ export const authRepository = {
},
async getUser() {
const apiResponse = await fetch(apiEndpoint('/informacoes-do-usuario-autenticado'), {
method: 'GET',
headers: getAuthenticatedHeaders(),
}).catch(() => null)
const apiEndpoints = [
apiEndpoint('/user-info'),
apiEndpoint('/informacoes-do-usuario-autenticado'),
]
if (apiResponse?.ok) {
return apiResponse.json()
}
for (const url of apiEndpoints) {
const apiResponse = await fetch(url, {
method: 'GET',
headers: getAuthenticatedHeaders(),
}).catch(() => null)
if (apiResponse && !shouldFallback(apiResponse)) {
throw new Error(await getResponseError(apiResponse, 'Erro ao resgatar perfil de usuario.'))
if (apiResponse?.ok) {
return apiResponse.json()
}
if (apiResponse && !shouldFallback(apiResponse)) {
throw new Error(await getResponseError(apiResponse, 'Erro ao resgatar perfil de usuario.'))
}
}
const response = await fetch(`${apiConfig.supabaseUrl}/auth/v1/user`, {

View File

@@ -23,7 +23,9 @@ export const professionalRepository = {
function mapProfessional(doctor) {
return {
id: String(doctor.id || doctor.medico_id || doctor.user_id || doctor.name || doctor.nome),
userId: doctor.user_id || doctor.userId || doctor.usuario_id || doctor.auth_user_id || null,
name: doctor.name || doctor.nome || doctor.full_name || 'Medico(a)',
email: doctor.email || doctor.user_email || doctor.usuario_email || '',
role: doctor.specialty || doctor.speciality || doctor.especialidade || doctor.role || 'Medico(a)',
schedule: doctor.schedule || doctor.agenda || doctor.disponibilidade || 'Seg a Sex, 08h as 18h',
nextSlot: doctor.nextSlot || doctor.proximo_horario || doctor.next_slot || 'Consulta pendente',

View File

@@ -5,18 +5,34 @@ import { getResponseError } from './repositoryUtils.js'
export const profileRepository = {
async getCurrentUserProfile() {
const data = await authRepository.getUser()
const user = data?.user || data?.usuario || data?.profile || data
const profile = data?.profile || data?.perfil || {}
const user = data?.user || data?.usuario || profile || data
const meta = user?.user_metadata || user?.metadata || user?.app_metadata || {}
const avatarUrl = user?.avatarUrl || user?.avatar_url || meta.avatar_url || meta.picture || ''
const permissions = data?.permissions || {}
const roles = Array.isArray(data?.roles) ? data.roles : []
const avatarUrl =
profile?.avatar_url ||
profile?.avatarUrl ||
user?.avatarUrl ||
user?.avatar_url ||
meta.avatar_url ||
meta.picture ||
''
return {
id: user?.id || user?.user_id || user?.uid || '',
email: user?.email || meta.email || '',
name: user?.name || user?.nome || user?.full_name || meta.full_name || meta.name || 'Usuario',
phone: user?.phone || user?.telefone || meta.phone || meta.telefone || '',
role: user?.role || user?.cargo || meta.role || meta.cargo || 'Usuario do Sistema',
unit: user?.unit || user?.unidade || meta.unit || meta.unidade || 'Clinica Boa Vista',
id: profile?.id || user?.id || user?.user_id || user?.uid || '',
email: profile?.email || user?.email || meta.email || '',
name: profile?.full_name || user?.name || user?.nome || user?.full_name || meta.full_name || meta.name || 'Usuario',
phone: profile?.phone || user?.phone || user?.telefone || meta.phone || meta.telefone || '',
role: resolveProfileRole({ permissions, roles, user, meta }),
unit: profile?.unit || user?.unit || user?.unidade || meta.unit || meta.unidade || 'Clinica Boa Vista',
avatarUrl,
doctorId: data?.doctor_id || data?.doctorId || null,
patientId: data?.patient_id || data?.patientId || null,
roles,
permissions,
isDoctor: Boolean(permissions.isDoctor || roles.includes('doctor') || data?.doctor_id),
isAdmin: Boolean(permissions.isAdmin || roles.includes('admin')),
}
},
@@ -72,3 +88,13 @@ function normalizeAvatarResponse(data) {
path: data.path || data.key || '',
}
}
function resolveProfileRole({ permissions, roles, user, meta }) {
if (permissions.isAdmin || roles.includes('admin')) return 'Administrador'
if (permissions.isManager || roles.includes('manager')) return 'Gestor'
if (permissions.isDoctor || roles.includes('doctor')) return 'Medico(a)'
if (permissions.isSecretary || roles.includes('secretary')) return 'Secretaria'
if (permissions.isPatient || roles.includes('patient')) return 'Paciente'
return user?.role || user?.cargo || meta.role || meta.cargo || 'Usuario do Sistema'
}