Fix orchestrator issues

This commit is contained in:
Urtzi Alfaro
2025-11-05 22:54:14 +01:00
parent 80728eaa4e
commit 3ad093d38b
9 changed files with 422 additions and 484 deletions

View File

@@ -34,7 +34,8 @@ route_builder = RouteBuilder('notifications')
# Dependency injection for enhanced notification service
def get_enhanced_notification_service():
database_manager = create_database_manager()
from app.core.config import settings
database_manager = create_database_manager(settings.DATABASE_URL, "notification")
return EnhancedNotificationService(database_manager)
@@ -47,7 +48,6 @@ def get_enhanced_notification_service():
response_model=NotificationResponse,
status_code=201
)
@require_user_role(["member", "admin", "owner"])
@track_endpoint_metrics("notification_send")
async def send_notification(
notification_data: Dict[str, Any],
@@ -55,11 +55,23 @@ async def send_notification(
current_user: Dict[str, Any] = Depends(get_current_user_dep),
notification_service: EnhancedNotificationService = Depends(get_enhanced_notification_service)
):
"""Send a single notification with enhanced validation and features"""
"""Send a single notification with enhanced validation and features - allows service-to-service calls"""
try:
# Allow service-to-service calls (skip role check for service tokens)
is_service_call = current_user.get("type") == "service"
if not is_service_call:
# Check user role for non-service calls
user_role = current_user.get("role", "").lower()
if user_role not in ["member", "admin", "owner"]:
raise HTTPException(
status_code=status.HTTP_403_FORBIDDEN,
detail="Insufficient permissions"
)
# Check permissions for broadcast notifications (Admin+ only)
if notification_data.get("broadcast", False):
if notification_data.get("broadcast", False) and not is_service_call:
user_role = current_user.get("role", "").lower()
if user_role not in ["admin", "owner"]:
raise HTTPException(
@@ -111,10 +123,14 @@ async def send_notification(
detail=f"Invalid priority: {notification_data['priority']}"
)
# Use tenant_id from path parameter (especially for service calls)
effective_tenant_id = str(tenant_id) if is_service_call else current_user.get("tenant_id")
effective_sender_id = current_user.get("user_id", "system")
# Create notification using enhanced service
notification = await notification_service.create_notification(
tenant_id=current_user.get("tenant_id"),
sender_id=current_user["user_id"],
tenant_id=effective_tenant_id,
sender_id=effective_sender_id,
notification_type=notification_type,
message=notification_data["message"],
recipient_id=notification_data.get("recipient_id"),
@@ -131,18 +147,20 @@ async def send_notification(
logger.info("Notification sent successfully",
notification_id=notification.id,
tenant_id=current_user.get("tenant_id"),
tenant_id=effective_tenant_id,
type=notification_type.value,
priority=priority.value)
priority=priority.value,
is_service_call=is_service_call)
return NotificationResponse.from_orm(notification)
except HTTPException:
raise
except Exception as e:
effective_tenant_id = str(tenant_id) if current_user.get("type") == "service" else current_user.get("tenant_id")
logger.error("Failed to send notification",
tenant_id=current_user.get("tenant_id"),
sender_id=current_user["user_id"],
tenant_id=effective_tenant_id,
sender_id=current_user.get("user_id", "system"),
error=str(e))
raise HTTPException(
status_code=status.HTTP_500_INTERNAL_SERVER_ERROR,

View File

@@ -25,7 +25,8 @@ route_builder = RouteBuilder('notifications')
# Dependency injection for enhanced notification service
def get_enhanced_notification_service():
database_manager = create_database_manager()
from app.core.config import settings
database_manager = create_database_manager(settings.DATABASE_URL, "notification")
return EnhancedNotificationService(database_manager)
# ============================================================================

View File

@@ -46,7 +46,6 @@ class EnhancedNotificationService:
'log': self.log_repo
}
@transactional
async def create_notification(
self,
tenant_id: str,
@@ -70,11 +69,11 @@ class EnhancedNotificationService:
try:
async with self.database_manager.get_session() as db_session:
async with UnitOfWork(db_session) as uow:
# Register repositories
notification_repo = uow.register_repository("notifications", NotificationRepository)
template_repo = uow.register_repository("templates", TemplateRepository)
preference_repo = uow.register_repository("preferences", PreferenceRepository)
log_repo = uow.register_repository("logs", LogRepository)
# Register repositories with model classes
notification_repo = uow.register_repository("notifications", NotificationRepository, Notification)
template_repo = uow.register_repository("templates", TemplateRepository, NotificationTemplate)
preference_repo = uow.register_repository("preferences", PreferenceRepository, NotificationPreference)
log_repo = uow.register_repository("logs", LogRepository, NotificationLog)
notification_data = {
"tenant_id": tenant_id,