Private
Public Access
207 lines
5.8 KiB
TypeScript
207 lines
5.8 KiB
TypeScript
import { apiFetch } from '$lib/api';
|
|
import type {
|
|
AuthResponse,
|
|
EmpresaActual,
|
|
EmpresaPlataforma,
|
|
EmpresaResumen,
|
|
Invitacion,
|
|
InvitacionCreada,
|
|
InvitacionInfo,
|
|
Miembro,
|
|
PlataformaAdmin
|
|
} from '../../types';
|
|
import { aplicarSesion } from './session';
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Empresas y sesión
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function obtenerMisEmpresas() {
|
|
return apiFetch<{
|
|
empresas: EmpresaResumen[];
|
|
es_plataforma_admin: boolean;
|
|
rol_plataforma: string;
|
|
}>('/auth/empresas');
|
|
}
|
|
|
|
export async function cambiarEmpresa(empresaID: number): Promise<AuthResponse> {
|
|
const data = await apiFetch<AuthResponse>('/auth/empresa/cambiar', {
|
|
method: 'POST',
|
|
body: { empresa_id: empresaID }
|
|
});
|
|
aplicarSesion(data);
|
|
return data;
|
|
}
|
|
|
|
export async function obtenerEmpresaActual(): Promise<EmpresaActual> {
|
|
return apiFetch<EmpresaActual>('/empresa');
|
|
}
|
|
|
|
export async function actualizarEmpresa(dto: {
|
|
nombre?: string;
|
|
slug?: string;
|
|
cuit?: string;
|
|
}): Promise<void> {
|
|
await apiFetch('/empresa', { method: 'PUT', body: dto });
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Miembros de la empresa
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function obtenerMiembros(pagina = 0, tamano = 10) {
|
|
return apiFetch<{ usuarios: Miembro[]; miembros: Miembro[]; total: number }>(
|
|
`/empresa/miembros?p=${pagina}&s=${tamano}`
|
|
);
|
|
}
|
|
|
|
export async function crearMiembro(dto: {
|
|
name: string;
|
|
email: string;
|
|
password?: string;
|
|
rol?: string;
|
|
grupo_id?: number;
|
|
}) {
|
|
return apiFetch<{
|
|
usuario: Miembro;
|
|
password_temporal: string;
|
|
usuario_existente: boolean;
|
|
}>('/empresa/miembros', { method: 'POST', body: dto });
|
|
}
|
|
|
|
export async function actualizarMiembro(
|
|
id: number,
|
|
dto: { name?: string; email?: string; rol?: string; activo?: boolean }
|
|
): Promise<Miembro> {
|
|
return apiFetch<Miembro>(`/empresa/miembros/${id}`, { method: 'PUT', body: dto });
|
|
}
|
|
|
|
export async function eliminarMiembro(id: number): Promise<void> {
|
|
await apiFetch(`/empresa/miembros/${id}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export async function altaMiembro(id: number): Promise<Miembro> {
|
|
return apiFetch<Miembro>(`/empresa/miembros/${id}/alta`, { method: 'PATCH' });
|
|
}
|
|
|
|
export async function resetearPasswordMiembro(id: number, newPassword: string): Promise<void> {
|
|
await apiFetch('/auth/cambiar-contraseña', {
|
|
method: 'POST',
|
|
body: { user_id: id, new_password: newPassword }
|
|
});
|
|
}
|
|
|
|
export async function cambiarPasswordPropia(
|
|
oldPassword: string,
|
|
newPassword: string
|
|
): Promise<void> {
|
|
await apiFetch('/auth/cambiar-contraseña', {
|
|
method: 'POST',
|
|
body: { old_password: oldPassword, new_password: newPassword }
|
|
});
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Invitaciones
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function obtenerInvitaciones() {
|
|
return apiFetch<{ invitaciones: Invitacion[] }>('/empresa/invitaciones');
|
|
}
|
|
|
|
export async function crearInvitacion(dto: {
|
|
email: string;
|
|
rol?: string;
|
|
grupo_id?: number;
|
|
expira_horas?: number;
|
|
}): Promise<InvitacionCreada> {
|
|
return apiFetch<InvitacionCreada>('/empresa/invitaciones', { method: 'POST', body: dto });
|
|
}
|
|
|
|
export async function eliminarInvitacion(id: number): Promise<void> {
|
|
await apiFetch(`/empresa/invitaciones/${id}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export async function obtenerInvitacionPublica(token: string): Promise<InvitacionInfo> {
|
|
return apiFetch<InvitacionInfo>(`/auth/invitaciones/${encodeURIComponent(token)}`, {
|
|
auth: false
|
|
});
|
|
}
|
|
|
|
export async function aceptarInvitacion(
|
|
token: string,
|
|
dto: { name?: string; password?: string }
|
|
): Promise<AuthResponse | { message: string }> {
|
|
const data = await apiFetch<AuthResponse | { message: string }>(
|
|
`/auth/invitaciones/${encodeURIComponent(token)}/aceptar`,
|
|
{ method: 'POST', body: dto, auth: false }
|
|
);
|
|
if ('access_token' in data) {
|
|
aplicarSesion(data);
|
|
}
|
|
return data;
|
|
}
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// Plataforma
|
|
// ---------------------------------------------------------------------------
|
|
|
|
export async function listarEmpresasPlataforma() {
|
|
return apiFetch<{ empresas: EmpresaPlataforma[]; total: number }>('/plataforma/empresas');
|
|
}
|
|
|
|
export async function crearEmpresaPlataforma(dto: {
|
|
nombre: string;
|
|
slug?: string;
|
|
cuit?: string;
|
|
owner_email: string;
|
|
owner_name?: string;
|
|
owner_password?: string;
|
|
}) {
|
|
return apiFetch<{
|
|
empresa: EmpresaPlataforma;
|
|
owner: Miembro;
|
|
password_temporal: string;
|
|
owner_existente: boolean;
|
|
}>('/plataforma/empresas', { method: 'POST', body: dto });
|
|
}
|
|
|
|
export async function actualizarEmpresaPlataforma(
|
|
id: number,
|
|
dto: { nombre?: string; slug?: string; cuit?: string; activa?: boolean }
|
|
): Promise<EmpresaPlataforma> {
|
|
return apiFetch<EmpresaPlataforma>(`/plataforma/empresas/${id}`, { method: 'PUT', body: dto });
|
|
}
|
|
|
|
export async function listarAdminsPlataforma() {
|
|
return apiFetch<{ admins: PlataformaAdmin[] }>('/plataforma/admins');
|
|
}
|
|
|
|
export async function agregarAdminPlataforma(dto: {
|
|
usuario_id?: number;
|
|
email?: string;
|
|
rol?: string;
|
|
}) {
|
|
return apiFetch('/plataforma/admins', { method: 'POST', body: dto });
|
|
}
|
|
|
|
export async function quitarAdminPlataforma(usuarioID: number): Promise<void> {
|
|
await apiFetch(`/plataforma/admins/${usuarioID}`, { method: 'DELETE' });
|
|
}
|
|
|
|
export async function listarUsuariosGlobales(pagina = 0, tamano = 20) {
|
|
return apiFetch<{
|
|
usuarios: Array<{
|
|
id: number;
|
|
email: string;
|
|
name: string;
|
|
activo: boolean;
|
|
debe_cambiar_password: boolean;
|
|
rol_plataforma: string;
|
|
empresas: number;
|
|
created_at: string;
|
|
}>;
|
|
total: number;
|
|
}>(`/plataforma/usuarios?p=${pagina}&s=${tamano}`);
|
|
}
|