feat(event): restore event model and migrate tenant event data
This commit is contained in:
124
database/migrations/2026_09_18_000500_restore_events.php
Normal file
124
database/migrations/2026_09_18_000500_restore_events.php
Normal file
@@ -0,0 +1,124 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Database\Schema\Blueprint;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Illuminate\Support\Facades\Schema;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
public function up(): void
|
||||
{
|
||||
Schema::create('events', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->string('tenant_code');
|
||||
$table->string('title');
|
||||
$table->string('subtitle')->nullable();
|
||||
$table->text('description')->nullable();
|
||||
$table->string('location')->nullable();
|
||||
$table->text('date_text')->nullable();
|
||||
$table->timestamp('published_at')->nullable();
|
||||
$table->timestamps();
|
||||
$table->foreign('tenant_code')->references('codigo')->on('tenants')->cascadeOnUpdate()->cascadeOnDelete();
|
||||
$table->index(['tenant_code', 'published_at']);
|
||||
});
|
||||
|
||||
Schema::table('event_dates', function (Blueprint $table): void {
|
||||
$table->foreignId('event_id')->nullable()->after('tenant_code')->constrained('events')->cascadeOnDelete();
|
||||
});
|
||||
|
||||
Schema::create('event_social_media', function (Blueprint $table): void {
|
||||
$table->id();
|
||||
$table->foreignId('event_id')->constrained('events')->cascadeOnDelete();
|
||||
$table->string('social_media_code');
|
||||
$table->string('url');
|
||||
$table->unsignedInteger('orden')->default(0);
|
||||
$table->timestamps();
|
||||
$table->foreign('social_media_code')->references('code')->on('social_media')->cascadeOnDelete();
|
||||
$table->unique(['event_id', 'social_media_code']);
|
||||
});
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->foreignId('active_event_id')->nullable()->constrained('events')->nullOnDelete();
|
||||
});
|
||||
|
||||
DB::table('tenants')->orderBy('id')->chunk(100, function ($tenants): void {
|
||||
foreach ($tenants as $tenant) {
|
||||
$hasEvent = $tenant->event_title !== null
|
||||
|| DB::table('event_dates')->where('tenant_code', $tenant->codigo)->exists();
|
||||
|
||||
if (! $hasEvent) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$eventId = DB::table('events')->insertGetId([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'title' => $tenant->event_title ?? $tenant->nombre,
|
||||
'location' => $tenant->event_location,
|
||||
'date_text' => $tenant->event_date_text,
|
||||
'published_at' => now(),
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
|
||||
DB::table('event_dates')->where('tenant_code', $tenant->codigo)->update(['event_id' => $eventId]);
|
||||
|
||||
DB::table('tenant_social_media')
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->orderBy('id')
|
||||
->get()
|
||||
->each(function ($socialMedia) use ($eventId): void {
|
||||
DB::table('event_social_media')->insert([
|
||||
'event_id' => $eventId,
|
||||
'social_media_code' => $socialMedia->social_media_code,
|
||||
'url' => $socialMedia->url,
|
||||
'orden' => $socialMedia->orden,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
});
|
||||
|
||||
if ($tenant->storefront_website_type_code === 'onticket') {
|
||||
DB::table('tenants')->where('id', $tenant->id)->update(['active_event_id' => $eventId]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('event_dates', function (Blueprint $table): void {
|
||||
$table->foreignId('event_id')->nullable(false)->change();
|
||||
});
|
||||
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->dropColumn(['event_title', 'event_location', 'event_date_text']);
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('tenants', function (Blueprint $table): void {
|
||||
$table->string('event_title')->nullable();
|
||||
$table->string('event_location')->nullable();
|
||||
$table->text('event_date_text')->nullable();
|
||||
});
|
||||
|
||||
DB::table('tenants')->orderBy('id')->chunk(100, function ($tenants): void {
|
||||
foreach ($tenants as $tenant) {
|
||||
$event = DB::table('events')->where('tenant_code', $tenant->codigo)
|
||||
->when($tenant->active_event_id !== null, fn ($query) => $query->orderByRaw('id = ? desc', [$tenant->active_event_id]))
|
||||
->orderBy('id')->first();
|
||||
if ($event !== null) {
|
||||
DB::table('tenants')->where('id', $tenant->id)->update([
|
||||
'event_title' => $event->title,
|
||||
'event_location' => $event->location,
|
||||
'event_date_text' => $event->date_text,
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
Schema::table('tenants', fn (Blueprint $table) => $table->dropConstrainedForeignId('active_event_id'));
|
||||
Schema::table('event_dates', fn (Blueprint $table) => $table->dropConstrainedForeignId('event_id'));
|
||||
Schema::dropIfExists('event_social_media');
|
||||
Schema::dropIfExists('events');
|
||||
}
|
||||
};
|
||||
@@ -56,15 +56,12 @@ class DesfilePuraTendenciaSeeder extends Seeder
|
||||
|
||||
private function createTenant(Client $client): void
|
||||
{
|
||||
$this->tenantService->create([
|
||||
$tenant = $this->tenantService->create([
|
||||
'client_id' => $client->id,
|
||||
'codigo' => self::TENANT_CODE,
|
||||
'nombre' => 'Desfile Pura Tendencia',
|
||||
'dominio' => 'desfile-pura-tendencia.localhost',
|
||||
'site_title' => 'Desfile Pura Tendencia',
|
||||
'event_title' => 'Desfile Pura Tendencia',
|
||||
'event_location' => 'Salón Centro Recreativo Luz y Fuerza',
|
||||
'event_date_text' => '16 de Octubre 2026',
|
||||
'primary_color' => '#BA69A9',
|
||||
'secondary_color' => '#A0A0A0',
|
||||
'danger_color' => '#FF8888',
|
||||
@@ -107,6 +104,14 @@ class DesfilePuraTendenciaSeeder extends Seeder
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$event = $tenant->events()->create([
|
||||
'title' => 'Desfile Pura Tendencia',
|
||||
'location' => 'Salón Centro Recreativo Luz y Fuerza',
|
||||
'date_text' => '16 de Octubre 2026',
|
||||
'published_at' => now(),
|
||||
]);
|
||||
$tenant->update(['active_event_id' => $event->id]);
|
||||
}
|
||||
|
||||
private function createEventDate(): void
|
||||
@@ -124,6 +129,7 @@ class DesfilePuraTendenciaSeeder extends Seeder
|
||||
|
||||
DB::table('event_dates')->insert([
|
||||
'tenant_code' => self::TENANT_CODE,
|
||||
'event_id' => DB::table('events')->where('tenant_code', self::TENANT_CODE)->value('id'),
|
||||
'validity_time_id' => $validityTimeId,
|
||||
'date' => '2026-10-16',
|
||||
'time_start' => '20:30:00',
|
||||
|
||||
@@ -44,17 +44,37 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]));
|
||||
|
||||
$tenant->update([
|
||||
'event_title' => 'Fiesta Nacional del Fútbol Infantil',
|
||||
'event_location' => 'Sunchales, Santa Fe',
|
||||
]);
|
||||
$event = $tenant->events()->firstOrCreate(
|
||||
['title' => 'Fiesta Nacional del Fútbol Infantil'],
|
||||
['location' => 'Sunchales, Santa Fe', 'published_at' => now()],
|
||||
);
|
||||
$event->update(['location' => 'Sunchales, Santa Fe']);
|
||||
if ($tenant->storefront_website_type_code === 'onticket' && $tenant->active_event_id === null) {
|
||||
$tenant->update(['active_event_id' => $event->id]);
|
||||
}
|
||||
|
||||
$existingCodes = $event->socialMedia()->pluck('social_media.code')->all();
|
||||
$missingSocialMedia = $tenant->socialMedia()
|
||||
->get()
|
||||
->reject(fn ($network): bool => in_array($network->code, $existingCodes, true))
|
||||
->mapWithKeys(fn ($network): array => [
|
||||
$network->code => [
|
||||
'url' => $network->pivot->url,
|
||||
'orden' => $network->pivot->orden,
|
||||
],
|
||||
])
|
||||
->all();
|
||||
|
||||
if ($missingSocialMedia !== []) {
|
||||
$event->socialMedia()->syncWithoutDetaching($missingSocialMedia);
|
||||
}
|
||||
|
||||
$existingValidityTimeIds = $tenant->eventDates()->pluck('validity_time_id');
|
||||
$tenant->eventDates()->delete();
|
||||
$event->dates()->delete();
|
||||
ValidityTime::query()->whereKey($existingValidityTimeIds)->delete();
|
||||
|
||||
$eventDates = collect(['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'])
|
||||
->map(function (string $date) use ($tenant): EventDate {
|
||||
->map(function (string $date) use ($tenant, $event): EventDate {
|
||||
$validityTime = ValidityTime::query()->create([
|
||||
'type' => ValidityTimeType::FixedWindow,
|
||||
'start_time' => null,
|
||||
@@ -69,9 +89,10 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
|
||||
'time_start' => '00:00:00',
|
||||
'time_end' => '23:59:59',
|
||||
'validity_time_id' => $validityTime->id,
|
||||
'tenant_code' => $tenant->codigo,
|
||||
]);
|
||||
|
||||
return $tenant->eventDates()->save($eventDate);
|
||||
return $event->dates()->save($eventDate);
|
||||
});
|
||||
$dateIds = $eventDates->pluck('id')->map(fn ($id): int => (int) $id)->values();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user