Fix new services implementation 3

This commit is contained in:
Urtzi Alfaro
2025-08-14 16:47:34 +02:00
parent 0951547e92
commit 03737430ee
51 changed files with 657 additions and 982 deletions

View File

@@ -39,7 +39,7 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
logger.info(
"Created sales record",
record_id=record.id,
product=record.product_name,
inventory_product_id=record.inventory_product_id,
quantity=record.quantity_sold,
tenant_id=tenant_id
)
@@ -65,10 +65,16 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
stmt = stmt.where(SalesData.date >= query_params.start_date)
if query_params.end_date:
stmt = stmt.where(SalesData.date <= query_params.end_date)
if query_params.product_name:
stmt = stmt.where(SalesData.product_name.ilike(f"%{query_params.product_name}%"))
if query_params.product_category:
stmt = stmt.where(SalesData.product_category == query_params.product_category)
# Note: product_name queries now require joining with inventory service
# if query_params.product_name:
# # Would need to join with inventory service to filter by product name
# pass
# Note: product_category field was removed - filtering by category now requires inventory service
# if query_params.product_category:
# # Would need to join with inventory service to filter by product category
# pass
if hasattr(query_params, 'inventory_product_id') and query_params.inventory_product_id:
stmt = stmt.where(SalesData.inventory_product_id == query_params.inventory_product_id)
if query_params.location_id:
stmt = stmt.where(SalesData.location_id == query_params.location_id)
if query_params.sales_channel:
@@ -174,7 +180,7 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
# Top products
top_products_query = select(
SalesData.product_name,
SalesData.inventory_product_id, # Note: was product_name
func.sum(SalesData.revenue).label('revenue'),
func.sum(SalesData.quantity_sold).label('quantity')
).where(SalesData.tenant_id == tenant_id)
@@ -185,7 +191,7 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
top_products_query = top_products_query.where(SalesData.date <= end_date)
top_products_query = top_products_query.group_by(
SalesData.product_name
SalesData.inventory_product_id # Note: was product_name
).order_by(
desc(func.sum(SalesData.revenue))
).limit(10)
@@ -193,7 +199,7 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
top_products_result = await self.session.execute(top_products_query)
top_products = [
{
'product_name': row.product_name,
'inventory_product_id': str(row.inventory_product_id), # Note: was product_name
'revenue': float(row.revenue) if row.revenue else 0,
'quantity': row.quantity or 0
}
@@ -239,15 +245,12 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
async def get_product_categories(self, tenant_id: UUID) -> List[str]:
"""Get distinct product categories for a tenant"""
try:
stmt = select(SalesData.product_category).where(
and_(
SalesData.tenant_id == tenant_id,
SalesData.product_category.is_not(None)
)
).distinct()
result = await self.session.execute(stmt)
categories = [row[0] for row in result if row[0]]
# Note: product_category field was removed - categories now managed via inventory service
# This method should be updated to query categories from inventory service
# For now, return empty list to avoid breaking existing code
logger.warning("get_product_categories called but product_category field was removed",
tenant_id=tenant_id)
categories = []
return sorted(categories)
@@ -279,15 +282,18 @@ class SalesRepository(BaseRepository[SalesData, SalesDataCreate, SalesDataUpdate
async def get_product_statistics(self, tenant_id: str) -> List[Dict[str, Any]]:
"""Get product statistics for tenant"""
try:
stmt = select(SalesData.product_name).where(
# Note: product_name field was removed - product info now managed via inventory service
# This method should be updated to query products from inventory service
# For now, return inventory_product_ids to avoid breaking existing code
stmt = select(SalesData.inventory_product_id).where(
and_(
SalesData.tenant_id == tenant_id,
SalesData.product_name.is_not(None)
SalesData.inventory_product_id.is_not(None)
)
).distinct()
result = await self.session.execute(stmt)
products = [row[0] for row in result if row[0]]
products = [str(row[0]) for row in result if row[0]]
return sorted(products)

View File

@@ -53,9 +53,10 @@ class SalesDataCreate(SalesDataBase):
class SalesDataUpdate(BaseModel):
"""Schema for updating sales data"""
product_name: Optional[str] = Field(None, min_length=1, max_length=255)
product_category: Optional[str] = Field(None, max_length=100)
product_sku: Optional[str] = Field(None, max_length=100)
# Note: product_name and product_category fields removed - use inventory service for product management
# product_name: Optional[str] = Field(None, min_length=1, max_length=255) # DEPRECATED
# product_category: Optional[str] = Field(None, max_length=100) # DEPRECATED
# product_sku: Optional[str] = Field(None, max_length=100) # DEPRECATED - use inventory service
quantity_sold: Optional[int] = Field(None, gt=0)
unit_price: Optional[Decimal] = Field(None, ge=0)
@@ -98,8 +99,10 @@ class SalesDataQuery(BaseModel):
"""Schema for sales data queries"""
start_date: Optional[datetime] = None
end_date: Optional[datetime] = None
product_name: Optional[str] = None
product_category: Optional[str] = None
# Note: product_name and product_category filtering now requires inventory service integration
# product_name: Optional[str] = None # DEPRECATED - use inventory_product_id or join with inventory service
# product_category: Optional[str] = None # DEPRECATED - use inventory service categories
inventory_product_id: Optional[UUID] = None # Filter by specific inventory product ID
location_id: Optional[str] = None
sales_channel: Optional[str] = None
source: Optional[str] = None
@@ -136,7 +139,8 @@ class SalesAnalytics(BaseModel):
class ProductSalesAnalytics(BaseModel):
"""Product-specific sales analytics"""
product_name: str
inventory_product_id: UUID # Reference to inventory service product
# Note: product_name can be fetched from inventory service using inventory_product_id
total_revenue: Decimal
total_quantity: int
total_transactions: int