From 25b1aa39b001f34c0d87419ee56daf718bce2d77 Mon Sep 17 00:00:00 2001 From: "IGNY8 VPS (Salman)" Date: Sun, 16 Nov 2025 09:22:33 +0000 Subject: [PATCH] Step 5: Validate account_id is required and exists in tasks.py - Make account_id parameter required (remove default None) - Validate account_id is provided before proceeding - Validate account exists in database - Return proper error responses with error_type - Update function docstring to reflect required parameter --- backend/igny8_core/ai/tasks.py | 37 +++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 10 deletions(-) diff --git a/backend/igny8_core/ai/tasks.py b/backend/igny8_core/ai/tasks.py index 98faf489..40f1000b 100644 --- a/backend/igny8_core/ai/tasks.py +++ b/backend/igny8_core/ai/tasks.py @@ -10,7 +10,7 @@ logger = logging.getLogger(__name__) @shared_task(bind=True, max_retries=3) -def run_ai_task(self, function_name: str, payload: dict, account_id: int = None): +def run_ai_task(self, function_name: str, payload: dict, account_id: int): """ Single Celery entrypoint for all AI functions. Dynamically loads and executes the requested function. @@ -18,7 +18,10 @@ def run_ai_task(self, function_name: str, payload: dict, account_id: int = None) Args: function_name: Name of the AI function (e.g., 'auto_cluster') payload: Function-specific payload - account_id: Account ID for account isolation + account_id: Account ID for account isolation (required) + + Raises: + Returns error dict if account_id not provided or account not found """ logger.info("=" * 80) logger.info(f"run_ai_task STARTED: {function_name}") @@ -29,14 +32,28 @@ def run_ai_task(self, function_name: str, payload: dict, account_id: int = None) logger.info("=" * 80) try: - # Get account - account = None - if account_id: - from igny8_core.auth.models import Account - try: - account = Account.objects.get(id=account_id) - except Account.DoesNotExist: - logger.warning(f"Account {account_id} not found") + # Validate account_id is provided + if not account_id: + error_msg = "account_id is required for AI task execution" + logger.error(f"[run_ai_task] {error_msg}") + return { + 'success': False, + 'error': error_msg, + 'error_type': 'ConfigurationError' + } + + # Get account and validate it exists + from igny8_core.auth.models import Account + try: + account = Account.objects.get(id=account_id) + except Account.DoesNotExist: + error_msg = f"Account {account_id} not found" + logger.error(f"[run_ai_task] {error_msg}") + return { + 'success': False, + 'error': error_msg, + 'error_type': 'AccountNotFound' + } # Get function from registry fn = get_function_instance(function_name)