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

@@ -34,21 +34,21 @@ class ForecastingBaseRepository(BaseRepository):
)
return await self.get_multi(skip=skip, limit=limit)
async def get_by_product_name(
async def get_by_inventory_product_id(
self,
tenant_id: str,
product_name: str,
inventory_product_id: str,
skip: int = 0,
limit: int = 100
) -> List:
"""Get records by tenant and product"""
if hasattr(self.model, 'product_name'):
"""Get records by tenant and inventory product"""
if hasattr(self.model, 'inventory_product_id'):
return await self.get_multi(
skip=skip,
limit=limit,
filters={
"tenant_id": tenant_id,
"product_name": product_name
"inventory_product_id": inventory_product_id
},
order_by="created_at",
order_desc=True
@@ -163,17 +163,17 @@ class ForecastingBaseRepository(BaseRepository):
# Get records by product if applicable
product_stats = {}
if hasattr(self.model, 'product_name'):
if hasattr(self.model, 'inventory_product_id'):
product_query = text(f"""
SELECT product_name, COUNT(*) as count
SELECT inventory_product_id, COUNT(*) as count
FROM {table_name}
WHERE tenant_id = :tenant_id
GROUP BY product_name
GROUP BY inventory_product_id
ORDER BY count DESC
""")
result = await self.session.execute(product_query, {"tenant_id": tenant_id})
product_stats = {row.product_name: row.count for row in result.fetchall()}
product_stats = {row.inventory_product_id: row.count for row in result.fetchall()}
return {
"total_records": total_records,
@@ -206,11 +206,11 @@ class ForecastingBaseRepository(BaseRepository):
if not isinstance(tenant_id, str) or len(tenant_id) < 1:
errors.append("Invalid tenant_id format")
# Validate product_name if present
if "product_name" in data and data["product_name"]:
product_name = data["product_name"]
if not isinstance(product_name, str) or len(product_name) < 1:
errors.append("Invalid product_name format")
# Validate inventory_product_id if present
if "inventory_product_id" in data and data["inventory_product_id"]:
inventory_product_id = data["inventory_product_id"]
if not isinstance(inventory_product_id, str) or len(inventory_product_id) < 1:
errors.append("Invalid inventory_product_id format")
# Validate dates if present - accept datetime objects, date objects, and date strings
date_fields = ["forecast_date", "created_at", "evaluation_date", "expires_at"]