Private
Public Access
feat:añadido quitar y asignar grupos a un usuario
This commit is contained in:
1 parent
38e040354a
commit
22b2b7d2d0
14 files changed
+486
-9
No files matched your search
@@ -4,6 +4,8 @@ import { API_URL } from '../env';
|
||||
import { getToken, removeToken, removeAuthTokens } from './token';
|
||||
import { removeCurrentUser } from './user';
|
||||
|
||||
//Se usa en la parte del menu donde se obtienen los grupos y permisos para mostrar
|
||||
//
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
@@ -19,7 +21,9 @@ async function handleResponse<T>(response: Response): Promise<T> {
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function ObtenerGruposDeUsuario(usu: Usuario): Promise<GruposYPermisosResponse> {
|
||||
export async function ObtenerGruposDeUsuario(
|
||||
usu: Partial<Usuario>
|
||||
): Promise<GruposYPermisosResponse> {
|
||||
const response = await fetch(`${API_URL}/usuarios/grupos-permisos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function agregarGrupoAUsuario(
|
||||
dto: { grupo_id: number; usuario_id: number },
|
||||
callback: Function
|
||||
) {
|
||||
if (!dto.grupo_id || !dto.usuario_id) return 'Faltan cargar datos';
|
||||
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios/grupos/añadir`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
|
||||
let data = await req.json();
|
||||
if (!req.ok) {
|
||||
return data.error || 'Error';
|
||||
}
|
||||
callback();
|
||||
return 'Ok';
|
||||
} catch {
|
||||
return 'No se pudo conectar con el servidor';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
import { handleResponse } from '$lib/handleRespondeJSON';
|
||||
import type { Grupo } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
// Se usa en la parte de /usuarios
|
||||
export async function obtenerGruposDeUsuario(dto: {
|
||||
usuario_id: number;
|
||||
}): Promise<Grupo[] | string> {
|
||||
if (!dto.usuario_id) return 'Falta datos';
|
||||
const response = await fetch(`${API_URL}/usuarios/${dto.usuario_id}/grupos`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
|
||||
return handleResponse<Grupo[]>(response);
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { invalidate } from '$app/navigation';
|
||||
import { API_URL } from '$lib/env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function quitarGrupoAUsuario(
|
||||
dto: { grupo_id: number; usuario_id: number },
|
||||
callback: Function
|
||||
) {
|
||||
if (!dto.grupo_id || !dto.usuario_id) return 'Faltan cargar datos';
|
||||
|
||||
try {
|
||||
const req = await fetch(`${API_URL}/usuarios/grupos/quitar`, {
|
||||
method: 'PUT',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify(dto)
|
||||
});
|
||||
|
||||
if (!req.ok) {
|
||||
return 'Error';
|
||||
}
|
||||
callback();
|
||||
return 'Ok';
|
||||
} catch (e) {
|
||||
console.log(e);
|
||||
return 'No se pudo conectar con el servidor';
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user