Private
Public Access
todas las cosas para tener lo de los grupos funcional
This commit is contained in:
1 parent
f3edc5ceb2
commit
ad0612c4d8
16 files changed
+668
-112
No files matched your search
@@ -2,13 +2,16 @@ import { goto } from '$app/navigation';
|
||||
import type { Usuario, GruposYPermisosResponse } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken, removeToken, removeAuthTokens } from './token';
|
||||
import { removeCurrentUser } from './user';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
console.log(error);
|
||||
if (error.error) {
|
||||
removeToken();
|
||||
removeAuthTokens();
|
||||
removeCurrentUser();
|
||||
goto('/');
|
||||
}
|
||||
throw new Error(error.message || error.error || 'Error en la petición');
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function agregarSubGrupo(grupoId: number, childGroupId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/subgrupos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ child_group_id: childGroupId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al agregar subgrupo');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function asignarPermisoAGrupo(grupoId: number, permissionId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/permisos`, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ permission_id: permissionId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al asignar permiso');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
import type { Grupo } from '../../types';
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
async function handleResponse<T>(response: Response): Promise<T> {
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.error || 'Error en la petición');
|
||||
}
|
||||
return response.json();
|
||||
}
|
||||
|
||||
export async function obtenerGrupo(id: number, fetch?: Function): Promise<Grupo> {
|
||||
if (!fetch) fetch = window.fetch;
|
||||
const response = await fetch(`${API_URL}/grupos/${id}`, {
|
||||
headers: { Authorization: `Bearer ${getToken()}` }
|
||||
});
|
||||
return handleResponse<Grupo>(response);
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function quitarPermisoAGrupo(grupoId: number, permissionId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/permisos`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ permission_id: permissionId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al quitar permiso');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
import { API_URL } from '../env';
|
||||
import { getToken } from './token';
|
||||
|
||||
export async function quitarSubGrupo(grupoId: number, childGroupId: number): Promise<void> {
|
||||
const response = await fetch(`${API_URL}/grupos/${grupoId}/subgrupos`, {
|
||||
method: 'DELETE',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${getToken()}`
|
||||
},
|
||||
body: JSON.stringify({ child_group_id: childGroupId })
|
||||
});
|
||||
if (!response.ok) {
|
||||
const error = await response.json().catch(() => ({ message: 'Error desconocido' }));
|
||||
throw new Error(error.message || 'Error al quitar subgrupo');
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user