Fix some UI issues 2
This commit is contained in:
@@ -93,13 +93,17 @@ class ProductionService:
|
||||
batch_dict = batch_data.model_dump()
|
||||
batch_dict["tenant_id"] = tenant_id
|
||||
|
||||
# Validate recipe exists if provided
|
||||
# Validate recipe exists and get quality configuration
|
||||
recipe_quality_config = None
|
||||
if batch_data.recipe_id:
|
||||
recipe_details = await self.recipes_client.get_recipe_by_id(
|
||||
str(tenant_id), str(batch_data.recipe_id)
|
||||
)
|
||||
if not recipe_details:
|
||||
raise ValueError(f"Recipe {batch_data.recipe_id} not found")
|
||||
|
||||
# Extract quality configuration from recipe
|
||||
recipe_quality_config = recipe_details.get("quality_check_configuration")
|
||||
|
||||
# Check ingredient availability
|
||||
if batch_data.recipe_id:
|
||||
@@ -118,10 +122,15 @@ class ProductionService:
|
||||
|
||||
# Create the batch
|
||||
batch = await batch_repo.create_batch(batch_dict)
|
||||
|
||||
logger.info("Production batch created",
|
||||
batch_id=str(batch.id), tenant_id=str(tenant_id))
|
||||
|
||||
|
||||
# Inherit quality templates from recipe if configured
|
||||
if recipe_quality_config and recipe_quality_config.get("auto_create_quality_checks", True):
|
||||
await self._setup_batch_quality_checks(session, batch, recipe_quality_config, tenant_id)
|
||||
|
||||
logger.info("Production batch created with quality inheritance",
|
||||
batch_id=str(batch.id), tenant_id=str(tenant_id),
|
||||
has_quality_config=bool(recipe_quality_config))
|
||||
|
||||
return batch
|
||||
|
||||
except Exception as e:
|
||||
@@ -129,6 +138,132 @@ class ProductionService:
|
||||
error=str(e), tenant_id=str(tenant_id))
|
||||
raise
|
||||
|
||||
async def _setup_batch_quality_checks(
|
||||
self,
|
||||
session,
|
||||
batch: ProductionBatch,
|
||||
quality_config: Dict[str, Any],
|
||||
tenant_id: UUID
|
||||
):
|
||||
"""Set up quality checks for a production batch based on recipe configuration"""
|
||||
try:
|
||||
# Initialize pending and completed quality checks structures
|
||||
pending_quality_checks = {}
|
||||
completed_quality_checks = {}
|
||||
|
||||
# Process each stage configuration
|
||||
stages = quality_config.get("stages", {})
|
||||
for stage_name, stage_config in stages.items():
|
||||
template_ids = stage_config.get("template_ids", [])
|
||||
required_checks = stage_config.get("required_checks", [])
|
||||
optional_checks = stage_config.get("optional_checks", [])
|
||||
min_quality_score = stage_config.get("min_quality_score")
|
||||
blocking_on_failure = stage_config.get("blocking_on_failure", True)
|
||||
|
||||
# Set up pending checks for this stage
|
||||
if template_ids or required_checks or optional_checks:
|
||||
pending_quality_checks[stage_name] = {
|
||||
"template_ids": [str(tid) for tid in template_ids],
|
||||
"required_checks": required_checks,
|
||||
"optional_checks": optional_checks,
|
||||
"min_quality_score": min_quality_score,
|
||||
"blocking_on_failure": blocking_on_failure,
|
||||
"status": "pending"
|
||||
}
|
||||
|
||||
# Initialize completed structure for this stage
|
||||
completed_quality_checks[stage_name] = {
|
||||
"checks": [],
|
||||
"overall_score": None,
|
||||
"passed": None,
|
||||
"completed_at": None
|
||||
}
|
||||
|
||||
# Update batch with quality check configuration
|
||||
batch.pending_quality_checks = pending_quality_checks
|
||||
batch.completed_quality_checks = completed_quality_checks
|
||||
|
||||
# Save the updated batch
|
||||
await session.commit()
|
||||
|
||||
logger.info("Quality checks setup completed for batch",
|
||||
batch_id=str(batch.id),
|
||||
stages_configured=list(stages.keys()),
|
||||
total_templates=sum(len(stage.get("template_ids", [])) for stage in stages.values()))
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error setting up batch quality checks",
|
||||
error=str(e), batch_id=str(batch.id))
|
||||
raise
|
||||
|
||||
async def update_batch_stage_with_quality_checks(
|
||||
self,
|
||||
tenant_id: UUID,
|
||||
batch_id: UUID,
|
||||
new_stage: str
|
||||
) -> ProductionBatch:
|
||||
"""Update batch stage and create required quality checks"""
|
||||
try:
|
||||
async with self.database_manager.get_session() as session:
|
||||
batch_repo = ProductionBatchRepository(session)
|
||||
|
||||
# Get the batch
|
||||
batch = await batch_repo.get_batch(tenant_id, batch_id)
|
||||
if not batch:
|
||||
raise ValueError(f"Batch {batch_id} not found")
|
||||
|
||||
# Update current stage
|
||||
old_stage = batch.current_process_stage
|
||||
batch.current_process_stage = new_stage
|
||||
|
||||
# Check if there are pending quality checks for this stage
|
||||
pending_checks = batch.pending_quality_checks or {}
|
||||
stage_checks = pending_checks.get(new_stage)
|
||||
|
||||
if stage_checks and stage_checks.get("template_ids"):
|
||||
# Create quality check records from templates
|
||||
from app.repositories.quality_template_repository import QualityTemplateRepository
|
||||
|
||||
template_repo = QualityTemplateRepository(session)
|
||||
quality_repo = QualityCheckRepository(session)
|
||||
|
||||
template_ids = [UUID(tid) for tid in stage_checks["template_ids"]]
|
||||
templates = await template_repo.get_templates_by_ids(str(tenant_id), template_ids)
|
||||
|
||||
# Create quality checks for each template
|
||||
for template in templates:
|
||||
quality_check_data = {
|
||||
"tenant_id": tenant_id,
|
||||
"batch_id": batch_id,
|
||||
"template_id": template.id,
|
||||
"check_type": template.check_type,
|
||||
"process_stage": new_stage,
|
||||
"check_time": datetime.utcnow(),
|
||||
"quality_score": 0.0, # To be filled when check is performed
|
||||
"pass_fail": False, # To be updated when check is performed
|
||||
"defect_count": 0,
|
||||
"target_weight": template.target_value,
|
||||
"target_temperature": template.target_value if template.check_type == "temperature" else None,
|
||||
"tolerance_percentage": template.tolerance_percentage
|
||||
}
|
||||
|
||||
await quality_repo.create_quality_check(quality_check_data)
|
||||
|
||||
logger.info("Created quality checks for batch stage transition",
|
||||
batch_id=str(batch_id),
|
||||
stage=new_stage,
|
||||
checks_created=len(templates))
|
||||
|
||||
# Save batch changes
|
||||
await session.commit()
|
||||
|
||||
return batch
|
||||
|
||||
except Exception as e:
|
||||
logger.error("Error updating batch stage with quality checks",
|
||||
error=str(e), batch_id=str(batch_id), new_stage=new_stage)
|
||||
raise
|
||||
|
||||
async def get_production_batches_list(
|
||||
self,
|
||||
tenant_id: UUID,
|
||||
|
||||
Reference in New Issue
Block a user