demo seed change
This commit is contained in:
@@ -3,7 +3,7 @@
|
||||
Production Batches API - ATOMIC CRUD operations on ProductionBatch model
|
||||
"""
|
||||
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query
|
||||
from fastapi import APIRouter, Depends, HTTPException, Path, Query, Request
|
||||
from typing import Optional
|
||||
from datetime import date
|
||||
from uuid import UUID
|
||||
@@ -26,8 +26,19 @@ from app.schemas.production import (
|
||||
)
|
||||
from app.core.config import settings
|
||||
from app.utils.cache import get_cached, set_cached, make_cache_key
|
||||
from app.services.production_alert_service import ProductionAlertService
|
||||
|
||||
logger = structlog.get_logger()
|
||||
|
||||
|
||||
async def get_production_alert_service(request: Request) -> ProductionAlertService:
|
||||
"""Dependency injection for production alert service"""
|
||||
# Get the alert service from app state, which is where it's stored during app startup
|
||||
alert_service = getattr(request.app.state, 'production_alert_service', None)
|
||||
if not alert_service:
|
||||
logger.warning("Production alert service not available in app state")
|
||||
return None
|
||||
return alert_service
|
||||
route_builder = RouteBuilder('production')
|
||||
router = APIRouter(tags=["production-batches"])
|
||||
|
||||
@@ -35,10 +46,11 @@ router = APIRouter(tags=["production-batches"])
|
||||
audit_logger = create_audit_logger("production-service", AuditLog)
|
||||
|
||||
|
||||
def get_production_service() -> ProductionService:
|
||||
def get_production_service(request: Request) -> ProductionService:
|
||||
"""Dependency injection for production service"""
|
||||
from app.core.database import database_manager
|
||||
return ProductionService(database_manager, settings)
|
||||
notification_service = getattr(request.app.state, 'notification_service', None)
|
||||
return ProductionService(database_manager, settings, notification_service)
|
||||
|
||||
|
||||
@router.get(
|
||||
@@ -108,12 +120,60 @@ async def create_production_batch(
|
||||
batch_data: ProductionBatchCreate,
|
||||
tenant_id: UUID = Path(...),
|
||||
current_user: dict = Depends(get_current_user_dep),
|
||||
production_service: ProductionService = Depends(get_production_service)
|
||||
production_service: ProductionService = Depends(get_production_service),
|
||||
request: Request = None,
|
||||
alert_service: ProductionAlertService = Depends(get_production_alert_service)
|
||||
):
|
||||
"""Create a new production batch"""
|
||||
try:
|
||||
batch = await production_service.create_production_batch(tenant_id, batch_data)
|
||||
|
||||
# Trigger Start Production alert
|
||||
if alert_service:
|
||||
try:
|
||||
# Generate reasoning data for the batch
|
||||
reasoning_data = {
|
||||
"type": "manual_creation",
|
||||
"parameters": {
|
||||
"product_name": batch.product_name,
|
||||
"planned_quantity": batch.planned_quantity,
|
||||
"priority": batch.priority.value if batch.priority else "MEDIUM"
|
||||
},
|
||||
"urgency": {
|
||||
"level": "normal",
|
||||
"ready_by_time": batch.planned_start_time.strftime('%H:%M') if batch.planned_start_time else "unknown"
|
||||
},
|
||||
"metadata": {
|
||||
"trigger_source": "manual_creation",
|
||||
"created_by": current_user.get("user_id", "unknown"),
|
||||
"is_ai_assisted": False
|
||||
}
|
||||
}
|
||||
|
||||
# Update batch with reasoning data
|
||||
from app.core.database import get_db
|
||||
db = next(get_db())
|
||||
batch.reasoning_data = reasoning_data
|
||||
await db.commit()
|
||||
|
||||
# Emit Start Production alert
|
||||
await alert_service.emit_start_production_alert(
|
||||
tenant_id=tenant_id,
|
||||
batch_id=batch.id,
|
||||
product_name=batch.product_name,
|
||||
batch_number=batch.batch_number,
|
||||
reasoning_data=reasoning_data,
|
||||
planned_start_time=batch.planned_start_time.isoformat() if batch.planned_start_time else None
|
||||
)
|
||||
|
||||
logger.info("Start Production alert triggered for batch",
|
||||
batch_id=str(batch.id), tenant_id=str(tenant_id))
|
||||
|
||||
except Exception as alert_error:
|
||||
logger.error("Failed to trigger Start Production alert",
|
||||
error=str(alert_error), batch_id=str(batch.id))
|
||||
# Don't fail the batch creation if alert fails
|
||||
|
||||
logger.info("Created production batch",
|
||||
batch_id=str(batch.id), tenant_id=str(tenant_id))
|
||||
|
||||
|
||||
Reference in New Issue
Block a user