From 36761dce917e632c57f2153b3e47c6a28ceba496 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Sun, 5 Jul 2026 23:16:39 +0300 Subject: [PATCH] Update: 2026-07-05 23:16:38 --- backend/marketing_engine/social_worker.php | 38 +++++++++++++++++----- 1 file changed, 30 insertions(+), 8 deletions(-) diff --git a/backend/marketing_engine/social_worker.php b/backend/marketing_engine/social_worker.php index 48176a78..3f0cd858 100644 --- a/backend/marketing_engine/social_worker.php +++ b/backend/marketing_engine/social_worker.php @@ -26,22 +26,38 @@ try { case 'get_task': // The bot asks for a task to do $platform = $_GET['platform'] ?? 'facebook'; + $requestedAccountId = $_GET['account_id'] ?? null; // Find a pending task that is scheduled for now or earlier - $stmt = $con->prepare(" - SELECT id, account_id, type, target_url, prompt_context, generated_comment - FROM social_tasks - WHERE status = 'pending' AND platform = ? AND (scheduled_at IS NULL OR scheduled_at <= NOW()) - ORDER BY created_at ASC LIMIT 1 - "); - $stmt->execute([$platform]); + if ($requestedAccountId) { + // If the phone is strictly tied to one account, fetch a task specifically for it + // Or fetch an unassigned task and assign it to this phone's account + $stmt = $con->prepare(" + SELECT id, account_id, type, target_url, prompt_context, generated_comment + FROM social_tasks + WHERE status = 'pending' + AND platform = ? + AND (account_id = ? OR account_id IS NULL) + AND (scheduled_at IS NULL OR scheduled_at <= NOW()) + ORDER BY created_at ASC LIMIT 1 + "); + $stmt->execute([$platform, $requestedAccountId]); + } else { + $stmt = $con->prepare(" + SELECT id, account_id, type, target_url, prompt_context, generated_comment + FROM social_tasks + WHERE status = 'pending' AND platform = ? AND (scheduled_at IS NULL OR scheduled_at <= NOW()) + ORDER BY created_at ASC LIMIT 1 + "); + $stmt->execute([$platform]); + } $task = $stmt->fetch(PDO::FETCH_ASSOC); if ($task) { require_once __DIR__ . '/account_manager.php'; $am = new AccountManager(); - $accountId = $task['account_id']; + $accountId = $task['account_id'] ?: $requestedAccountId; $account = null; if ($accountId) { @@ -52,6 +68,12 @@ try { "); $stmtAcc->execute([$accountId]); $account = $stmtAcc->fetch(PDO::FETCH_ASSOC); + + if ($account && !$task['account_id']) { + $updateTask = $con->prepare("UPDATE social_tasks SET account_id = ? WHERE id = ?"); + $updateTask->execute([$accountId, $task['id']]); + $task['account_id'] = $accountId; + } } else { // Dynamically get an available account and assign it $account = $am->getAvailableAccount($platform, 15); // 15 min cooldown