56 lines
1.7 KiB
Python
56 lines
1.7 KiB
Python
"""
|
|
Internal API for Inventory Service
|
|
Handles internal service-to-service operations
|
|
"""
|
|
|
|
from fastapi import APIRouter, Depends, HTTPException, Header
|
|
from sqlalchemy.ext.asyncio import AsyncSession
|
|
from sqlalchemy import select, func
|
|
from uuid import UUID
|
|
import structlog
|
|
|
|
from app.core.database import get_db
|
|
from app.core.config import settings
|
|
from app.models import Ingredient
|
|
|
|
logger = structlog.get_logger()
|
|
router = APIRouter(prefix="/internal", tags=["internal"])
|
|
|
|
|
|
async def verify_internal_api_key(x_internal_api_key: str = Header(None)):
|
|
"""Verify internal API key for service-to-service communication"""
|
|
required_key = settings.INTERNAL_API_KEY
|
|
if x_internal_api_key != required_key:
|
|
logger.warning("Unauthorized internal API access attempted")
|
|
raise HTTPException(status_code=403, detail="Invalid internal API key")
|
|
return True
|
|
|
|
|
|
@router.get("/count")
|
|
async def get_ingredient_count(
|
|
tenant_id: str,
|
|
db: AsyncSession = Depends(get_db),
|
|
_: bool = Depends(verify_internal_api_key)
|
|
):
|
|
"""
|
|
Get count of active ingredients for onboarding status check.
|
|
Internal endpoint for tenant service.
|
|
"""
|
|
try:
|
|
count = await db.scalar(
|
|
select(func.count()).select_from(Ingredient)
|
|
.where(
|
|
Ingredient.tenant_id == UUID(tenant_id),
|
|
Ingredient.is_active == True
|
|
)
|
|
)
|
|
|
|
return {
|
|
"count": count or 0,
|
|
"tenant_id": tenant_id
|
|
}
|
|
|
|
except Exception as e:
|
|
logger.error("Failed to get ingredient count", tenant_id=tenant_id, error=str(e))
|
|
raise HTTPException(status_code=500, detail=f"Failed to get ingredient count: {str(e)}")
|