This commit is contained in:
Lizandro Guarnizo
2026-01-23 22:12:27 -05:00
parent d9942456c8
commit 2017c8c805
8 changed files with 420 additions and 110 deletions
+15 -4
View File
@@ -21,6 +21,8 @@ try {
$offset = ($page - 1) * $limit;
// Obtener conversaciones agregadas por usuario: último mensaje + conteo de no leídos + avatar
$filter = isset($_GET['filter']) ? strtolower(trim($_GET['filter'])) : 'all';
$sql = "SELECT
u.id AS user_id,
COALESCE(u.name, u.phone_number) AS name,
@@ -46,14 +48,23 @@ try {
SELECT user_id, MAX(created_at) AS last_time FROM conversations GROUP BY user_id
) t2 ON t1.user_id = t2.user_id AND t1.created_at = t2.last_time
) lm ON lm.user_id = u.id
GROUP BY u.id
ORDER BY lm.created_at DESC
LIMIT %d OFFSET %d";
GROUP BY u.id";
// Aplicar filtro 'unread' si se solicita
if ($filter === 'unread') {
$sql .= " HAVING IFNULL(SUM(CASE WHEN c.direction = 'incoming' AND (c.is_read IS NULL OR c.is_read = 0) THEN 1 ELSE 0 END), 0) > 0";
}
$sql .= "\n ORDER BY lm.created_at DESC\n LIMIT %d OFFSET %d";
$conversations = $db->fetchAll(sprintf($sql, $limit, $offset));
// Conteo total de usuarios con al menos una conversación (útil para paginar)
$totalRow = $db->fetch("SELECT COUNT(DISTINCT user_id) AS count FROM conversations");
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)");
} else {
$totalRow = $db->fetch("SELECT COUNT(DISTINCT user_id) AS count FROM conversations");
}
$total = isset($totalRow['count']) ? intval($totalRow['count']) : 0;
// Si no hay datos, retornar array vacío