modified: src/components/AppShell.jsx modified: src/config/api.js modified: src/config/permissions.js modified: src/data/mockData.js modified: src/hooks/useAgenda.js modified: src/hooks/useAuth.js modified: src/mappers/appointmentMapper.js modified: src/pages/AgendaPage.jsx modified: src/pages/AuthPages.jsx modified: src/pages/HomePage.jsx modified: src/pages/MedicalRecordsPage.jsx modified: src/pages/MessagesPage.jsx modified: src/pages/NotFoundPage.jsx modified: src/pages/PatientsPage.jsx modified: src/pages/ReportsPage.jsx modified: src/pages/TeamPage.jsx modified: src/pages/UsersPage.jsx modified: src/pages/VisitsPage.jsx modified: src/repositories/authRepository.js new file: src/repositories/availabilityRepository.js modified: src/repositories/communicationRepository.js modified: src/repositories/patientRepository.js modified: src/repositories/professionalRepository.js modified: src/repositories/profileRepository.js modified: src/repositories/reportRepository.js modified: src/repositories/repositoryUtils.js modified: src/repositories/settingsRepository.js modified: src/repositories/userRepository.js modified: src/repositories/visitRepository.js
124 lines
4.3 KiB
JavaScript
124 lines
4.3 KiB
JavaScript
import { authRepository } from './authRepository.js'
|
|
import { normalizeRole, ROLE_LABELS } from '../config/permissions.js'
|
|
import { apiConfig, apiEndpoint, getAuthenticatedHeaders } from '../config/api.js'
|
|
import { getResponseError } from './repositoryUtils.js'
|
|
|
|
export const profileRepository = {
|
|
async getCurrentUserProfile() {
|
|
const data = await authRepository.getUser()
|
|
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 permissions = data?.permissions || {}
|
|
const roles = collectRoles({ data, meta, profile, user })
|
|
const normalizedRole = resolveNormalizedRole({ permissions, roles, user, meta })
|
|
const avatarUrl =
|
|
profile?.avatar_url ||
|
|
profile?.avatarUrl ||
|
|
user?.avatarUrl ||
|
|
user?.avatar_url ||
|
|
meta.avatar_url ||
|
|
meta.picture ||
|
|
''
|
|
|
|
return {
|
|
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 || 'Usuário',
|
|
phone: profile?.phone || user?.phone || user?.telefone || meta.phone || meta.telefone || '',
|
|
role: ROLE_LABELS[normalizedRole] || user?.role || user?.cargo || meta.role || meta.cargo || 'Usuário do Sistema',
|
|
unit: profile?.unit || user?.unit || user?.unidade || meta.unit || meta.unidade || 'Clínica Boa Vista',
|
|
avatarUrl,
|
|
doctorId: data?.doctor_id || data?.doctorId || null,
|
|
patientId: data?.patient_id || data?.patientId || null,
|
|
roles,
|
|
permissions,
|
|
isDoctor: normalizedRole === 'medico',
|
|
isAdmin: normalizedRole === 'admin',
|
|
isManager: normalizedRole === 'gestor',
|
|
isSecretary: normalizedRole === 'secretaria',
|
|
}
|
|
},
|
|
|
|
async updateAvatar(file) {
|
|
const profile = await this.getCurrentUserProfile()
|
|
const formData = new FormData()
|
|
formData.append('avatar', file)
|
|
formData.append('file', file)
|
|
|
|
const apiResponse = await fetch(apiEndpoint('/upload-avatar'), {
|
|
method: 'POST',
|
|
headers: getAuthenticatedHeaders({ 'Content-Type': undefined }),
|
|
body: formData,
|
|
}).catch(() => null)
|
|
|
|
if (apiResponse?.ok) {
|
|
return normalizeAvatarResponse(await apiResponse.json().catch(() => ({})))
|
|
}
|
|
|
|
if (apiResponse && ![404, 405].includes(apiResponse.status)) {
|
|
throw new Error(await getResponseError(apiResponse, 'Falha ao enviar avatar.'))
|
|
}
|
|
|
|
if (!profile.id) {
|
|
throw new Error('Não foi possível identificar o usuário para enviar o avatar.')
|
|
}
|
|
|
|
const extension = file.name?.split('.').pop() || 'jpg'
|
|
const objectPath = `${profile.id}/avatar.${extension}`
|
|
const response = await fetch(`${apiConfig.storageUrl}/object/avatars/${objectPath}`, {
|
|
method: 'POST',
|
|
headers: getAuthenticatedHeaders({
|
|
'Content-Type': file.type || 'application/octet-stream',
|
|
'x-upsert': 'true',
|
|
}),
|
|
body: file,
|
|
})
|
|
|
|
if (!response.ok) {
|
|
throw new Error(await getResponseError(response, 'Falha ao enviar avatar.'))
|
|
}
|
|
|
|
return {
|
|
avatarUrl: `${apiConfig.storageUrl}/object/public/avatars/${objectPath}`,
|
|
path: objectPath,
|
|
}
|
|
},
|
|
}
|
|
|
|
function normalizeAvatarResponse(data) {
|
|
return {
|
|
avatarUrl: data.avatarUrl || data.avatar_url || data.publicUrl || data.public_url || data.url || '',
|
|
path: data.path || data.key || '',
|
|
}
|
|
}
|
|
|
|
function collectRoles({ data, meta, profile, user }) {
|
|
return [
|
|
...(Array.isArray(data?.roles) ? data.roles : []),
|
|
...(Array.isArray(user?.roles) ? user.roles : []),
|
|
data?.role,
|
|
data?.cargo,
|
|
profile?.role,
|
|
profile?.cargo,
|
|
user?.role,
|
|
user?.cargo,
|
|
meta.role,
|
|
meta.cargo,
|
|
].filter(Boolean)
|
|
}
|
|
|
|
function resolveNormalizedRole({ permissions, roles, user, meta }) {
|
|
for (const role of roles) {
|
|
const normalized = normalizeRole(role)
|
|
if (normalized) return normalized
|
|
}
|
|
|
|
if (permissions.isAdmin) return 'admin'
|
|
if (permissions.isManager) return 'gestor'
|
|
if (permissions.isDoctor) return 'medico'
|
|
if (permissions.isSecretary) return 'secretaria'
|
|
|
|
return normalizeRole(user?.role || user?.cargo || meta.role || meta.cargo)
|
|
}
|