Fix few issues

This commit is contained in:
Urtzi Alfaro
2025-09-26 12:12:17 +02:00
parent d573c38621
commit a27f159e24
32 changed files with 2694 additions and 575 deletions

View File

@@ -1302,4 +1302,169 @@ async def duplicate_quality_template(
except Exception as e:
logger.error("Error duplicating quality template",
error=str(e), tenant_id=str(tenant_id), template_id=str(template_id))
raise HTTPException(status_code=500, detail="Failed to duplicate quality template")
raise HTTPException(status_code=500, detail="Failed to duplicate quality template")
# ================================================================
# TRANSFORMATION ENDPOINTS
# ================================================================
@router.post("/tenants/{tenant_id}/production/batches/{batch_id}/complete-with-transformation", response_model=dict)
async def complete_batch_with_transformation(
transformation_data: Optional[dict] = None,
completion_data: Optional[dict] = None,
tenant_id: UUID = Path(...),
batch_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
production_service: ProductionService = Depends(get_production_service)
):
"""Complete batch and apply transformation (e.g. par-baked to fully baked)"""
try:
result = await production_service.complete_production_batch_with_transformation(
tenant_id, batch_id, completion_data, transformation_data
)
logger.info("Completed batch with transformation",
batch_id=str(batch_id),
has_transformation=bool(transformation_data),
tenant_id=str(tenant_id))
return result
except ValueError as e:
logger.warning("Invalid batch completion with transformation", error=str(e), batch_id=str(batch_id))
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error("Error completing batch with transformation",
error=str(e), batch_id=str(batch_id), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to complete batch with transformation")
@router.post("/tenants/{tenant_id}/production/transformations/par-baked-to-fresh", response_model=dict)
async def transform_par_baked_products(
source_ingredient_id: UUID = Query(..., description="Par-baked ingredient ID"),
target_ingredient_id: UUID = Query(..., description="Fresh baked ingredient ID"),
quantity: float = Query(..., gt=0, description="Quantity to transform"),
batch_reference: Optional[str] = Query(None, description="Production batch reference"),
expiration_hours: int = Query(24, ge=1, le=72, description="Hours until expiration after transformation"),
notes: Optional[str] = Query(None, description="Transformation notes"),
tenant_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
production_service: ProductionService = Depends(get_production_service)
):
"""Transform par-baked products to fresh baked products"""
try:
result = await production_service.transform_par_baked_products(
tenant_id=tenant_id,
source_ingredient_id=source_ingredient_id,
target_ingredient_id=target_ingredient_id,
quantity=quantity,
batch_reference=batch_reference,
expiration_hours=expiration_hours,
notes=notes
)
if not result:
raise HTTPException(status_code=400, detail="Failed to create transformation")
logger.info("Transformed par-baked products to fresh",
transformation_id=result.get('transformation_id'),
quantity=quantity, tenant_id=str(tenant_id))
return result
except HTTPException:
raise
except ValueError as e:
logger.warning("Invalid transformation data", error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error("Error transforming par-baked products",
error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to transform par-baked products")
@router.get("/tenants/{tenant_id}/production/transformations", response_model=dict)
async def get_production_transformations(
tenant_id: UUID = Path(...),
days_back: int = Query(30, ge=1, le=365, description="Days back to retrieve transformations"),
limit: int = Query(100, ge=1, le=500, description="Maximum number of transformations to retrieve"),
current_user: dict = Depends(get_current_user_dep),
production_service: ProductionService = Depends(get_production_service)
):
"""Get transformations related to production processes"""
try:
transformations = await production_service.get_production_transformations(
tenant_id, days_back, limit
)
result = {
"transformations": transformations,
"total_count": len(transformations),
"period_days": days_back,
"retrieved_at": datetime.now().isoformat()
}
logger.info("Retrieved production transformations",
count=len(transformations), tenant_id=str(tenant_id))
return result
except Exception as e:
logger.error("Error getting production transformations",
error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to get production transformations")
@router.get("/tenants/{tenant_id}/production/analytics/transformation-efficiency", response_model=dict)
async def get_transformation_efficiency_analytics(
tenant_id: UUID = Path(...),
days_back: int = Query(30, ge=1, le=365, description="Days back for efficiency analysis"),
current_user: dict = Depends(get_current_user_dep),
production_service: ProductionService = Depends(get_production_service)
):
"""Get transformation efficiency metrics for analytics"""
try:
metrics = await production_service.get_transformation_efficiency_metrics(
tenant_id, days_back
)
logger.info("Retrieved transformation efficiency analytics",
total_transformations=metrics.get('total_transformations', 0),
tenant_id=str(tenant_id))
return metrics
except Exception as e:
logger.error("Error getting transformation efficiency analytics",
error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to get transformation efficiency analytics")
@router.get("/tenants/{tenant_id}/production/batches/{batch_id}/transformations", response_model=dict)
async def get_batch_transformations(
tenant_id: UUID = Path(...),
batch_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
production_service: ProductionService = Depends(get_production_service)
):
"""Get batch details with associated transformations"""
try:
result = await production_service.get_batch_with_transformations(tenant_id, batch_id)
if not result:
raise HTTPException(status_code=404, detail="Batch not found")
logger.info("Retrieved batch with transformations",
batch_id=str(batch_id),
transformation_count=result.get('transformation_count', 0),
tenant_id=str(tenant_id))
return result
except HTTPException:
raise
except Exception as e:
logger.error("Error getting batch transformations",
error=str(e), batch_id=str(batch_id), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to get batch transformations")