fix(conversations): exclude canal='turnero' messages from main conversations list

All queries in get_conversations.php now filter (canal IS NULL OR canal='bot')
so the main chat only shows bot conversations and the turnero chat stays isolated.

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Lizandro Guarnizo
2026-07-03 08:35:49 -05:00
co-authored by Claude Sonnet 4.6
parent 4d04fea1de
commit 7eea2ff7ba
+12 -9
View File
@@ -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;