This commit is contained in:
Lizandro Guarnizo
2026-04-24 21:40:10 -05:00
parent 877a0d393a
commit 148fa6b9f9
36 changed files with 3037 additions and 0 deletions
@@ -0,0 +1,37 @@
<?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
{
Schema::create('whatsapp_users', function (Blueprint $table) {
$table->id();
$table->string('phone_number', 20)->unique();
$table->string('name', 100)->nullable();
$table->enum('status', ['active', 'blocked', 'inactive'])->default('active');
$table->unsignedBigInteger('current_menu_id')->nullable();
$table->integer('current_step')->default(0);
$table->text('session_data')->nullable();
$table->datetime('welcome_sent_at')->nullable();
$table->tinyInteger('in_service')->default(0);
$table->unsignedBigInteger('in_service_by')->nullable();
$table->tinyInteger('on_hold')->default(0);
$table->datetime('bot_paused_until')->nullable();
$table->tinyInteger('advisor_requested')->default(0);
$table->tinyInteger('bot_enabled')->default(1);
$table->tinyInteger('terms_pending')->default(0);
$table->datetime('terms_accepted_at')->nullable();
$table->unsignedBigInteger('terms_version_id')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('whatsapp_users');
}
};
@@ -0,0 +1,41 @@
<?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
{
Schema::create('whatsapp_conversations', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id');
$table->string('message_id', 255)->nullable()->unique();
$table->string('reply_to_message_id', 255)->nullable();
$table->string('reaction_to_message_id', 255)->nullable();
$table->string('reaction_emoji', 64)->nullable();
$table->enum('direction', ['incoming', 'outgoing']);
$table->string('message_type', 32)->default('text');
$table->text('content')->nullable();
$table->text('media_url')->nullable();
$table->string('whatsapp_media_id', 255)->nullable();
$table->string('local_file', 255)->nullable();
$table->string('local_thumb', 255)->nullable();
$table->string('media_storage', 50)->nullable();
$table->string('status', 32)->default('received');
$table->tinyInteger('is_read')->default(0);
$table->string('filename', 255)->nullable();
$table->string('mime_type', 100)->nullable();
$table->timestamp('created_at')->useCurrent();
$table->foreign('user_id')->references('id')->on('whatsapp_users')->onDelete('cascade');
$table->index(['user_id', 'created_at']);
});
}
public function down(): void
{
Schema::dropIfExists('whatsapp_conversations');
}
};
@@ -0,0 +1,42 @@
<?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
{
Schema::create('whatsapp_menus', function (Blueprint $table) {
$table->id();
$table->string('name', 255);
$table->string('title', 255);
$table->text('message');
$table->unsignedBigInteger('parent_id')->nullable();
$table->tinyInteger('is_main')->default(0);
$table->tinyInteger('is_active')->default(1);
$table->integer('sort_order')->default(0);
$table->timestamps();
});
Schema::create('whatsapp_menu_options', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('menu_id');
$table->integer('option_number');
$table->string('title', 255);
$table->enum('action_type', ['submenu', 'message', 'template', 'url', 'advisor', 'flow']);
$table->text('action_value')->nullable();
$table->tinyInteger('is_active')->default(1);
$table->integer('sort_order')->default(0);
$table->foreign('menu_id')->references('id')->on('whatsapp_menus')->onDelete('cascade');
});
}
public function down(): void
{
Schema::dropIfExists('whatsapp_menu_options');
Schema::dropIfExists('whatsapp_menus');
}
};
@@ -0,0 +1,31 @@
<?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
{
Schema::create('whatsapp_message_templates', function (Blueprint $table) {
$table->id();
$table->string('name', 255);
$table->string('template_name', 255);
$table->string('language_code', 10)->default('es');
$table->enum('status', ['pending', 'approved', 'rejected', 'paused'])->default('pending');
$table->text('body_text')->nullable();
$table->enum('header_type', ['none', 'text', 'image', 'video', 'document'])->default('none');
$table->string('header_text', 255)->nullable();
$table->string('footer_text', 255)->nullable();
$table->longText('components')->nullable();
$table->longText('example_parameters')->nullable();
$table->timestamps();
});
}
public function down(): void
{
Schema::dropIfExists('whatsapp_message_templates');
}
};
@@ -0,0 +1,39 @@
<?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
{
Schema::create('whatsapp_system_config', function (Blueprint $table) {
$table->id();
$table->string('config_key', 255)->unique();
$table->text('config_value')->nullable();
$table->text('description')->nullable();
$table->timestamps();
});
// Seed default config keys
\DB::table('whatsapp_system_config')->insert([
['config_key' => 'whatsapp_token', 'config_value' => '', 'description' => 'Token de acceso Bearer de Meta', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'phone_number_id', 'config_value' => '', 'description' => 'ID del número de teléfono en Meta', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'whatsapp_api_url', 'config_value' => 'https://graph.facebook.com/v22.0/', 'description' => 'URL base de la Graph API', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'webhook_verify_token', 'config_value' => '', 'description' => 'Token secreto para verificar el webhook', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'bot_enabled', 'config_value' => '1', 'description' => 'Activar/desactivar el bot (1/0)', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'welcome_message', 'config_value' => '¡Hola! Bienvenido. ¿En qué te podemos ayudar?', 'description' => 'Mensaje de bienvenida', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'default_no_match', 'config_value' => 'No entendí tu mensaje. Escribe MENU para ver las opciones.', 'description' => 'Mensaje cuando no se entiende la entrada', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'advisor_message', 'config_value' => 'Un asesor se comunicará contigo pronto.', 'description' => 'Mensaje al solicitar asesor', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'business_hours_enabled', 'config_value' => '0', 'description' => 'Validar horarios de atención (1/0)', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'business_hours_start', 'config_value' => '08:00', 'description' => 'Hora inicio de atención', 'created_at' => now(), 'updated_at' => now()],
['config_key' => 'business_hours_end', 'config_value' => '18:00', 'description' => 'Hora fin de atención', 'created_at' => now(), 'updated_at' => now()],
]);
}
public function down(): void
{
Schema::dropIfExists('whatsapp_system_config');
}
};
@@ -0,0 +1,44 @@
<?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
{
Schema::create('whatsapp_scheduled_messages', function (Blueprint $table) {
$table->id();
$table->unsignedBigInteger('user_id')->nullable();
$table->string('phone_number', 20);
$table->unsignedBigInteger('template_id')->nullable();
$table->string('template_name', 100)->nullable();
$table->string('template_language', 10)->nullable();
$table->longText('template_parameters')->nullable();
$table->enum('message_type', ['text', 'template'])->default('text');
$table->text('message_content')->nullable();
$table->date('scheduled_date');
$table->time('scheduled_time');
$table->enum('status', ['pending', 'sent', 'failed', 'cancelled'])->default('pending');
$table->datetime('sent_at')->nullable();
$table->text('error_message')->nullable();
$table->unsignedBigInteger('created_by')->nullable();
$table->datetime('created_at')->useCurrent();
});
Schema::create('whatsapp_webhook_logs', function (Blueprint $table) {
$table->id();
$table->longText('payload');
$table->text('response')->nullable();
$table->integer('status_code')->nullable();
$table->timestamp('created_at')->useCurrent();
});
}
public function down(): void
{
Schema::dropIfExists('whatsapp_webhook_logs');
Schema::dropIfExists('whatsapp_scheduled_messages');
}
};