Fix childer tennats

This commit is contained in:
Urtzi Alfaro
2025-12-29 17:25:20 +01:00
parent adef7971a0
commit c1dedfa44f
5 changed files with 108 additions and 57 deletions

View File

@@ -54,7 +54,7 @@ export const useTenantInitializer = () => {
const demoAccountType = useDemoAccountType();
const availableTenants = useAvailableTenants();
const currentTenant = useCurrentTenant();
const { loadUserTenants, setCurrentTenant } = useTenantActions();
const { loadUserTenants, setCurrentTenant, setAvailableTenants } = useTenantActions();
// Load tenants for authenticated users (but not demo users - they have special initialization below)
useEffect(() => {
@@ -79,34 +79,35 @@ export const useTenantInitializer = () => {
typeof currentTenant === 'object' &&
currentTenant.id === virtualTenantId;
// Determine the appropriate subscription tier based on stored value or account type
const subscriptionTier = storedTier as SubscriptionTier || getDemoTierForAccountType(demoAccountType);
// Get appropriate tenant details based on account type
const tenantDetails = getTenantDetailsForAccountType(demoAccountType);
// Create a complete tenant object matching TenantResponse structure
const mockTenant = {
id: virtualTenantId,
name: tenantDetails.name,
subdomain: `demo-${demoSessionId.slice(0, 8)}`,
business_type: tenantDetails.business_type,
business_model: tenantDetails.business_model,
description: tenantDetails.description,
address: 'Demo Address',
city: 'Madrid',
postal_code: '28001',
phone: null,
is_active: true,
subscription_plan: subscriptionTier,
subscription_tier: subscriptionTier,
ml_model_trained: false,
last_training_date: null,
owner_id: 'demo-user',
created_at: new Date().toISOString(),
};
// Only set current tenant if not already valid
if (!isValidDemoTenant) {
// Determine the appropriate subscription tier based on stored value or account type
const subscriptionTier = storedTier as SubscriptionTier || getDemoTierForAccountType(demoAccountType);
// Get appropriate tenant details based on account type
const tenantDetails = getTenantDetailsForAccountType(demoAccountType);
// Create a complete tenant object matching TenantResponse structure
const mockTenant = {
id: virtualTenantId,
name: tenantDetails.name,
subdomain: `demo-${demoSessionId.slice(0, 8)}`,
business_type: tenantDetails.business_type,
business_model: tenantDetails.business_model,
description: tenantDetails.description,
address: 'Demo Address',
city: 'Madrid',
postal_code: '28001',
phone: null,
is_active: true,
subscription_plan: subscriptionTier,
subscription_tier: subscriptionTier,
ml_model_trained: false,
last_training_date: null,
owner_id: 'demo-user',
created_at: new Date().toISOString(),
};
// Set the demo tenant as current
setCurrentTenant(mockTenant);
@@ -114,25 +115,45 @@ export const useTenantInitializer = () => {
import('../api/client').then(({ apiClient }) => {
apiClient.setTenantId(virtualTenantId);
});
}
// For enterprise demos, load child tenants immediately
if (demoAccountType === 'enterprise') {
const mockUserId = 'demo-user';
import('../api/services/tenant').then(({ TenantService }) => {
const tenantService = new TenantService();
tenantService.getUserTenants(mockUserId)
.then(tenants => {
import('../stores/tenant.store').then(({ useTenantStore }) => {
useTenantStore.getState().setAvailableTenants(tenants);
});
})
.catch(() => {
// Silently handle error
});
});
// For professional demos, just set the single mock tenant as available (if not already set)
if (demoAccountType !== 'enterprise') {
if (!availableTenants || availableTenants.length === 0) {
setAvailableTenants([mockTenant as any]);
}
return;
}
// For enterprise demos, ALWAYS ensure child tenants are loaded
// Check if we need to load child tenants:
// - availableTenants is empty/null, OR
// - availableTenants only has the parent (length === 1)
const needsChildTenants = !availableTenants || availableTenants.length <= 1;
if (needsChildTenants) {
console.log('[useTenantInitializer] Enterprise demo - loading child tenants for parent:', virtualTenantId);
import('../api/services/tenant').then(({ TenantService }) => {
const tenantService = new TenantService();
// Use getChildTenants with the parent tenant ID (virtualTenantId)
// This calls GET /tenants/{tenant_id}/children
tenantService.getChildTenants(virtualTenantId)
.then(childTenants => {
console.log('[useTenantInitializer] Enterprise demo - loaded child tenants:', childTenants?.length, childTenants);
// Combine parent tenant with children for the tenant switcher
const allTenants = [mockTenant as any, ...childTenants];
setAvailableTenants(allTenants);
})
.catch((error) => {
console.error('[useTenantInitializer] Enterprise demo - failed to load child tenants:', error);
// Fallback: at least set the parent mock tenant as available so TenantSwitcher renders
if (!availableTenants || availableTenants.length === 0) {
setAvailableTenants([mockTenant as any]);
}
});
});
}
}
}, [isDemoMode, demoSessionId, demoAccountType, currentTenant, setCurrentTenant]);
}, [isDemoMode, demoSessionId, demoAccountType, currentTenant, availableTenants, setCurrentTenant, setAvailableTenants]);
};