up
This commit is contained in:
@@ -14,9 +14,14 @@ header('Access-Control-Allow-Headers: Content-Type');
|
||||
try {
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Paginación: recibir page y limit desde query string (con límites razonables)
|
||||
$limit = isset($_GET['limit']) ? intval($_GET['limit']) : 50;
|
||||
$limit = max(1, min(200, $limit)); // máximo 200 por página
|
||||
$page = isset($_GET['page']) ? max(1, intval($_GET['page'])) : 1;
|
||||
$offset = ($page - 1) * $limit;
|
||||
|
||||
// Obtener conversaciones agregadas por usuario: último mensaje + conteo de no leídos + avatar
|
||||
$conversations = $db->fetchAll(
|
||||
"SELECT
|
||||
$sql = "SELECT
|
||||
u.id AS user_id,
|
||||
COALESCE(u.name, u.phone_number) AS name,
|
||||
u.phone_number,
|
||||
@@ -43,14 +48,19 @@ try {
|
||||
) lm ON lm.user_id = u.id
|
||||
GROUP BY u.id
|
||||
ORDER BY lm.created_at DESC
|
||||
LIMIT 500"
|
||||
);
|
||||
|
||||
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");
|
||||
$total = isset($totalRow['count']) ? intval($totalRow['count']) : 0;
|
||||
|
||||
// Si no hay datos, retornar array vacío
|
||||
if (empty($conversations)) {
|
||||
$conversations = [];
|
||||
}
|
||||
|
||||
|
||||
// Formatear datos para el frontend
|
||||
$conversations = array_map(function($conv) {
|
||||
return [
|
||||
@@ -71,8 +81,19 @@ try {
|
||||
'in_service_at' => $conv['in_service_at'] ?? null
|
||||
];
|
||||
}, $conversations);
|
||||
|
||||
echo json_encode($conversations);
|
||||
|
||||
// Indicar metadatos para la paginación
|
||||
$hasMore = ($page * $limit) < $total;
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'data' => $conversations,
|
||||
'page' => $page,
|
||||
'limit' => $limit,
|
||||
'has_more' => (bool)$hasMore,
|
||||
'total' => $total
|
||||
]);
|
||||
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_conversations.php: " . $e->getMessage());
|
||||
|
||||
Reference in New Issue
Block a user