Files
bakery-ia/services/inventory/app/main.py
2025-11-02 20:24:44 +01:00

151 lines
5.0 KiB
Python

# services/inventory/app/main.py
"""
Inventory Service FastAPI Application
"""
import os
from fastapi import FastAPI
from sqlalchemy import text
# Import core modules
from app.core.config import settings
from app.core.database import database_manager
from app.services.inventory_alert_service import InventoryAlertService
from shared.service_base import StandardFastAPIService
from app.api import (
ingredients,
stock_entries,
transformations,
inventory_operations,
food_safety_compliance,
temperature_logs,
food_safety_alerts,
food_safety_operations,
dashboard,
analytics,
sustainability,
internal_demo,
audit
)
class InventoryService(StandardFastAPIService):
"""Inventory Service with standardized setup"""
expected_migration_version = "00001"
async def on_startup(self, app):
"""Custom startup logic including migration verification"""
await self.verify_migrations()
await super().on_startup(app)
async def verify_migrations(self):
"""Verify database schema matches the latest migrations."""
try:
async with self.database_manager.get_session() as session:
result = await session.execute(text("SELECT version_num FROM alembic_version"))
version = result.scalar()
if version != self.expected_migration_version:
self.logger.error(f"Migration version mismatch: expected {self.expected_migration_version}, got {version}")
raise RuntimeError(f"Migration version mismatch: expected {self.expected_migration_version}, got {version}")
self.logger.info(f"Migration verification successful: {version}")
except Exception as e:
self.logger.error(f"Migration verification failed: {e}")
raise
def __init__(self):
# Define expected database tables for health checks
inventory_expected_tables = [
'ingredients', 'stock', 'stock_movements', 'product_transformations',
'stock_alerts', 'food_safety_compliance', 'temperature_logs', 'food_safety_alerts'
]
super().__init__(
service_name="inventory-service",
app_name=settings.APP_NAME,
description=settings.DESCRIPTION,
version=settings.VERSION,
log_level=settings.LOG_LEVEL,
cors_origins=settings.CORS_ORIGINS,
api_prefix="", # Empty because RouteBuilder already includes /api/v1
database_manager=database_manager,
expected_tables=inventory_expected_tables
)
async def on_startup(self, app: FastAPI):
"""Custom startup logic for inventory service"""
# Initialize alert service
alert_service = InventoryAlertService(settings)
await alert_service.start()
self.logger.info("Inventory alert service started")
# Store alert service in app state
app.state.alert_service = alert_service
async def on_shutdown(self, app: FastAPI):
"""Custom shutdown logic for inventory service"""
# Stop alert service
if hasattr(app.state, 'alert_service'):
await app.state.alert_service.stop()
self.logger.info("Alert service stopped")
def get_service_features(self):
"""Return inventory-specific features"""
return [
"ingredient_management",
"stock_tracking",
"expiration_alerts",
"low_stock_alerts",
"batch_tracking",
"fifo_consumption",
"barcode_support",
"food_safety_compliance",
"temperature_monitoring",
"dashboard_analytics",
"business_model_detection",
"real_time_alerts",
"regulatory_reporting",
"sustainability_tracking",
"sdg_compliance",
"environmental_impact",
"grant_reporting"
]
# Create service instance
service = InventoryService()
# Create FastAPI app with standardized setup
app = service.create_app()
# Setup standard endpoints
service.setup_standard_endpoints()
# Include new standardized routers
# IMPORTANT: Register audit router FIRST to avoid route matching conflicts
service.add_router(audit.router)
service.add_router(ingredients.router)
service.add_router(stock_entries.router)
service.add_router(transformations.router)
service.add_router(inventory_operations.router)
service.add_router(food_safety_compliance.router)
service.add_router(temperature_logs.router)
service.add_router(food_safety_alerts.router)
service.add_router(food_safety_operations.router)
service.add_router(dashboard.router)
service.add_router(analytics.router)
service.add_router(sustainability.router)
service.add_router(internal_demo.router)
if __name__ == "__main__":
import uvicorn
uvicorn.run(
"app.main:app",
host="0.0.0.0",
port=8000,
reload=os.getenv("RELOAD", "false").lower() == "true",
log_level="info"
)