forked from RiseUP/riseup_squad_03
modified: src/App.jsx
modified: src/components/AppShell.jsx new file: src/config/permissions.js new file: src/hooks/useAuth.js new file: src/pages/UsersPage.jsx new file: src/repositories/userRepository.js
This commit is contained in:
72
src/repositories/userRepository.js
Normal file
72
src/repositories/userRepository.js
Normal file
@@ -0,0 +1,72 @@
|
||||
import { apiConfig, getAuthenticatedHeaders } from '../config/api.js'
|
||||
|
||||
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])
|
||||
},
|
||||
// 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')
|
||||
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,
|
||||
}
|
||||
|
||||
if (data.create_patient_record) {
|
||||
body.create_patient_record = true
|
||||
body.cpf = data.cpf
|
||||
body.phone_mobile = data.phone_mobile
|
||||
}
|
||||
|
||||
console.log('Enviando para create-user:', body)
|
||||
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/create-user`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
body: JSON.stringify(body),
|
||||
})
|
||||
|
||||
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))
|
||||
}
|
||||
|
||||
return response.json()
|
||||
},
|
||||
|
||||
// Deletar usuário permanentemente — IRREVERSÍVEL (admin/gestor)
|
||||
async remove(userId) {
|
||||
const response = await fetch(`${apiConfig.functionsUrl}/delete-user`, {
|
||||
method: 'POST',
|
||||
headers: getAuthenticatedHeaders(),
|
||||
body: JSON.stringify({ user_id: userId }),
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({}))
|
||||
throw new Error(error.message || 'Erro ao deletar usuário')
|
||||
}
|
||||
|
||||
return true
|
||||
},
|
||||
}
|
||||
Reference in New Issue
Block a user