fix(principal): integra auth, agenda e laudos com a api
This commit is contained in:
@@ -1,11 +1,74 @@
|
||||
import { authRepository } from './authRepository.js'
|
||||
import { apiConfig, apiEndpoint, getAuthenticatedHeaders } from '../config/api.js'
|
||||
import { getResponseError } from './repositoryUtils.js'
|
||||
|
||||
export const profileRepository = {
|
||||
getCurrentUserProfile() {
|
||||
async getCurrentUserProfile() {
|
||||
const data = await authRepository.getUser()
|
||||
const user = data?.user || data?.usuario || data?.profile || data
|
||||
const meta = user?.user_metadata || user?.metadata || user?.app_metadata || {}
|
||||
const avatarUrl = user?.avatarUrl || user?.avatar_url || meta.avatar_url || meta.picture || ''
|
||||
|
||||
return {
|
||||
email: 'henrique.cardoso@mediconnect.com.br',
|
||||
name: 'Dr. Henrique Cardoso',
|
||||
phone: '(81) 98888-0101',
|
||||
role: 'Medico Clinico Geral',
|
||||
unit: 'Clinica Boa Vista',
|
||||
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',
|
||||
avatarUrl,
|
||||
}
|
||||
},
|
||||
|
||||
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('Nao foi possivel identificar o usuario 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 || '',
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user