Files
whatsapp/api/get_conversations.php
T
2026-01-12 11:06:43 -05:00

62 lines
1.8 KiB
PHP

<?php
/**
* API - Obtener conversaciones recientes
* Fecha: 4 de enero de 2026
*/
require_once '../config/config.php';
header('Content-Type: application/json; charset=utf-8');
header('Access-Control-Allow-Origin: *');
header('Access-Control-Allow-Methods: GET');
header('Access-Control-Allow-Headers: Content-Type');
try {
$db = Database::getInstance();
// Obtener conversaciones de la tabla conversations
$conversations = $db->fetchAll(
"SELECT
c.id,
c.user_id,
COALESCE(c.content, '') as content,
c.direction,
c.message_type,
c.status,
c.created_at,
u.phone_number,
COALESCE(u.name, u.phone_number) as name
FROM conversations c
JOIN users u ON c.user_id = u.id
ORDER BY c.created_at DESC
LIMIT 500"
);
// Si no hay datos, retornar array vacío
if (empty($conversations)) {
$conversations = [];
}
// Formatear datos para el frontend
$conversations = array_map(function($conv) {
return [
'id' => intval($conv['id']),
'user_id' => intval($conv['user_id']),
'content' => $conv['content'] ?? '',
'direction' => $conv['direction'] ?? 'incoming',
'message_type' => $conv['message_type'] ?? 'text',
'status' => $conv['status'] ?? 'sent',
'created_at' => $conv['created_at'],
'phone_number' => $conv['phone_number'],
'name' => $conv['name'] ?? $conv['phone_number']
];
}, $conversations);
echo json_encode($conversations);
} catch (Exception $e) {
error_log("Error in get_conversations.php: " . $e->getMessage());
http_response_code(500);
echo json_encode(['error' => 'Error interno del servidor: ' . $e->getMessage()]);
}
?>