Files
sirpremiumv2/database/migrations/2026_04_30_000001_create_chat_tables.php
T
2026-04-30 21:32:32 -05:00

88 lines
4.5 KiB
PHP

<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
// ── Configuración global del chatbot ─────────────────────────────
Schema::create('chat_config', function (Blueprint $table) {
$table->id();
$table->string('config_key')->unique();
$table->text('config_value')->nullable();
$table->string('description')->nullable();
$table->timestamps();
});
// ── Contactos (un registro por teléfono/chat_id + canal) ─────────
Schema::create('chat_contacts', function (Blueprint $table) {
$table->id();
$table->string('canal', 20); // 'web' | 'telegram'
$table->string('canal_id'); // teléfono (web) o chat_id numérico (telegram)
$table->string('nombre')->nullable();
$table->string('telefono')->nullable();
$table->json('metadata')->nullable(); // datos extra del canal
$table->timestamps();
$table->unique(['canal', 'canal_id']);
});
// ── Conversaciones ────────────────────────────────────────────────
Schema::create('chat_conversations', function (Blueprint $table) {
$table->id();
$table->foreignId('contact_id')->constrained('chat_contacts')->cascadeOnDelete();
$table->string('canal', 20); // 'web' | 'telegram'
$table->string('estado', 20)->default('bot'); // 'bot' | 'agente' | 'cerrada'
$table->integer('menu_actual_id')->nullable(); // id del menú donde está el usuario
$table->timestamp('ultimo_mensaje_at')->nullable();
$table->unsignedInteger('mensajes_no_leidos')->default(0);
$table->timestamps();
});
// ── Mensajes ──────────────────────────────────────────────────────
Schema::create('chat_messages', function (Blueprint $table) {
$table->id();
$table->foreignId('conversation_id')->constrained('chat_conversations')->cascadeOnDelete();
$table->string('tipo', 20); // 'usuario' | 'bot' | 'agente'
$table->text('contenido');
$table->boolean('leido')->default(false);
$table->timestamps();
});
// ── Menús del bot ─────────────────────────────────────────────────
Schema::create('chat_menus', function (Blueprint $table) {
$table->id();
$table->string('nombre');
$table->text('mensaje'); // texto que envía el bot al llegar a este menú
$table->boolean('es_raiz')->default(false);
$table->boolean('activo')->default(true);
$table->timestamps();
});
// ── Opciones de menú ──────────────────────────────────────────────
Schema::create('chat_menu_options', function (Blueprint $table) {
$table->id();
$table->foreignId('menu_id')->constrained('chat_menus')->cascadeOnDelete();
$table->string('clave', 20); // lo que el usuario escribe: '1', '2', 'a', etc.
$table->string('etiqueta'); // texto descriptivo de la opción
$table->string('accion', 30); // 'ir_menu' | 'respuesta' | 'agente'
$table->unsignedBigInteger('menu_destino_id')->nullable(); // si accion = ir_menu
$table->text('respuesta_texto')->nullable(); // si accion = respuesta
$table->integer('orden')->default(0);
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('chat_menu_options');
Schema::dropIfExists('chat_menus');
Schema::dropIfExists('chat_messages');
Schema::dropIfExists('chat_conversations');
Schema::dropIfExists('chat_contacts');
Schema::dropIfExists('chat_config');
}
};