';
}
@@ -1939,12 +1940,14 @@ HTML;
$db = db();
// Get unique conversations with last message and unread count
$rows = $db->query("
- SELECT c.phone_number, c.contact_name, c.company_id,
- MAX(c.created_at) as last_time,
+ SELECT c.phone_number,
+ MAX(c.contact_name) as contact_name,
+ MAX(c.company_id) as company_id,
+ MAX(c.created_at) as last_time,
(SELECT content FROM conversations c2 WHERE c2.phone_number = c.phone_number ORDER BY c2.id DESC LIMIT 1) as last_message,
(SELECT COUNT(*) FROM conversations c3 WHERE c3.phone_number = c.phone_number AND c3.direction = 'inbound' AND c3.id > COALESCE((SELECT MAX(c4.id) FROM conversations c4 WHERE c4.phone_number = c.phone_number AND c4.direction = 'outbound'), 0)) as unread_count
FROM conversations c
- GROUP BY c.phone_number, c.contact_name, c.company_id
+ GROUP BY c.phone_number
ORDER BY last_time DESC
")->fetchAll();