mejoras
This commit is contained in:
+3
-3
@@ -69,10 +69,10 @@ try {
|
||||
|
||||
try {
|
||||
// Eliminar mensajes relacionados usando PDO
|
||||
$stmt = $conn->prepare("DELETE FROM messages WHERE user_id = ?");
|
||||
$stmt = $conn->prepare("DELETE FROM conversations WHERE user_id = ?");
|
||||
$stmt->bindValue(1, $user_id, PDO::PARAM_INT);
|
||||
$stmt->execute();
|
||||
$deleted_messages = $stmt->rowCount();
|
||||
$deleted_conversations = $stmt->rowCount();
|
||||
|
||||
// Eliminar el usuario
|
||||
$stmt = $conn->prepare("DELETE FROM users WHERE id = ?");
|
||||
@@ -94,7 +94,7 @@ try {
|
||||
'deleted_user_id' => $user_id,
|
||||
'deleted_user_name' => $user_data['name'],
|
||||
'deleted_user_phone' => $user_data['phone_number'],
|
||||
'deleted_messages_count' => $deleted_messages,
|
||||
'deleted_conversations_count' => $deleted_conversations,
|
||||
'timestamp' => date('Y-m-d H:i:s')
|
||||
]
|
||||
]);
|
||||
|
||||
@@ -27,7 +27,7 @@ try {
|
||||
u.status,
|
||||
u.created_at,
|
||||
u.updated_at,
|
||||
COUNT(c.id) as total_messages,
|
||||
COUNT(c.id) as total_conversations,
|
||||
MAX(c.created_at) as last_activity
|
||||
FROM users u
|
||||
LEFT JOIN conversations c ON u.id = c.user_id
|
||||
@@ -61,7 +61,7 @@ try {
|
||||
$user['name'] ?? 'Sin nombre',
|
||||
$user['email'] ?? 'Sin email',
|
||||
$user['status'],
|
||||
$user['total_messages'],
|
||||
$user['total_conversations'],
|
||||
$user['last_activity'] ? date('d/m/Y H:i', strtotime($user['last_activity'])) : 'Nunca',
|
||||
date('d/m/Y H:i', strtotime($user['created_at']))
|
||||
], ';');
|
||||
|
||||
@@ -41,7 +41,7 @@ try {
|
||||
}
|
||||
|
||||
// Obtener todos los mensajes de la conversación
|
||||
$messages = $db->fetchAll(
|
||||
$conversations = $db->fetchAll(
|
||||
"SELECT
|
||||
id,
|
||||
message_id,
|
||||
@@ -62,7 +62,7 @@ try {
|
||||
);
|
||||
|
||||
// Formatear mensajes
|
||||
$messages = array_map(function($msg) {
|
||||
$conversations = array_map(function($msg) {
|
||||
return [
|
||||
'id' => intval($msg['id']),
|
||||
'message_id' => $msg['message_id'] ?? '',
|
||||
@@ -75,7 +75,7 @@ try {
|
||||
'time' => date('H:i', strtotime($msg['created_at'])),
|
||||
'date' => date('d/m/Y', strtotime($msg['created_at']))
|
||||
];
|
||||
}, $messages);
|
||||
}, $conversations);
|
||||
|
||||
// Marcar mensajes como leídos
|
||||
$db->query(
|
||||
@@ -90,8 +90,8 @@ try {
|
||||
'phone_number' => $user['phone_number'],
|
||||
'name' => $user['name'] ?? $user['phone_number']
|
||||
],
|
||||
'messages' => $messages,
|
||||
'total_messages' => count($messages)
|
||||
'conversations' => $conversations,
|
||||
'total_conversations' => count($conversations)
|
||||
]);
|
||||
|
||||
} catch (Exception $e) {
|
||||
|
||||
@@ -31,7 +31,7 @@ try {
|
||||
c.message_type as last_message_type,
|
||||
c.created_at as last_message_time,
|
||||
c.status as last_message_status,
|
||||
COUNT(*) as total_messages,
|
||||
COUNT(*) as total_conversations,
|
||||
SUM(CASE WHEN c.direction = 'incoming' AND c.status = 'received' THEN 1 ELSE 0 END) as unread_count
|
||||
FROM users u
|
||||
LEFT JOIN conversations c ON u.id = c.user_id
|
||||
@@ -61,7 +61,7 @@ try {
|
||||
'last_message_type' => $conv['last_message_type'] ?? 'text',
|
||||
'last_message_time' => $conv['last_message_time'],
|
||||
'last_message_status' => $conv['last_message_status'] ?? 'sent',
|
||||
'total_messages' => intval($conv['total_messages']),
|
||||
'total_conversations' => intval($conv['total_conversations']),
|
||||
'unread_count' => intval($conv['unread_count']),
|
||||
'time_ago' => timeAgo($conv['last_message_time'])
|
||||
];
|
||||
|
||||
@@ -33,10 +33,10 @@ try {
|
||||
// En el futuro, esto consultaría la base de datos real
|
||||
$stats = [
|
||||
'user_id' => $user_id,
|
||||
'total_messages' => rand(10, 100),
|
||||
'messages_today' => rand(0, 15),
|
||||
'messages_this_week' => rand(5, 50),
|
||||
'messages_this_month' => rand(20, 80),
|
||||
'total_conversations' => rand(10, 100),
|
||||
'conversations_today' => rand(0, 15),
|
||||
'conversations_this_week' => rand(5, 50),
|
||||
'conversations_this_month' => rand(20, 80),
|
||||
'first_message_date' => date('Y-m-d H:i:s', strtotime('-' . rand(1, 30) . ' days')),
|
||||
'last_message_date' => date('Y-m-d H:i:s', strtotime('-' . rand(1, 24) . ' hours')),
|
||||
'avg_response_time' => rand(5, 120) . ' minutos',
|
||||
|
||||
@@ -14,7 +14,7 @@ header('Access-Control-Allow-Headers: Content-Type');
|
||||
try {
|
||||
$db = Database::getInstance();
|
||||
|
||||
$recentMessages = $db->fetchAll(
|
||||
$recentconversations = $db->fetchAll(
|
||||
"SELECT
|
||||
c.content,
|
||||
c.direction,
|
||||
@@ -29,10 +29,10 @@ try {
|
||||
LIMIT 10"
|
||||
);
|
||||
|
||||
echo json_encode($recentMessages);
|
||||
echo json_encode($recentconversations);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_recent_messages.php: " . $e->getMessage());
|
||||
error_log("Error in get_recent_conversations.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor']);
|
||||
}
|
||||
|
||||
@@ -21,15 +21,15 @@ try {
|
||||
"SELECT config_key, config_value FROM system_config"
|
||||
);
|
||||
|
||||
$settings = [];
|
||||
$system_config = [];
|
||||
foreach ($configs as $config) {
|
||||
$settings[$config['config_key']] = $config['config_value'];
|
||||
$system_config[$config['config_key']] = $config['config_value'];
|
||||
}
|
||||
|
||||
echo json_encode($settings);
|
||||
echo json_encode($system_config);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_settings.php: " . $e->getMessage());
|
||||
error_log("Error in get_system_config.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor']);
|
||||
}
|
||||
|
||||
+4
-4
@@ -28,7 +28,7 @@ try {
|
||||
$totalUsers = $db->fetch("SELECT COUNT(*) as count FROM users")['count'];
|
||||
|
||||
// Mensajes hoy
|
||||
$messagesToday = $db->fetch(
|
||||
$conversationsToday = $db->fetch(
|
||||
"SELECT COUNT(*) as count FROM conversations WHERE DATE(created_at) = CURDATE()"
|
||||
)['count'];
|
||||
|
||||
@@ -39,13 +39,13 @@ try {
|
||||
)['count'];
|
||||
|
||||
// Total de mensajes
|
||||
$totalMessages = $db->fetch("SELECT COUNT(*) as count FROM conversations")['count'];
|
||||
$totalconversations = $db->fetch("SELECT COUNT(*) as count FROM conversations")['count'];
|
||||
|
||||
$stats = [
|
||||
'total_users' => (int)$totalUsers,
|
||||
'messages_today' => (int)$messagesToday,
|
||||
'conversations_today' => (int)$conversationsToday,
|
||||
'active_users' => (int)$activeUsers,
|
||||
'total_messages' => (int)$totalMessages
|
||||
'total_conversations' => (int)$totalconversations
|
||||
];
|
||||
|
||||
echo json_encode($stats);
|
||||
|
||||
+10
-10
@@ -26,7 +26,7 @@ try {
|
||||
$db = Database::getInstance();
|
||||
|
||||
// Obtener mensajes del usuario (de ambas tablas para compatibilidad)
|
||||
$messages = $db->fetchAll(
|
||||
$conversations = $db->fetchAll(
|
||||
"SELECT
|
||||
id,
|
||||
user_id,
|
||||
@@ -55,15 +55,15 @@ try {
|
||||
status,
|
||||
created_at,
|
||||
1 as is_read
|
||||
FROM messages
|
||||
FROM conversations
|
||||
WHERE user_id = :user_id
|
||||
ORDER BY created_at ASC",
|
||||
['user_id' => $userId]
|
||||
);
|
||||
|
||||
// Si no hay mensajes en conversations, intentar con messages
|
||||
if (empty($messages)) {
|
||||
$messages = $db->fetchAll(
|
||||
// Si no hay mensajes en conversations, intentar con conversations
|
||||
if (empty($conversations)) {
|
||||
$conversations = $db->fetchAll(
|
||||
"SELECT
|
||||
id,
|
||||
user_id,
|
||||
@@ -72,7 +72,7 @@ try {
|
||||
message_type,
|
||||
status,
|
||||
created_at
|
||||
FROM messages
|
||||
FROM conversations
|
||||
WHERE user_id = :user_id
|
||||
ORDER BY created_at ASC",
|
||||
['user_id' => $userId]
|
||||
@@ -80,7 +80,7 @@ try {
|
||||
}
|
||||
|
||||
// Formatear fechas y limpiar datos
|
||||
$messages = array_map(function($msg) {
|
||||
$conversations = array_map(function($msg) {
|
||||
return [
|
||||
'id' => intval($msg['id']),
|
||||
'user_id' => intval($msg['user_id']),
|
||||
@@ -93,12 +93,12 @@ try {
|
||||
'status' => $msg['status'] ?? 'sent',
|
||||
'created_at' => $msg['created_at']
|
||||
];
|
||||
}, $messages);
|
||||
}, $conversations);
|
||||
|
||||
echo json_encode($messages);
|
||||
echo json_encode($conversations);
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in get_user_messages.php: " . $e->getMessage());
|
||||
error_log("Error in get_user_conversations.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor']);
|
||||
}
|
||||
|
||||
@@ -40,7 +40,7 @@ try {
|
||||
$offset = ($page - 1) * $limit;
|
||||
$sinceId = isset($_GET['since_id']) ? intval($_GET['since_id']) : null;
|
||||
$search = trim($_GET['search'] ?? '');
|
||||
$filter = trim($_GET['filter'] ?? ''); // e.g., HAS_MESSAGES
|
||||
$filter = trim($_GET['filter'] ?? ''); // e.g., HAS_conversations
|
||||
|
||||
$where = [];
|
||||
$params = [];
|
||||
@@ -55,9 +55,9 @@ try {
|
||||
$params[] = "%{$search}%";
|
||||
}
|
||||
|
||||
if ($filter === 'HAS_MESSAGES') {
|
||||
if ($filter === 'HAS_conversations') {
|
||||
$where[] = 'request_body LIKE ?';
|
||||
$params[] = '%"messages"%';
|
||||
$params[] = '%"conversations"%';
|
||||
}
|
||||
|
||||
$whereClause = empty($where) ? '' : 'WHERE ' . implode(' AND ', $where);
|
||||
|
||||
@@ -85,7 +85,7 @@ try {
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("Error in save_settings.php: " . $e->getMessage());
|
||||
error_log("Error in save_system_config.php: " . $e->getMessage());
|
||||
http_response_code(500);
|
||||
echo json_encode(['error' => 'Error interno del servidor: ' . $e->getMessage()]);
|
||||
}
|
||||
|
||||
@@ -133,7 +133,7 @@ try {
|
||||
}
|
||||
|
||||
// Guardar respuesta en base de datos
|
||||
if ($response && isset($response['messages'][0]['id'])) {
|
||||
if ($response && isset($response['conversations'][0]['id'])) {
|
||||
error_log("send_media_message.php - Respuesta de WhatsApp exitosa: " . json_encode($response));
|
||||
|
||||
// Guardar en base de datos
|
||||
@@ -149,7 +149,7 @@ try {
|
||||
// Datos base para insertar
|
||||
$conversationData = [
|
||||
'user_id' => $user['id'],
|
||||
'message_id' => $response['messages'][0]['id'],
|
||||
'message_id' => $response['conversations'][0]['id'],
|
||||
'direction' => 'outgoing',
|
||||
'message_type' => $mediaType,
|
||||
'content' => $caption ?? $filename ?? '',
|
||||
|
||||
@@ -470,7 +470,7 @@ try {
|
||||
|
||||
$response = $whatsappService->sendTextMessage($user['phone_number'], $message);
|
||||
|
||||
if ($response && isset($response['messages']) && !empty($response['messages'])) {
|
||||
if ($response && isset($response['conversations']) && !empty($response['conversations'])) {
|
||||
// Saving handled by WhatsAppService->saveOutgoingMessage(), skip manual insert to avoid duplicates
|
||||
echo json_encode([
|
||||
'success' => true,
|
||||
|
||||
+10
-10
@@ -80,8 +80,8 @@ class WhatsAppWebhook {
|
||||
foreach ($data['entry'] as $entry) {
|
||||
if (isset($entry['changes'])) {
|
||||
foreach ($entry['changes'] as $change) {
|
||||
if ($change['field'] === 'messages') {
|
||||
$this->processMessages($change['value']);
|
||||
if ($change['field'] === 'conversations') {
|
||||
$this->processconversations($change['value']);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -96,12 +96,12 @@ class WhatsAppWebhook {
|
||||
}
|
||||
}
|
||||
|
||||
private function processMessages($value) {
|
||||
if (!isset($value['messages'])) {
|
||||
private function processconversations($value) {
|
||||
if (!isset($value['conversations'])) {
|
||||
return;
|
||||
}
|
||||
|
||||
foreach ($value['messages'] as $message) {
|
||||
foreach ($value['conversations'] as $message) {
|
||||
$phoneNumber = $message['from'];
|
||||
$messageId = $message['id'];
|
||||
$timestamp = $message['timestamp'];
|
||||
@@ -209,11 +209,11 @@ class WhatsAppWebhook {
|
||||
|
||||
// Procesar estados de mensajes (entregado, leído, etc.)
|
||||
if (isset($value['statuses'])) {
|
||||
$this->processMessageStatuses($value['statuses']);
|
||||
$this->processconversationstatuses($value['statuses']);
|
||||
}
|
||||
}
|
||||
|
||||
private function processMessageStatuses($statuses) {
|
||||
private function processconversationstatuses($statuses) {
|
||||
foreach ($statuses as $status) {
|
||||
$messageId = $status['id'];
|
||||
$newStatus = $status['status']; // sent, delivered, read, failed
|
||||
@@ -294,11 +294,11 @@ class WhatsAppWebhook {
|
||||
if (isset($entry['changes'])) {
|
||||
foreach ($entry['changes'] as $change) {
|
||||
error_log("processPayload: processing change field=" . ($change['field'] ?? ''));
|
||||
if (($change['field'] ?? '') === 'messages' && isset($change['value'])) {
|
||||
if (($change['field'] ?? '') === 'conversations' && isset($change['value'])) {
|
||||
try {
|
||||
$this->processMessages($change['value']);
|
||||
$this->processconversations($change['value']);
|
||||
} catch (Exception $e) {
|
||||
error_log("processPayload: processMessages failed: " . $e->getMessage());
|
||||
error_log("processPayload: processconversations failed: " . $e->getMessage());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user