Add quality template logic

This commit is contained in:
Urtzi Alfaro
2025-09-24 16:42:23 +02:00
parent 474d7176bf
commit 2de1e6ce40
11 changed files with 450 additions and 228 deletions

View File

@@ -22,7 +22,6 @@ from app.schemas.production import (
ProductionStatusEnum
)
from app.core.config import settings
from .quality_templates import router as quality_templates_router
logger = structlog.get_logger()
@@ -1096,6 +1095,211 @@ async def get_yield_metrics(
return metrics
except Exception as e:
logger.error("Error getting yield metrics",
logger.error("Error getting yield metrics",
error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to get yield metrics")
raise HTTPException(status_code=500, detail="Failed to get yield metrics")
# ================================================================
# QUALITY TEMPLATES ENDPOINTS
# ================================================================
from app.repositories.quality_template_repository import QualityTemplateRepository
from app.schemas.quality_templates import (
QualityCheckTemplateCreate,
QualityCheckTemplateUpdate,
QualityCheckTemplateResponse,
QualityCheckTemplateList
)
@router.get("/tenants/{tenant_id}/production/quality-templates", response_model=QualityCheckTemplateList)
async def get_quality_templates(
tenant_id: UUID = Path(...),
stage: Optional[str] = Query(None, description="Filter by process stage"),
check_type: Optional[str] = Query(None, description="Filter by check type"),
is_active: Optional[bool] = Query(True, description="Filter by active status"),
skip: int = Query(0, ge=0),
limit: int = Query(100, ge=1, le=1000),
current_user: dict = Depends(get_current_user_dep),
db=Depends(get_db)
):
"""Get quality check templates for tenant"""
try:
repo = QualityTemplateRepository(db)
# Convert stage string to ProcessStage enum if provided
stage_enum = None
if stage:
try:
stage_enum = ProcessStage(stage)
except ValueError:
raise HTTPException(status_code=400, detail=f"Invalid stage: {stage}")
templates, total = await repo.get_templates_by_tenant(
tenant_id=str(tenant_id),
stage=stage_enum,
check_type=check_type,
is_active=is_active,
skip=skip,
limit=limit
)
return QualityCheckTemplateList(
templates=[QualityCheckTemplateResponse.from_orm(t) for t in templates],
total=total,
skip=skip,
limit=limit
)
except HTTPException:
raise
except Exception as e:
logger.error("Error getting quality templates",
error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to get quality templates")
@router.post("/tenants/{tenant_id}/production/quality-templates", response_model=QualityCheckTemplateResponse)
async def create_quality_template(
template_data: QualityCheckTemplateCreate,
tenant_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
db=Depends(get_db)
):
"""Create a new quality check template"""
try:
repo = QualityTemplateRepository(db)
# Add tenant_id to the template data
create_data = template_data.dict()
create_data['tenant_id'] = str(tenant_id)
template = await repo.create(create_data)
return QualityCheckTemplateResponse.from_orm(template)
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error("Error creating quality template",
error=str(e), tenant_id=str(tenant_id))
raise HTTPException(status_code=500, detail="Failed to create quality template")
@router.get("/tenants/{tenant_id}/production/quality-templates/{template_id}", response_model=QualityCheckTemplateResponse)
async def get_quality_template(
tenant_id: UUID = Path(...),
template_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
db=Depends(get_db)
):
"""Get a specific quality check template"""
try:
repo = QualityTemplateRepository(db)
template = await repo.get_by_tenant_and_id(str(tenant_id), template_id)
if not template:
raise HTTPException(status_code=404, detail="Quality template not found")
return QualityCheckTemplateResponse.from_orm(template)
except HTTPException:
raise
except Exception as e:
logger.error("Error getting quality template",
error=str(e), tenant_id=str(tenant_id), template_id=str(template_id))
raise HTTPException(status_code=500, detail="Failed to get quality template")
@router.put("/tenants/{tenant_id}/production/quality-templates/{template_id}", response_model=QualityCheckTemplateResponse)
async def update_quality_template(
template_data: QualityCheckTemplateUpdate,
tenant_id: UUID = Path(...),
template_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
db=Depends(get_db)
):
"""Update a quality check template"""
try:
repo = QualityTemplateRepository(db)
# First check if template exists and belongs to tenant
existing = await repo.get_by_tenant_and_id(str(tenant_id), template_id)
if not existing:
raise HTTPException(status_code=404, detail="Quality template not found")
template = await repo.update(template_id, template_data.dict(exclude_unset=True))
return QualityCheckTemplateResponse.from_orm(template)
except HTTPException:
raise
except ValueError as e:
raise HTTPException(status_code=400, detail=str(e))
except Exception as e:
logger.error("Error updating quality template",
error=str(e), tenant_id=str(tenant_id), template_id=str(template_id))
raise HTTPException(status_code=500, detail="Failed to update quality template")
@router.delete("/tenants/{tenant_id}/production/quality-templates/{template_id}")
async def delete_quality_template(
tenant_id: UUID = Path(...),
template_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
db=Depends(get_db)
):
"""Delete a quality check template"""
try:
repo = QualityTemplateRepository(db)
# First check if template exists and belongs to tenant
existing = await repo.get_by_tenant_and_id(str(tenant_id), template_id)
if not existing:
raise HTTPException(status_code=404, detail="Quality template not found")
await repo.delete(template_id)
return {"message": "Quality template deleted successfully"}
except HTTPException:
raise
except Exception as e:
logger.error("Error deleting quality template",
error=str(e), tenant_id=str(tenant_id), template_id=str(template_id))
raise HTTPException(status_code=500, detail="Failed to delete quality template")
@router.post("/tenants/{tenant_id}/production/quality-templates/{template_id}/duplicate", response_model=QualityCheckTemplateResponse)
async def duplicate_quality_template(
tenant_id: UUID = Path(...),
template_id: UUID = Path(...),
current_user: dict = Depends(get_current_user_dep),
db=Depends(get_db)
):
"""Duplicate an existing quality check template"""
try:
repo = QualityTemplateRepository(db)
# Get original template
original = await repo.get_by_tenant_and_id(str(tenant_id), template_id)
if not original:
raise HTTPException(status_code=404, detail="Quality template not found")
# Create duplicate data
duplicate_data = {
"tenant_id": original.tenant_id,
"name": f"{original.name} (Copy)",
"template_code": None, # Will be auto-generated
"check_type": original.check_type,
"category": original.category,
"description": original.description,
"instructions": original.instructions,
"criteria": original.criteria,
"is_required": original.is_required,
"is_critical": original.is_critical,
"weight": original.weight,
"min_value": original.min_value,
"max_value": original.max_value,
"unit": original.unit,
"tolerance_percentage": original.tolerance_percentage,
"applicable_stages": original.applicable_stages,
"created_by": original.created_by
}
template = await repo.create(duplicate_data)
return QualityCheckTemplateResponse.from_orm(template)
except HTTPException:
raise
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")