Files
bakery-ia/services/forecasting/app/services/sales_client.py
2025-11-18 07:17:17 +01:00

98 lines
3.1 KiB
Python

# ================================================================
# services/forecasting/app/services/sales_client.py
# ================================================================
"""
Sales Client for Forecasting Service
Wrapper around shared sales client with forecasting-specific methods
"""
from typing import List, Dict, Any
from datetime import datetime
import structlog
import uuid
from shared.clients.sales_client import SalesServiceClient
from shared.config.base import BaseServiceSettings
logger = structlog.get_logger()
class SalesClient:
"""Client for fetching sales data from sales service"""
def __init__(self):
"""Initialize sales client"""
# Load configuration
config = BaseServiceSettings()
self.client = SalesServiceClient(config, calling_service_name="forecasting")
async def get_sales_by_date_range(
self,
tenant_id: uuid.UUID,
start_date: datetime,
end_date: datetime,
product_id: uuid.UUID = None
) -> List[Dict[str, Any]]:
"""
Get sales data for a date range
Args:
tenant_id: Tenant identifier
start_date: Start of date range
end_date: End of date range
product_id: Optional product filter
Returns:
List of sales records
"""
try:
# Convert datetime to ISO format strings
start_date_str = start_date.isoformat() if start_date else None
end_date_str = end_date.isoformat() if end_date else None
product_id_str = str(product_id) if product_id else None
# Use the paginated method to get all sales data
sales_data = await self.client.get_all_sales_data(
tenant_id=str(tenant_id),
start_date=start_date_str,
end_date=end_date_str,
product_id=product_id_str,
aggregation="none", # Get raw data without aggregation
page_size=1000,
max_pages=100
)
if not sales_data:
logger.info(
"No sales data found for date range",
tenant_id=tenant_id,
start_date=start_date.isoformat(),
end_date=end_date.isoformat()
)
return []
logger.info(
"Retrieved sales data",
tenant_id=tenant_id,
records_count=len(sales_data),
start_date=start_date.isoformat(),
end_date=end_date.isoformat()
)
return sales_data
except Exception as e:
logger.error(
"Failed to fetch sales data",
tenant_id=tenant_id,
error=str(e),
error_type=type(e).__name__
)
# Return empty list instead of raising to allow validation to continue
return []
async def close(self):
"""Close the client connection"""
if hasattr(self.client, 'close'):
await self.client.close()