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

@@ -1,11 +1,76 @@
# shared/clients/tenant_client.py
"""
Tenant Service Client for Inter-Service Communication
Provides access to tenant settings and configuration from other services
This client provides a high-level API for interacting with the Tenant Service,
which manages tenant metadata, settings, hierarchical relationships (parent-child),
and multi-location support for enterprise bakery networks.
Key Capabilities:
- Tenant Management: Get, create, update tenant records
- Settings Management: Category-specific settings (procurement, inventory, production, etc.)
- Enterprise Hierarchy: Parent-child tenant relationships for multi-location networks
- Tenant Locations: Physical location management (central_production, retail_outlet)
- Subscription Management: Subscription tier and quota validation
- Multi-Tenancy: Tenant isolation and access control
Enterprise Hierarchy Features:
- get_child_tenants(): Fetch all child outlets for a parent (central bakery)
- get_parent_tenant(): Get parent tenant from child outlet
- get_tenant_hierarchy(): Get complete hierarchy path and metadata
- get_tenant_locations(): Get all physical locations for a tenant
- Supports 3 tenant types: standalone, parent, child
Usage Example:
```python
from shared.clients import create_tenant_client
from shared.config.base import get_settings
config = get_settings()
client = create_tenant_client(config)
# Get parent tenant and all children
parent = await client.get_tenant(parent_tenant_id)
children = await client.get_child_tenants(parent_tenant_id)
# Get hierarchy information
hierarchy = await client.get_tenant_hierarchy(tenant_id)
# Returns: {tenant_type: 'parent', hierarchy_path: 'parent_id', child_count: 3}
# Get physical locations
locations = await client.get_tenant_locations(parent_tenant_id)
# Returns: [{location_type: 'central_production', ...}, ...]
# Get category settings
procurement_settings = await client.get_procurement_settings(tenant_id)
```
Settings Categories:
- procurement: Min/max order quantities, lead times, reorder points
- inventory: FIFO settings, expiry thresholds, temperature monitoring
- production: Batch sizes, quality control, equipment settings
- supplier: Payment terms, delivery preferences
- pos: POS integration settings
- order: Order fulfillment rules
- notification: Alert preferences
Service Architecture:
- Base URL: Configured via TENANT_SERVICE_URL environment variable
- Authentication: Uses BaseServiceClient with tenant_id header validation
- Error Handling: Returns None on errors, logs detailed error context
- Async: All methods are async and use httpx for HTTP communication
Related Services:
- Distribution Service: Uses tenant locations for delivery route planning
- Forecasting Service: Uses hierarchy for network demand aggregation
- Procurement Service: Validates parent-child for internal transfers
- Orchestrator Service: Enterprise dashboard queries hierarchy data
For more details, see services/tenant/README.md
"""
import structlog
from typing import Dict, Any, Optional
from typing import Dict, Any, Optional, List
from uuid import UUID
from shared.clients.base_service_client import BaseServiceClient
from shared.config.base import BaseServiceSettings
@@ -230,6 +295,116 @@ class TenantServiceClient(BaseServiceClient):
logger.error("Error getting active tenants", error=str(e))
return []
# ================================================================
# ENTERPRISE TIER METHODS
# ================================================================
async def get_child_tenants(self, parent_tenant_id: str) -> Optional[List[Dict[str, Any]]]:
"""
Get all child tenants for a parent tenant
Args:
parent_tenant_id: Parent tenant ID
Returns:
List of child tenant dictionaries
"""
try:
result = await self.get(f"tenants/{parent_tenant_id}/children", tenant_id=parent_tenant_id)
if result:
logger.info("Retrieved child tenants",
parent_tenant_id=parent_tenant_id,
child_count=len(result) if isinstance(result, list) else 0)
return result
except Exception as e:
logger.error("Error getting child tenants",
error=str(e), parent_tenant_id=parent_tenant_id)
return None
async def get_tenant_children_count(self, tenant_id: str) -> int:
"""
Get count of child tenants for a parent tenant
Args:
tenant_id: Tenant ID to check
Returns:
Number of child tenants (0 if not a parent)
"""
try:
children = await self.get_child_tenants(tenant_id)
return len(children) if children else 0
except Exception as e:
logger.error("Error getting child tenant count",
error=str(e), tenant_id=tenant_id)
return 0
async def get_parent_tenant(self, child_tenant_id: str) -> Optional[Dict[str, Any]]:
"""
Get parent tenant for a child tenant
Args:
child_tenant_id: Child tenant ID
Returns:
Parent tenant dictionary
"""
try:
result = await self.get(f"tenants/{child_tenant_id}/parent", tenant_id=child_tenant_id)
if result:
logger.info("Retrieved parent tenant",
child_tenant_id=child_tenant_id,
parent_tenant_id=result.get('id'))
return result
except Exception as e:
logger.error("Error getting parent tenant",
error=str(e), child_tenant_id=child_tenant_id)
return None
async def get_tenant_hierarchy(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""
Get complete tenant hierarchy information
Args:
tenant_id: Tenant ID to get hierarchy for
Returns:
Hierarchy information dictionary
"""
try:
result = await self.get("hierarchy", tenant_id=tenant_id)
if result:
logger.info("Retrieved tenant hierarchy",
tenant_id=tenant_id,
hierarchy_type=result.get('tenant_type'))
return result
except Exception as e:
logger.error("Error getting tenant hierarchy",
error=str(e), tenant_id=tenant_id)
return None
async def get_tenant_locations(self, tenant_id: str) -> Optional[List[Dict[str, Any]]]:
"""
Get all locations for a tenant
Args:
tenant_id: Tenant ID
Returns:
List of tenant location dictionaries
"""
try:
result = await self.get("locations", tenant_id=tenant_id)
if result:
logger.info("Retrieved tenant locations",
tenant_id=tenant_id,
location_count=len(result) if isinstance(result, list) else 0)
return result
except Exception as e:
logger.error("Error getting tenant locations",
error=str(e), tenant_id=tenant_id)
return None
# ================================================================
# UTILITY METHODS
# ================================================================