diff --git a/api/get_conversations.php b/api/get_conversations.php index fd8ece6..49ec595 100644 --- a/api/get_conversations.php +++ b/api/get_conversations.php @@ -64,24 +64,27 @@ try { $sql .= " FROM users u - LEFT JOIN conversations c ON c.user_id = u.id + LEFT JOIN conversations c ON c.user_id = u.id AND (c.canal IS NULL OR c.canal = 'bot') LEFT JOIN ( SELECT t1.* FROM conversations t1 JOIN ( - SELECT user_id, MAX(created_at) AS last_time FROM conversations GROUP BY user_id + SELECT user_id, MAX(created_at) AS last_time FROM conversations + WHERE (canal IS NULL OR canal = 'bot') GROUP BY user_id ) t2 ON t1.user_id = t2.user_id AND t1.created_at = t2.last_time + WHERE (t1.canal IS NULL OR t1.canal = 'bot') ) lm ON lm.user_id = u.id"; - + // Agregar condición de búsqueda si existe if (!empty($search)) { $sql .= " WHERE ( - u.name LIKE ? - OR u.phone_number LIKE ? + u.name LIKE ? + OR u.phone_number LIKE ? OR EXISTS ( - SELECT 1 FROM conversations c2 - WHERE c2.user_id = u.id + SELECT 1 FROM conversations c2 + WHERE c2.user_id = u.id AND c2.content LIKE ? + AND (c2.canal IS NULL OR c2.canal = 'bot') ) )"; } @@ -147,9 +150,9 @@ try { } else { // Conteo normal sin búsqueda if ($filter === 'unread') { - $totalRow = $db->fetch("SELECT COUNT(DISTINCT user_id) AS count FROM conversations c WHERE c.direction = 'incoming' AND (c.is_read IS NULL OR c.is_read = 0)"); + $totalRow = $db->fetch("SELECT COUNT(DISTINCT user_id) AS count FROM conversations c WHERE c.direction = 'incoming' AND (c.is_read IS NULL OR c.is_read = 0) AND (c.canal IS NULL OR c.canal = 'bot')"); } else { - $totalRow = $db->fetch("SELECT COUNT(DISTINCT user_id) AS count FROM conversations"); + $totalRow = $db->fetch("SELECT COUNT(DISTINCT user_id) AS count FROM conversations WHERE (canal IS NULL OR canal = 'bot')"); } } $total = isset($totalRow['count']) ? intval($totalRow['count']) : 0;