feat: enhance user statistics and tenant management

- Updated user statistics endpoint to include access token and hub tenant ID for improved data retrieval.
- Refactored user listing functionality to support access token and hub tenant ID, ensuring accurate user data from the Hub.
- Introduced new methods in UserService for fetching users with additional information from the Hub.
- Enhanced security functions to ensure user-tenant relationships are maintained and synchronized with the Hub.
- Improved frontend logic to handle company initialization and user authentication more effectively.
This commit is contained in:
2026-05-01 23:32:48 -05:00
parent d24bb89294
commit 2ce8a9047c
6 changed files with 436 additions and 90 deletions

View File

@@ -24,11 +24,12 @@ export const load: LayoutServerLoad = async ({ cookies, url, fetch }) => {
const redirectOnFail = `/login?redirect=${encodeURIComponent(url.pathname)}`;
try {
const userData = await validateAuth(cookies, fetch, redirectOnFail);
// Cargar las compañías del usuario en el servidor (SSR)
// Primero my-companies: ejecuta get_current_user y puede crear tenant/empresa/usuario
// antes de /me/profile en validateAuth (evita SSR sin la empresa recién provisionada).
const companies = await getUserCompanies(cookies, fetch);
const userData = await validateAuth(cookies, fetch, redirectOnFail);
// Si la cookie active_company_id apunta a una compañía que ya no existe, limpiarla
const cookieCompanyId = cookies.get('active_company_id');
if (cookieCompanyId) {

View File

@@ -110,7 +110,12 @@
// ── Inicializar compañías ─────────────────────────────────────────────
if (data.companies) {
companyStore.initialize(data.companies, data.activeCompanyId);
const activeCompanyId = (data as { activeCompanyId?: number }).activeCompanyId;
void (async () => {
await companyStore.initialize(data.companies, activeCompanyId);
// Releer en cliente: el SSR puede haber quedado desfasado respecto al provisionamiento.
await companyStore.loadCompanies(undefined, activeCompanyId);
})();
}
// ── Escuchar cambios de compañía y recargar datos ─────────────────────

View File

@@ -6,6 +6,7 @@ export const load: LayoutLoad = async ({ data }) => {
user: data.user,
companies: data.companies,
authenticated: data.authenticated,
userTenants: data.userTenants ?? []
userTenants: data.userTenants ?? [],
activeCompanyId: data.activeCompanyId
};
};