117 lines
3.7 KiB
Python
117 lines
3.7 KiB
Python
# services/recipes/app/api/ingredients.py
|
|
"""
|
|
API endpoints for ingredient-related operations (bridge to inventory service)
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Header, Query
|
|
from typing import List, Optional
|
|
from uuid import UUID
|
|
import logging
|
|
|
|
from ..services.inventory_client import InventoryClient
|
|
|
|
logger = logging.getLogger(__name__)
|
|
router = APIRouter()
|
|
|
|
|
|
def get_tenant_id(x_tenant_id: str = Header(...)) -> UUID:
|
|
"""Extract tenant ID from header"""
|
|
try:
|
|
return UUID(x_tenant_id)
|
|
except ValueError:
|
|
raise HTTPException(status_code=400, detail="Invalid tenant ID format")
|
|
|
|
|
|
@router.get("/search")
|
|
async def search_ingredients(
|
|
tenant_id: UUID = Depends(get_tenant_id),
|
|
search_term: Optional[str] = Query(None),
|
|
product_type: Optional[str] = Query(None),
|
|
category: Optional[str] = Query(None),
|
|
limit: int = Query(100, ge=1, le=1000),
|
|
offset: int = Query(0, ge=0)
|
|
):
|
|
"""Search ingredients from inventory service"""
|
|
try:
|
|
inventory_client = InventoryClient()
|
|
|
|
# This would call the inventory service search endpoint
|
|
# For now, return a placeholder response
|
|
return {
|
|
"ingredients": [],
|
|
"total": 0,
|
|
"message": "Integration with inventory service needed"
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error(f"Error searching ingredients: {e}")
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
|
|
@router.get("/{ingredient_id}")
|
|
async def get_ingredient(
|
|
ingredient_id: UUID,
|
|
tenant_id: UUID = Depends(get_tenant_id)
|
|
):
|
|
"""Get ingredient details from inventory service"""
|
|
try:
|
|
inventory_client = InventoryClient()
|
|
ingredient = await inventory_client.get_ingredient_by_id(tenant_id, ingredient_id)
|
|
|
|
if not ingredient:
|
|
raise HTTPException(status_code=404, detail="Ingredient not found")
|
|
|
|
return ingredient
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error getting ingredient {ingredient_id}: {e}")
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
|
|
@router.get("/{ingredient_id}/stock")
|
|
async def get_ingredient_stock(
|
|
ingredient_id: UUID,
|
|
tenant_id: UUID = Depends(get_tenant_id)
|
|
):
|
|
"""Get ingredient stock level from inventory service"""
|
|
try:
|
|
inventory_client = InventoryClient()
|
|
stock = await inventory_client.get_ingredient_stock_level(tenant_id, ingredient_id)
|
|
|
|
if not stock:
|
|
raise HTTPException(status_code=404, detail="Stock information not found")
|
|
|
|
return stock
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error getting stock for ingredient {ingredient_id}: {e}")
|
|
raise HTTPException(status_code=500, detail="Internal server error")
|
|
|
|
|
|
@router.post("/check-availability")
|
|
async def check_ingredients_availability(
|
|
required_ingredients: List[dict],
|
|
tenant_id: UUID = Depends(get_tenant_id)
|
|
):
|
|
"""Check if required ingredients are available for production"""
|
|
try:
|
|
inventory_client = InventoryClient()
|
|
result = await inventory_client.check_ingredient_availability(
|
|
tenant_id,
|
|
required_ingredients
|
|
)
|
|
|
|
if not result["success"]:
|
|
raise HTTPException(status_code=400, detail=result["error"])
|
|
|
|
return result["data"]
|
|
|
|
except HTTPException:
|
|
raise
|
|
except Exception as e:
|
|
logger.error(f"Error checking ingredient availability: {e}")
|
|
raise HTTPException(status_code=500, detail="Internal server error") |