modified: src/App.jsx
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
This commit is contained in:
@@ -1,60 +1,84 @@
|
||||
import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
|
||||
import { getResponseError, normalizeCollection } from './repositoryUtils.js'
|
||||
|
||||
const USER_PROFILE_TABLES = ['profiles', 'user_profiles']
|
||||
const USER_LIST_KEYS = ['users', 'usuarios', 'data', 'items', 'results']
|
||||
|
||||
export const userRepository = {
|
||||
// Listar todos os usuários (admin/gestor)
|
||||
async getAll() {
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/user-info`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
})
|
||||
if (!response.ok) throw new Error('Erro ao listar usuários')
|
||||
const data = await response.json()
|
||||
console.log('Resposta de user-info:', data)
|
||||
return Array.isArray(data) ? data : (data.users ?? [data])
|
||||
let lastResponse = null
|
||||
|
||||
for (const table of USER_PROFILE_TABLES) {
|
||||
const query = new URLSearchParams({
|
||||
select: '*',
|
||||
})
|
||||
const response = await fetch(`${apiConfig.restUrl}/${table}?${query.toString()}`, {
|
||||
headers: getAuthenticatedHeaders(),
|
||||
}).catch(() => null)
|
||||
|
||||
if (!response) continue
|
||||
lastResponse = response
|
||||
|
||||
if (response.ok) {
|
||||
const data = await response.json().catch(() => null)
|
||||
return normalizeCollection(data, USER_LIST_KEYS).map(normalizeListedUser)
|
||||
}
|
||||
|
||||
if (![404, 406].includes(response.status)) {
|
||||
throw new Error(await getResponseError(response, 'Erro ao listar usuários.'))
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(await getResponseError(lastResponse, 'Tabela de perfis de usuários não encontrada.'))
|
||||
},
|
||||
// Buscar usuário por ID (admin/gestor)
|
||||
|
||||
async getById(userId) {
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/user-info-by-id`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
body: JSON.stringify({ user_id: userId }),
|
||||
})
|
||||
if (!response.ok) throw new Error('Erro ao buscar usuário')
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(await getResponseError(response, 'Erro ao buscar usuário.'))
|
||||
}
|
||||
|
||||
return response.json()
|
||||
},
|
||||
|
||||
// Criar usuário com magic link (admin/gestor/secretaria)
|
||||
async create(data) {
|
||||
const body = {
|
||||
email: data.email,
|
||||
full_name: data.full_name,
|
||||
role: data.role,
|
||||
}
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/create-user`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
body: JSON.stringify(buildCreateUserBody(data)),
|
||||
})
|
||||
|
||||
if (data.create_patient_record) {
|
||||
body.create_patient_record = true
|
||||
body.cpf = data.cpf
|
||||
body.phone_mobile = data.phone_mobile
|
||||
}
|
||||
if (!response.ok) {
|
||||
throw new Error(await getResponseError(response, 'Erro ao criar usuário.'))
|
||||
}
|
||||
|
||||
console.log('Enviando para create-user:', body)
|
||||
return response.json()
|
||||
},
|
||||
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/create-user`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
async createWithPassword(data) {
|
||||
const body = {
|
||||
...buildCreateUserBody(data),
|
||||
password: data.password,
|
||||
}
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
console.error('Resposta de erro da API:', error)
|
||||
throw new Error(error.message || error.error || error.msg || JSON.stringify(error))
|
||||
}
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/create-user-with-password`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
return response.json()
|
||||
},
|
||||
if (!response.ok) {
|
||||
throw new Error(await getResponseError(response, 'Erro ao criar usuário com senha.'))
|
||||
}
|
||||
|
||||
return response.json()
|
||||
},
|
||||
|
||||
// Deletar usuário permanentemente — IRREVERSÍVEL (admin/gestor)
|
||||
async remove(userId) {
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/delete-user`, {
|
||||
method: 'POST',
|
||||
@@ -63,10 +87,35 @@ export const userRepository = {
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
throw new Error(error.message || 'Erro ao deletar usuário')
|
||||
throw new Error(await getResponseError(response, 'Erro ao deletar usuário.'))
|
||||
}
|
||||
|
||||
return true
|
||||
},
|
||||
}
|
||||
|
||||
function buildCreateUserBody(data) {
|
||||
const body = {
|
||||
email: data.email?.trim(),
|
||||
full_name: data.full_name?.trim(),
|
||||
phone: data.phone?.trim(),
|
||||
cpf: data.cpf?.trim(),
|
||||
role: data.role,
|
||||
}
|
||||
|
||||
if (data.create_patient_record) {
|
||||
body.create_patient_record = true
|
||||
body.phone_mobile = data.phone_mobile?.trim() || data.phone?.trim()
|
||||
}
|
||||
|
||||
return body
|
||||
}
|
||||
|
||||
function normalizeListedUser(user) {
|
||||
return {
|
||||
...user,
|
||||
email: user.email || user.user_email || '',
|
||||
full_name: user.full_name || user.name || user.nome || '',
|
||||
role: Array.isArray(user.roles) ? user.roles[0] : (user.role || user.cargo || ''),
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user