New enterprise feature

This commit is contained in:
Urtzi Alfaro
2025-11-30 09:12:40 +01:00
parent f9d0eec6ec
commit 972db02f6d
176 changed files with 19741 additions and 1361 deletions

View File

@@ -314,6 +314,120 @@ class EnhancedTenantService:
error=str(e))
return []
async def get_virtual_tenants_for_session(self, demo_session_id: str, demo_account_type: str) -> List[TenantResponse]:
"""
Get virtual tenants associated with a specific demo session.
This method handles the special demo session access patterns:
- Individual bakery demo user: should have access to professional demo tenant (1 tenant)
- Enterprise demo session: should have access to parent tenant and its children (4 tenants)
Now properly filters by demo_session_id field which is populated during tenant cloning.
"""
try:
async with self.database_manager.get_session() as db_session:
await self._init_repositories(db_session)
# Query all tenants by demo_session_id (now properly populated during cloning)
virtual_tenants = await self.tenant_repo.get_tenants_by_session_id(demo_session_id)
if not virtual_tenants:
logger.warning(
"No virtual tenants found for demo session - session may not exist or tenants not yet created",
demo_session_id=demo_session_id,
demo_account_type=demo_account_type
)
return []
logger.info(
"Retrieved virtual tenants for demo session",
demo_session_id=demo_session_id,
demo_account_type=demo_account_type,
tenant_count=len(virtual_tenants)
)
return [TenantResponse.from_orm(tenant) for tenant in virtual_tenants]
except Exception as e:
logger.error("Error getting virtual tenants for demo session",
demo_session_id=demo_session_id,
demo_account_type=demo_account_type,
error=str(e))
# Fallback: return empty list instead of all demo tenants
return []
async def get_demo_tenants_by_session_type(self, demo_account_type: str, current_user_id: str) -> List[TenantResponse]:
"""
DEPRECATED: Fallback method for old demo sessions without demo_session_id.
Get demo tenants based on session type rather than user ownership.
This implements the specific requirements:
- Individual bakery demo user: access to professional demo tenant
- Enterprise demo session: access only to enterprise parent tenant and its child
WARNING: This method returns ALL demo tenants of a given type, not session-specific ones.
New code should use get_virtual_tenants_for_session() instead.
"""
logger.warning(
"Using deprecated fallback method - demo_session_id not available",
demo_account_type=demo_account_type,
user_id=current_user_id
)
try:
async with self.database_manager.get_session() as db_session:
await self._init_repositories(db_session)
if demo_account_type.lower() == 'professional_bakery':
# Individual bakery demo user should have access to professional demo tenant
# Return demo tenants with business_model='professional_bakery' that are demo tenants
tenants = await self.tenant_repo.get_multi(
filters={
"business_model": "professional_bakery",
"is_demo": True,
"is_active": True
}
)
elif demo_account_type.lower() in ['enterprise_chain', 'enterprise_parent']:
# Enterprise demo session should have access to parent tenant and its children
# Return demo tenants with tenant_type in ['parent', 'child'] that are demo tenants
parent_tenants = await self.tenant_repo.get_multi(
filters={
"tenant_type": "parent",
"is_demo": True,
"is_active": True
}
)
child_tenants = await self.tenant_repo.get_multi(
filters={
"tenant_type": "child",
"is_demo": True,
"is_active": True
}
)
tenants = parent_tenants + child_tenants
elif demo_account_type.lower() == 'enterprise_child':
# For child enterprise sessions, return only child demo tenants
tenants = await self.tenant_repo.get_multi(
filters={
"tenant_type": "child",
"is_demo": True,
"is_active": True
}
)
else:
# Default case - return the user's actual owned tenants
tenants = await self.tenant_repo.get_tenants_by_owner(current_user_id)
return [TenantResponse.from_orm(tenant) for tenant in tenants]
except Exception as e:
logger.error("Error getting demo tenants by session type",
demo_account_type=demo_account_type,
user_id=current_user_id,
error=str(e))
# Fallback: return empty list
return []
async def get_active_tenants(self, skip: int = 0, limit: int = 100) -> List[TenantResponse]:
"""Get all active tenants"""