Improve the frontend and repository layer

This commit is contained in:
Urtzi Alfaro
2025-10-23 07:44:54 +02:00
parent 8d30172483
commit 07c33fa578
112 changed files with 14726 additions and 2733 deletions

View File

@@ -15,6 +15,7 @@ from .orders_client import OrdersServiceClient
from .production_client import ProductionServiceClient
from .recipes_client import RecipesServiceClient
from .suppliers_client import SuppliersServiceClient
from .tenant_client import TenantServiceClient
# Import config
from shared.config.base import BaseServiceSettings
@@ -221,6 +222,7 @@ __all__ = [
'ProductionServiceClient',
'RecipesServiceClient',
'SuppliersServiceClient',
'TenantServiceClient',
'ServiceClients',
'get_training_client',
'get_sales_client',

View File

@@ -274,6 +274,76 @@ class ProductionServiceClient(BaseServiceClient):
error=str(e), alert_id=alert_id, tenant_id=tenant_id)
return None
# ================================================================
# WASTE AND SUSTAINABILITY ANALYTICS
# ================================================================
async def get_waste_analytics(
self,
tenant_id: str,
start_date: str,
end_date: str
) -> Optional[Dict[str, Any]]:
"""
Get production waste analytics for sustainability reporting
Args:
tenant_id: Tenant ID
start_date: Start date (ISO format)
end_date: End date (ISO format)
Returns:
Dictionary with waste analytics data:
- total_production_waste: Total waste in kg
- total_defects: Total defect waste in kg
- total_planned: Total planned production in kg
- total_actual: Total actual production in kg
- ai_assisted_batches: Number of AI-assisted batches
"""
try:
params = {
"start_date": start_date,
"end_date": end_date
}
result = await self.get("production/waste-analytics", tenant_id=tenant_id, params=params)
if result:
logger.info("Retrieved production waste analytics",
tenant_id=tenant_id,
start_date=start_date,
end_date=end_date)
return result
except Exception as e:
logger.error("Error getting production waste analytics",
error=str(e), tenant_id=tenant_id)
return None
async def get_baseline(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""
Get baseline waste percentage for SDG compliance calculations
Args:
tenant_id: Tenant ID
Returns:
Dictionary with baseline data:
- waste_percentage: Baseline waste percentage
- period: Information about the baseline period
- data_available: Whether real data is available
- total_production_kg: Total production during baseline
- total_waste_kg: Total waste during baseline
"""
try:
result = await self.get("production/baseline", tenant_id=tenant_id)
if result:
logger.info("Retrieved production baseline data",
tenant_id=tenant_id,
data_available=result.get('data_available', False))
return result
except Exception as e:
logger.error("Error getting production baseline",
error=str(e), tenant_id=tenant_id)
return None
# ================================================================
# UTILITY METHODS
# ================================================================

View File

@@ -251,10 +251,33 @@ class RecipesServiceClient(BaseServiceClient):
error=str(e), tenant_id=tenant_id)
return []
# ================================================================
# COUNT AND STATISTICS
# ================================================================
async def count_recipes(self, tenant_id: str) -> int:
"""
Get the count of recipes for a tenant
Used for subscription limit tracking
Returns:
int: Number of recipes for the tenant
"""
try:
result = await self.get("recipes/count", tenant_id=tenant_id)
count = result.get('count', 0) if result else 0
logger.info("Retrieved recipe count from recipes service",
count=count, tenant_id=tenant_id)
return count
except Exception as e:
logger.error("Error getting recipe count",
error=str(e), tenant_id=tenant_id)
return 0
# ================================================================
# UTILITY METHODS
# ================================================================
async def health_check(self) -> bool:
"""Check if recipes service is healthy"""
try:

View File

@@ -381,24 +381,47 @@ class SuppliersServiceClient(BaseServiceClient):
# ================================================================
# ALERTS AND NOTIFICATIONS
# ================================================================
async def acknowledge_alert(self, tenant_id: str, alert_id: str) -> Optional[Dict[str, Any]]:
"""Acknowledge a supplier-related alert"""
try:
result = await self.post(f"suppliers/alerts/{alert_id}/acknowledge", data={}, tenant_id=tenant_id)
if result:
logger.info("Acknowledged supplier alert",
logger.info("Acknowledged supplier alert",
alert_id=alert_id, tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error acknowledging supplier alert",
logger.error("Error acknowledging supplier alert",
error=str(e), alert_id=alert_id, tenant_id=tenant_id)
return None
# ================================================================
# COUNT AND STATISTICS
# ================================================================
async def count_suppliers(self, tenant_id: str) -> int:
"""
Get the count of suppliers for a tenant
Used for subscription limit tracking
Returns:
int: Number of suppliers for the tenant
"""
try:
result = await self.get("suppliers/count", tenant_id=tenant_id)
count = result.get('count', 0) if result else 0
logger.info("Retrieved supplier count from suppliers service",
count=count, tenant_id=tenant_id)
return count
except Exception as e:
logger.error("Error getting supplier count",
error=str(e), tenant_id=tenant_id)
return 0
# ================================================================
# UTILITY METHODS
# ================================================================
async def health_check(self) -> bool:
"""Check if suppliers service is healthy"""
try:

View File

@@ -0,0 +1,220 @@
# shared/clients/tenant_client.py
"""
Tenant Service Client for Inter-Service Communication
Provides access to tenant settings and configuration from other services
"""
import structlog
from typing import Dict, Any, Optional
from uuid import UUID
from shared.clients.base_service_client import BaseServiceClient
from shared.config.base import BaseServiceSettings
logger = structlog.get_logger()
class TenantServiceClient(BaseServiceClient):
"""Client for communicating with the Tenant Service"""
def __init__(self, config: BaseServiceSettings):
super().__init__("tenant", config)
def get_service_base_path(self) -> str:
return "/api/v1"
# ================================================================
# TENANT SETTINGS ENDPOINTS
# ================================================================
async def get_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""
Get all settings for a tenant
Args:
tenant_id: Tenant ID (UUID as string)
Returns:
Dictionary with all settings categories
"""
try:
result = await self.get("settings", tenant_id=tenant_id)
if result:
logger.info("Retrieved all settings from tenant service",
tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error getting all settings",
error=str(e), tenant_id=tenant_id)
return None
async def get_category_settings(self, tenant_id: str, category: str) -> Optional[Dict[str, Any]]:
"""
Get settings for a specific category
Args:
tenant_id: Tenant ID (UUID as string)
category: Category name (procurement, inventory, production, supplier, pos, order)
Returns:
Dictionary with category settings
"""
try:
result = await self.get(f"settings/{category}", tenant_id=tenant_id)
if result:
logger.info("Retrieved category settings from tenant service",
tenant_id=tenant_id,
category=category)
return result
except Exception as e:
logger.error("Error getting category settings",
error=str(e), tenant_id=tenant_id, category=category)
return None
async def get_procurement_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get procurement settings for a tenant"""
result = await self.get_category_settings(tenant_id, "procurement")
return result.get('settings', {}) if result else {}
async def get_inventory_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get inventory settings for a tenant"""
result = await self.get_category_settings(tenant_id, "inventory")
return result.get('settings', {}) if result else {}
async def get_production_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get production settings for a tenant"""
result = await self.get_category_settings(tenant_id, "production")
return result.get('settings', {}) if result else {}
async def get_supplier_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get supplier settings for a tenant"""
result = await self.get_category_settings(tenant_id, "supplier")
return result.get('settings', {}) if result else {}
async def get_pos_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get POS settings for a tenant"""
result = await self.get_category_settings(tenant_id, "pos")
return result.get('settings', {}) if result else {}
async def get_order_settings(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""Get order settings for a tenant"""
result = await self.get_category_settings(tenant_id, "order")
return result.get('settings', {}) if result else {}
async def update_settings(self, tenant_id: str, settings_data: Dict[str, Any]) -> Optional[Dict[str, Any]]:
"""
Update settings for a tenant
Args:
tenant_id: Tenant ID (UUID as string)
settings_data: Settings data to update
Returns:
Updated settings dictionary
"""
try:
result = await self.put("settings", data=settings_data, tenant_id=tenant_id)
if result:
logger.info("Updated tenant settings",
tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error updating tenant settings",
error=str(e), tenant_id=tenant_id)
return None
async def update_category_settings(
self,
tenant_id: str,
category: str,
settings_data: Dict[str, Any]
) -> Optional[Dict[str, Any]]:
"""
Update settings for a specific category
Args:
tenant_id: Tenant ID (UUID as string)
category: Category name
settings_data: Settings data to update
Returns:
Updated settings dictionary
"""
try:
result = await self.put(f"settings/{category}", data=settings_data, tenant_id=tenant_id)
if result:
logger.info("Updated category settings",
tenant_id=tenant_id,
category=category)
return result
except Exception as e:
logger.error("Error updating category settings",
error=str(e), tenant_id=tenant_id, category=category)
return None
async def reset_category_settings(self, tenant_id: str, category: str) -> Optional[Dict[str, Any]]:
"""
Reset category settings to default values
Args:
tenant_id: Tenant ID (UUID as string)
category: Category name
Returns:
Reset settings dictionary
"""
try:
result = await self.post(f"settings/{category}/reset", data={}, tenant_id=tenant_id)
if result:
logger.info("Reset category settings to defaults",
tenant_id=tenant_id,
category=category)
return result
except Exception as e:
logger.error("Error resetting category settings",
error=str(e), tenant_id=tenant_id, category=category)
return None
# ================================================================
# TENANT MANAGEMENT
# ================================================================
async def get_tenant(self, tenant_id: str) -> Optional[Dict[str, Any]]:
"""
Get tenant details
Args:
tenant_id: Tenant ID (UUID as string)
Returns:
Tenant data dictionary
"""
try:
# The tenant endpoint is not tenant-scoped, it's a direct path
result = await self._make_request("GET", f"tenants/{tenant_id}")
if result:
logger.info("Retrieved tenant details",
tenant_id=tenant_id)
return result
except Exception as e:
logger.error("Error getting tenant details",
error=str(e), tenant_id=tenant_id)
return None
# ================================================================
# UTILITY METHODS
# ================================================================
async def health_check(self) -> bool:
"""Check if tenant service is healthy"""
try:
result = await self.get("../health") # Health endpoint is not tenant-scoped
return result is not None
except Exception as e:
logger.error("Tenant service health check failed", error=str(e))
return False
# Factory function for dependency injection
def create_tenant_client(config: BaseServiceSettings) -> TenantServiceClient:
"""Create tenant service client instance"""
return TenantServiceClient(config)