Replace hardcoded user with live profile data
This commit is contained in:
@@ -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'
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user