Improve the frontend and repository layer
This commit is contained in:
@@ -12,7 +12,7 @@ from shared.auth.decorators import get_current_user_dep
|
||||
from shared.auth.access_control import require_user_role
|
||||
from shared.routing import RouteBuilder, RouteCategory
|
||||
from app.core.database import get_db
|
||||
from app.repositories.quality_template_repository import QualityTemplateRepository
|
||||
from app.services.quality_template_service import QualityTemplateService
|
||||
from app.models.production import ProcessStage, QualityCheckTemplate
|
||||
from app.schemas.quality_templates import (
|
||||
QualityCheckTemplateCreate,
|
||||
@@ -52,9 +52,9 @@ async def list_quality_templates(
|
||||
- is_active: Filter by active status (default: True)
|
||||
"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
templates, total = await repo.get_templates_by_tenant(
|
||||
templates, total = await service.get_templates(
|
||||
tenant_id=str(tenant_id),
|
||||
stage=stage,
|
||||
check_type=check_type.value if check_type else None,
|
||||
@@ -98,29 +98,18 @@ async def create_quality_template(
|
||||
):
|
||||
"""Create a new quality check template"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
# Check if template code already exists (if provided)
|
||||
if template_data.template_code:
|
||||
code_exists = await repo.check_template_code_exists(
|
||||
tenant_id=str(tenant_id),
|
||||
template_code=template_data.template_code
|
||||
)
|
||||
if code_exists:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Template code '{template_data.template_code}' already exists"
|
||||
)
|
||||
|
||||
# Create template
|
||||
# Add created_by from current user
|
||||
template_dict = template_data.dict()
|
||||
template_dict['tenant_id'] = str(tenant_id)
|
||||
template_dict['created_by'] = UUID(current_user["sub"])
|
||||
template_create = QualityCheckTemplateCreate(**template_dict)
|
||||
|
||||
template = QualityCheckTemplate(**template_dict)
|
||||
db.add(template)
|
||||
await db.commit()
|
||||
await db.refresh(template)
|
||||
# Create template via service (handles validation and business rules)
|
||||
template = await service.create_template(
|
||||
tenant_id=str(tenant_id),
|
||||
template_data=template_create
|
||||
)
|
||||
|
||||
logger.info("Created quality template",
|
||||
template_id=str(template.id),
|
||||
@@ -129,10 +118,13 @@ async def create_quality_template(
|
||||
|
||||
return QualityCheckTemplateResponse.from_orm(template)
|
||||
|
||||
except HTTPException:
|
||||
raise
|
||||
except ValueError as e:
|
||||
# Business rule validation errors
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e)
|
||||
)
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
logger.error("Error creating quality template",
|
||||
error=str(e), tenant_id=str(tenant_id))
|
||||
raise HTTPException(
|
||||
@@ -153,9 +145,9 @@ async def get_quality_template(
|
||||
):
|
||||
"""Get a specific quality check template"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
template = await repo.get_by_tenant_and_id(
|
||||
template = await service.get_template(
|
||||
tenant_id=str(tenant_id),
|
||||
template_id=template_id
|
||||
)
|
||||
@@ -195,12 +187,13 @@ async def update_quality_template(
|
||||
):
|
||||
"""Update a quality check template"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
# Get existing template
|
||||
template = await repo.get_by_tenant_and_id(
|
||||
# Update template via service (handles validation and business rules)
|
||||
template = await service.update_template(
|
||||
tenant_id=str(tenant_id),
|
||||
template_id=template_id
|
||||
template_id=template_id,
|
||||
template_data=template_data
|
||||
)
|
||||
|
||||
if not template:
|
||||
@@ -209,37 +202,21 @@ async def update_quality_template(
|
||||
detail="Quality template not found"
|
||||
)
|
||||
|
||||
# Check if template code already exists (if being updated)
|
||||
if template_data.template_code and template_data.template_code != template.template_code:
|
||||
code_exists = await repo.check_template_code_exists(
|
||||
tenant_id=str(tenant_id),
|
||||
template_code=template_data.template_code,
|
||||
exclude_id=template_id
|
||||
)
|
||||
if code_exists:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=f"Template code '{template_data.template_code}' already exists"
|
||||
)
|
||||
|
||||
# Update template fields
|
||||
update_data = template_data.dict(exclude_unset=True)
|
||||
for field, value in update_data.items():
|
||||
setattr(template, field, value)
|
||||
|
||||
await db.commit()
|
||||
await db.refresh(template)
|
||||
|
||||
logger.info("Updated quality template",
|
||||
template_id=str(template_id),
|
||||
tenant_id=str(tenant_id))
|
||||
|
||||
return QualityCheckTemplateResponse.from_orm(template)
|
||||
|
||||
except ValueError as e:
|
||||
# Business rule validation errors
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail=str(e)
|
||||
)
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
logger.error("Error updating quality template",
|
||||
error=str(e),
|
||||
template_id=str(template_id),
|
||||
@@ -262,31 +239,27 @@ async def delete_quality_template(
|
||||
db = Depends(get_db)
|
||||
):
|
||||
"""
|
||||
Delete a quality check template (soft delete by setting is_active to False)
|
||||
Delete a quality check template
|
||||
|
||||
Note: For safety, this performs a soft delete. Hard deletes would require
|
||||
checking for dependencies in recipes and production batches.
|
||||
Note: Service layer determines whether to use soft or hard delete
|
||||
based on business rules (checking dependencies, etc.)
|
||||
"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
# Get existing template
|
||||
template = await repo.get_by_tenant_and_id(
|
||||
# Delete template via service (handles business rules)
|
||||
success = await service.delete_template(
|
||||
tenant_id=str(tenant_id),
|
||||
template_id=template_id
|
||||
)
|
||||
|
||||
if not template:
|
||||
if not success:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Quality template not found"
|
||||
)
|
||||
|
||||
# Soft delete by marking as inactive
|
||||
template.is_active = False
|
||||
await db.commit()
|
||||
|
||||
logger.info("Deleted quality template (soft delete)",
|
||||
logger.info("Deleted quality template",
|
||||
template_id=str(template_id),
|
||||
tenant_id=str(tenant_id))
|
||||
|
||||
@@ -322,9 +295,9 @@ async def get_templates_for_stage(
|
||||
):
|
||||
"""Get all quality templates applicable to a specific process stage"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
templates = await repo.get_templates_for_stage(
|
||||
templates = await service.get_templates_for_stage(
|
||||
tenant_id=str(tenant_id),
|
||||
stage=stage,
|
||||
is_active=is_active
|
||||
@@ -367,50 +340,20 @@ async def duplicate_quality_template(
|
||||
):
|
||||
"""Duplicate an existing quality check template"""
|
||||
try:
|
||||
repo = QualityTemplateRepository(db)
|
||||
service = QualityTemplateService(db)
|
||||
|
||||
# Get existing template
|
||||
original = await repo.get_by_tenant_and_id(
|
||||
# Duplicate template via service (handles business rules)
|
||||
duplicate = await service.duplicate_template(
|
||||
tenant_id=str(tenant_id),
|
||||
template_id=template_id
|
||||
)
|
||||
|
||||
if not original:
|
||||
if not duplicate:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
detail="Quality template not found"
|
||||
)
|
||||
|
||||
# Create duplicate
|
||||
duplicate_data = {
|
||||
'tenant_id': original.tenant_id,
|
||||
'name': f"{original.name} (Copy)",
|
||||
'template_code': f"{original.template_code}_copy" if original.template_code else None,
|
||||
'check_type': original.check_type,
|
||||
'category': original.category,
|
||||
'description': original.description,
|
||||
'instructions': original.instructions,
|
||||
'parameters': original.parameters,
|
||||
'thresholds': original.thresholds,
|
||||
'scoring_criteria': original.scoring_criteria,
|
||||
'is_active': original.is_active,
|
||||
'is_required': original.is_required,
|
||||
'is_critical': original.is_critical,
|
||||
'weight': original.weight,
|
||||
'min_value': original.min_value,
|
||||
'max_value': original.max_value,
|
||||
'target_value': original.target_value,
|
||||
'unit': original.unit,
|
||||
'tolerance_percentage': original.tolerance_percentage,
|
||||
'applicable_stages': original.applicable_stages,
|
||||
'created_by': UUID(current_user["sub"])
|
||||
}
|
||||
|
||||
duplicate = QualityCheckTemplate(**duplicate_data)
|
||||
db.add(duplicate)
|
||||
await db.commit()
|
||||
await db.refresh(duplicate)
|
||||
|
||||
logger.info("Duplicated quality template",
|
||||
original_id=str(template_id),
|
||||
duplicate_id=str(duplicate.id),
|
||||
@@ -421,7 +364,6 @@ async def duplicate_quality_template(
|
||||
except HTTPException:
|
||||
raise
|
||||
except Exception as e:
|
||||
await db.rollback()
|
||||
logger.error("Error duplicating quality template",
|
||||
error=str(e),
|
||||
template_id=str(template_id),
|
||||
|
||||
Reference in New Issue
Block a user