Add POI feature and imporve the overall backend implementation

This commit is contained in:
Urtzi Alfaro
2025-11-12 15:34:10 +01:00
parent e8096cd979
commit 5783c7ed05
173 changed files with 16862 additions and 9078 deletions

View File

@@ -11,7 +11,9 @@ from sqlalchemy import text
from app.core.config import settings
from app.core.database import database_manager
from app.services.inventory_alert_service import InventoryAlertService
from app.consumers.delivery_event_consumer import DeliveryEventConsumer
from shared.service_base import StandardFastAPIService
import asyncio
from app.api import (
ingredients,
@@ -34,12 +36,7 @@ from app.api import (
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)
expected_migration_version = "make_stock_fields_nullable"
async def verify_migrations(self):
"""Verify database schema matches the latest migrations."""
@@ -62,6 +59,11 @@ class InventoryService(StandardFastAPIService):
'stock_alerts', 'food_safety_compliance', 'temperature_logs', 'food_safety_alerts'
]
# Initialize delivery consumer and rabbitmq client
self.delivery_consumer = None
self.delivery_consumer_task = None
self.rabbitmq_client = None
super().__init__(
service_name="inventory-service",
app_name=settings.APP_NAME,
@@ -71,11 +73,38 @@ class InventoryService(StandardFastAPIService):
cors_origins=settings.CORS_ORIGINS,
api_prefix="", # Empty because RouteBuilder already includes /api/v1
database_manager=database_manager,
expected_tables=inventory_expected_tables
expected_tables=inventory_expected_tables,
enable_messaging=True # Enable RabbitMQ for event consumption
)
async def _setup_messaging(self):
"""Setup messaging for inventory service"""
from shared.messaging.rabbitmq import RabbitMQClient
try:
self.rabbitmq_client = RabbitMQClient(settings.RABBITMQ_URL, service_name="inventory-service")
await self.rabbitmq_client.connect()
self.logger.info("Inventory service messaging setup completed")
except Exception as e:
self.logger.error("Failed to setup inventory messaging", error=str(e))
raise
async def _cleanup_messaging(self):
"""Cleanup messaging for inventory service"""
try:
if self.rabbitmq_client:
await self.rabbitmq_client.disconnect()
self.logger.info("Inventory service messaging cleanup completed")
except Exception as e:
self.logger.error("Error during inventory messaging cleanup", error=str(e))
async def on_startup(self, app: FastAPI):
"""Custom startup logic for inventory service"""
# Verify migrations first
await self.verify_migrations()
# Call parent startup (includes database, messaging, etc.)
await super().on_startup(app)
# Initialize alert service
alert_service = InventoryAlertService(settings)
await alert_service.start()
@@ -84,13 +113,37 @@ class InventoryService(StandardFastAPIService):
# Store alert service in app state
app.state.alert_service = alert_service
# Initialize and start delivery event consumer
self.delivery_consumer = DeliveryEventConsumer()
# Start consuming delivery.received events in background
if self.rabbitmq_client and self.rabbitmq_client.connected:
self.delivery_consumer_task = asyncio.create_task(
self.delivery_consumer.consume_delivery_received_events(self.rabbitmq_client)
)
self.logger.info("Delivery event consumer started successfully")
else:
self.logger.warning("RabbitMQ not connected, delivery event consumer not started")
app.state.delivery_consumer = self.delivery_consumer
async def on_shutdown(self, app: FastAPI):
"""Custom shutdown logic for inventory service"""
# Cancel delivery consumer task
if self.delivery_consumer_task and not self.delivery_consumer_task.done():
self.delivery_consumer_task.cancel()
try:
await self.delivery_consumer_task
except asyncio.CancelledError:
self.logger.info("Delivery event consumer task cancelled")
# Stop alert service
if hasattr(app.state, 'alert_service'):
await app.state.alert_service.stop()
self.logger.info("Alert service stopped")
await super().on_shutdown(app)
def get_service_features(self):
"""Return inventory-specific features"""
return [