funcional
This commit is contained in:
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
/**
|
||||
* API - Obtener mensajes de una conversación específica
|
||||
*/
|
||||
|
||||
require_once '../config/config.php';
|
||||
|
||||
// Modo debug: desactivar autenticación si existe el parámetro debug
|
||||
$debugMode = isset($_GET['debug']) && $_GET['debug'] === 'true';
|
||||
|
||||
if (!$debugMode) {
|
||||
requireAuthentication();
|
||||
}
|
||||
|
||||
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 {
|
||||
$user_id = $_GET['user_id'] ?? null;
|
||||
|
||||
if (!$user_id) {
|
||||
http_response_code(400);
|
||||
echo json_encode(['error' => 'user_id es requerido']);
|
||||
exit;
|
||||
}
|
||||
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Obtener información del usuario
|
||||
$user = $db->fetch(
|
||||
"SELECT id, phone_number, name FROM users WHERE id = ?",
|
||||
[$user_id]
|
||||
);
|
||||
|
||||
if (!$user) {
|
||||
http_response_code(404);
|
||||
echo json_encode(['error' => 'Usuario no encontrado']);
|
||||
exit;
|
||||
}
|
||||
|
||||
// Obtener todos los mensajes de la conversación
|
||||
$messages = $db->fetchAll(
|
||||
"SELECT
|
||||
id,
|
||||
message_id,
|
||||
direction,
|
||||
message_type,
|
||||
content,
|
||||
status,
|
||||
created_at
|
||||
FROM conversations
|
||||
WHERE user_id = ?
|
||||
ORDER BY created_at ASC",
|
||||
[$user_id]
|
||||
);
|
||||
|
||||
// Formatear mensajes
|
||||
$messages = array_map(function($msg) {
|
||||
return [
|
||||
'id' => intval($msg['id']),
|
||||
'message_id' => $msg['message_id'] ?? '',
|
||||
'direction' => $msg['direction'],
|
||||
'message_type' => $msg['message_type'],
|
||||
'content' => $msg['content'] ?? '',
|
||||
'status' => $msg['status'] ?? 'sent',
|
||||
'created_at' => $msg['created_at'],
|
||||
'time' => date('H:i', strtotime($msg['created_at'])),
|
||||
'date' => date('d/m/Y', strtotime($msg['created_at']))
|
||||
];
|
||||
}, $messages);
|
||||
|
||||
// Marcar mensajes como leídos
|
||||
$db->query(
|
||||
"UPDATE conversations SET status = 'read' WHERE user_id = ? AND direction = 'incoming' AND status != 'read'",
|
||||
[$user_id]
|
||||
);
|
||||
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
'user' => [
|
||||
'id' => intval($user['id']),
|
||||
'phone_number' => $user['phone_number'],
|
||||
'name' => $user['name'] ?? $user['phone_number']
|
||||
],
|
||||
'messages' => $messages,
|
||||
'total_messages' => count($messages)
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_conversation_detail.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor: ' . $e->getMessage()]);
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user