funcional
This commit is contained in:
@@ -0,0 +1,92 @@
|
||||
<?php
|
||||
/**
|
||||
* Test directo para mensajes de texto
|
||||
*/
|
||||
|
||||
require_once 'config/config.php';
|
||||
|
||||
// Simular datos de entrada para mensaje de texto
|
||||
$testData = [
|
||||
'recipient' => '+1234567890',
|
||||
'type' => 'text',
|
||||
'message' => 'Mensaje de prueba desde test directo'
|
||||
];
|
||||
|
||||
echo "=== TEST MENSAJE DE TEXTO ===\n";
|
||||
echo "Datos de entrada: " . json_encode($testData) . "\n\n";
|
||||
|
||||
try {
|
||||
// Test 1: Verificar conexión a la base de datos
|
||||
$db = Database::getInstance();
|
||||
echo "✅ Conexión a base de datos: OK\n";
|
||||
|
||||
// Test 2: Verificar si existe la tabla users
|
||||
$stmt = $db->query("SHOW TABLES LIKE 'users'");
|
||||
$usersTable = $stmt->fetch();
|
||||
echo "✅ Tabla users existe: " . ($usersTable ? 'OK' : 'NO') . "\n";
|
||||
|
||||
// Test 3: Verificar si existe la tabla conversations
|
||||
$stmt = $db->query("SHOW TABLES LIKE 'conversations'");
|
||||
$conversationsTable = $stmt->fetch();
|
||||
echo "✅ Tabla conversations existe: " . ($conversationsTable ? 'OK' : 'NO') . "\n";
|
||||
|
||||
// Test 4: Simular búsqueda de usuario
|
||||
$recipient = $testData['recipient'];
|
||||
$stmt = $db->query("SELECT id FROM users WHERE phone_number = ? LIMIT 1", [$recipient]);
|
||||
$user = $stmt->fetch();
|
||||
echo "📱 Usuario existente con teléfono $recipient: " . ($user ? "ID=" . $user['id'] : 'NO EXISTE') . "\n";
|
||||
|
||||
// Test 5: Simular creación de usuario si no existe
|
||||
if (!$user) {
|
||||
echo "🔄 Intentando crear nuevo usuario...\n";
|
||||
$stmt = $db->query(
|
||||
"INSERT INTO users (phone_number, name, created_at) VALUES (?, ?, NOW())",
|
||||
[$recipient, $recipient]
|
||||
);
|
||||
$userId = $db->lastInsertId();
|
||||
echo "✅ Usuario creado con ID: $userId\n";
|
||||
} else {
|
||||
$userId = $user['id'];
|
||||
echo "✅ Usuario existente con ID: $userId\n";
|
||||
}
|
||||
|
||||
// Test 6: Verificar ventana de 24 horas
|
||||
if ($userId) {
|
||||
$stmt = $db->query(
|
||||
"SELECT MAX(created_at) as last_message FROM conversations
|
||||
WHERE user_id = ? AND direction = 'incoming'
|
||||
AND created_at >= DATE_SUB(NOW(), INTERVAL 24 HOUR)",
|
||||
[$userId]
|
||||
);
|
||||
$lastMessage = $stmt->fetch();
|
||||
echo "⏰ Último mensaje entrante en 24h: " . ($lastMessage['last_message'] ?? 'NINGUNO') . "\n";
|
||||
|
||||
if (!$lastMessage || !$lastMessage['last_message']) {
|
||||
echo "⚠️ Usuario fuera de ventana de 24h - Solo plantillas permitidas\n";
|
||||
} else {
|
||||
echo "✅ Usuario dentro de ventana de 24h - Mensajes de texto permitidos\n";
|
||||
}
|
||||
}
|
||||
|
||||
// Test 7: Simular guardado de mensaje
|
||||
if ($userId) {
|
||||
echo "💾 Intentando guardar mensaje en conversations...\n";
|
||||
$messageContent = $testData['message'];
|
||||
$messageType = $testData['type'];
|
||||
|
||||
$stmt = $db->query(
|
||||
"INSERT INTO conversations (user_id, content, direction, message_type, status, message_id, created_at) VALUES (?, ?, 'outgoing', ?, 'sent', ?, NOW())",
|
||||
[$userId, $messageContent, $messageType, 'test_message_' . time()]
|
||||
);
|
||||
echo "✅ Mensaje guardado correctamente en conversations\n";
|
||||
}
|
||||
|
||||
echo "\n=== RESULTADO ===\n";
|
||||
echo "✅ Todas las operaciones de base de datos funcionan correctamente\n";
|
||||
echo "✅ El problema debería estar resuelto\n";
|
||||
|
||||
} catch (Exception $e) {
|
||||
echo "❌ ERROR: " . $e->getMessage() . "\n";
|
||||
echo "Stack trace: " . $e->getTraceAsString() . "\n";
|
||||
}
|
||||
?>
|
||||
Reference in New Issue
Block a user