feat(event): restore event model and migrate tenant event data

This commit is contained in:
2026-09-18 11:31:51 -03:00
parent 1e67d27a90
commit 0170a2eabb
13 changed files with 339 additions and 81 deletions

View File

@@ -2,6 +2,7 @@
namespace App\Domains\Core\Tenant\Models; namespace App\Domains\Core\Tenant\Models;
use App\Domains\Ticketing\Event\Models\Event;
use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -32,4 +33,17 @@ class SocialMedia extends Model
->withTimestamps() ->withTimestamps()
->orderByPivot('orden'); ->orderByPivot('orden');
} }
/** @return BelongsToMany<Event, $this> */
public function events(): BelongsToMany
{
return $this->belongsToMany(
Event::class,
'event_social_media',
'social_media_code',
'event_id',
'code',
'id',
)->withPivot(['url', 'orden'])->withTimestamps()->orderByPivot('orden');
}
} }

View File

@@ -10,6 +10,7 @@ use App\Domains\Commerce\Catalog\Models\Category;
use App\Domains\Core\Client\Models\Client; use App\Domains\Core\Client\Models\Client;
use App\Domains\Ticketing\Event\Models\EventDate; use App\Domains\Ticketing\Event\Models\EventDate;
use App\Domains\Ticketing\Event\Models\EventDateChange; use App\Domains\Ticketing\Event\Models\EventDateChange;
use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Core\Menu\Models\Menu; use App\Domains\Core\Menu\Models\Menu;
use App\Domains\Core\Menu\Models\TenantMenu; use App\Domains\Core\Menu\Models\TenantMenu;
use App\Domains\Core\Tenant\Enums\CartEditingPolicy; use App\Domains\Core\Tenant\Enums\CartEditingPolicy;
@@ -58,9 +59,7 @@ use Illuminate\Support\Facades\Schema;
'allow_ticket_total_refund', 'allow_ticket_total_refund',
'allow_ticket_partial_refund', 'allow_ticket_partial_refund',
'ticket_partial_refund_percentage', 'ticket_partial_refund_percentage',
'event_title', 'active_event_id',
'event_location',
'event_date_text',
])] ])]
class Tenant extends Model class Tenant extends Model
{ {
@@ -229,6 +228,18 @@ class Tenant extends Model
return $this->hasMany(CatalogItem::class, 'tenant_code', 'codigo'); return $this->hasMany(CatalogItem::class, 'tenant_code', 'codigo');
} }
/** @return HasMany<Event, $this> */
public function events(): HasMany
{
return $this->hasMany(Event::class, 'tenant_code', 'codigo');
}
/** @return BelongsTo<Event, $this> */
public function activeEvent(): BelongsTo
{
return $this->belongsTo(Event::class, 'active_event_id');
}
/** @return HasMany<EventDate, $this> */ /** @return HasMany<EventDate, $this> */
public function eventDates(): HasMany public function eventDates(): HasMany
{ {

View File

@@ -6,6 +6,7 @@ use App\Shared\Attachable\Models\Attachment;
use App\Shared\Attachable\Models\AttachmentCrop; use App\Shared\Attachable\Models\AttachmentCrop;
use App\Domains\Commerce\Catalog\Models\Category; use App\Domains\Commerce\Catalog\Models\Category;
use App\Domains\Ticketing\Event\Models\EventDate; use App\Domains\Ticketing\Event\Models\EventDate;
use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Ticketing\Event\Services\EventDateInfoFormatter; use App\Domains\Ticketing\Event\Services\EventDateInfoFormatter;
use App\Domains\Core\Menu\Models\Menu; use App\Domains\Core\Menu\Models\Menu;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
@@ -49,26 +50,9 @@ class TenantResource extends JsonResource
'admin_website_type_code' => $this->admin_website_type_code, 'admin_website_type_code' => $this->admin_website_type_code,
'storefront_website_type_code' => $this->storefront_website_type_code, 'storefront_website_type_code' => $this->storefront_website_type_code,
'header_type' => $this->storefrontWebsiteType?->header_type ?? 'standard', 'header_type' => $this->storefrontWebsiteType?->header_type ?? 'standard',
'event_date_text' => $this->event_date_text, 'active_event_id' => $this->active_event_id,
'event' => $this->whenLoaded('eventDates', fn () => $this->event_title === null 'event' => $this->whenLoaded('activeEvent', fn () => $this->activeEvent === null
? null ? null : $this->formatEvent($this->activeEvent, $eventDateChanges)),
: [
'title' => $this->event_title,
'location' => $this->event_location,
'dates' => $this->eventDates
->filter(fn (EventDate $eventDate): bool => $eventDate->rescheduled_to_event_date_id === null)
->map(fn (EventDate $eventDate): array => [
'id' => $eventDate->id,
'date' => $eventDate->date->format('Y-m-d'),
'time_start' => $eventDate->time_start,
'time_end' => $eventDate->time_end,
'info_text' => app(EventDateInfoFormatter::class)->format(
$eventDate,
$eventDateChanges,
),
'isCanceled' => $eventDate->suspended_at !== null,
])->values(),
]),
'extras' => $this->whenLoaded( 'extras' => $this->whenLoaded(
'websiteExtras', 'websiteExtras',
fn () => $this->websiteExtras fn () => $this->websiteExtras
@@ -100,7 +84,9 @@ class TenantResource extends JsonResource
'ticket_partial_refund_percentage' => $this->ticket_partial_refund_percentage, 'ticket_partial_refund_percentage' => $this->ticket_partial_refund_percentage,
'social_media' => $this->whenLoaded( 'social_media' => $this->whenLoaded(
'socialMedia', 'socialMedia',
fn () => $this->socialMedia fn () => ($this->relationLoaded('activeEvent') && $this->activeEvent !== null
? $this->activeEvent->socialMedia
: $this->socialMedia)
->map(fn ($socialMedia) => [ ->map(fn ($socialMedia) => [
'code' => $socialMedia->code, 'code' => $socialMedia->code,
'icon' => $socialMedia->icon, 'icon' => $socialMedia->icon,
@@ -120,6 +106,34 @@ class TenantResource extends JsonResource
]; ];
} }
private function formatEvent(Event $event, Collection $eventDateChanges): array
{
return [
'id' => $event->id,
'title' => $event->title,
'subtitle' => $event->subtitle,
'description' => $event->description,
'location' => $event->location,
'date_text' => $event->date_text,
'dates' => $event->dates
->filter(fn (EventDate $date): bool => $date->rescheduled_to_event_date_id === null)
->map(fn (EventDate $date): array => [
'id' => $date->id,
'date' => $date->date->format('Y-m-d'),
'time_start' => $date->time_start,
'time_end' => $date->time_end,
'info_text' => app(EventDateInfoFormatter::class)->format($date, $eventDateChanges),
'isCanceled' => $date->suspended_at !== null,
])->values(),
'social_media' => $event->socialMedia->map(fn ($item): array => [
'code' => $item->code,
'icon' => $item->icon,
'name' => $item->name,
'url' => $item->pivot->url,
])->values(),
];
}
private function formatExtraConfig(mixed $value): mixed private function formatExtraConfig(mixed $value): mixed
{ {
if ($value instanceof Attachment) { if ($value instanceof Attachment) {

View File

@@ -18,7 +18,8 @@ class TenantInformationService
'footerBackgroundImage', 'footerBackgroundImage',
'socialMedia', 'socialMedia',
'websiteExtras.websiteTypeExtra', 'websiteExtras.websiteTypeExtra',
'eventDates', 'activeEvent.dates',
'activeEvent.socialMedia',
]; ];
/** /**

View File

@@ -169,6 +169,7 @@ class TenantService
*/ */
private function syncSocialMedia(Tenant $tenant, array $socialMedia): void private function syncSocialMedia(Tenant $tenant, array $socialMedia): void
{ {
$owner = $tenant->activeEvent ?? $tenant;
$associations = []; $associations = [];
foreach (array_values($socialMedia) as $index => $item) { foreach (array_values($socialMedia) as $index => $item) {
@@ -178,8 +179,8 @@ class TenantService
]; ];
} }
$tenant->socialMedia()->sync($associations); $owner->socialMedia()->sync($associations);
$tenant->unsetRelation('socialMedia'); $owner->unsetRelation('socialMedia');
} }
private function storeTenantImage(mixed $image): ?int private function storeTenantImage(mixed $image): ?int

View File

@@ -0,0 +1,48 @@
<?php
namespace App\Domains\Ticketing\Event\Models;
use App\Domains\Core\Tenant\Models\SocialMedia;
use App\Domains\Core\Tenant\Models\Tenant;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasMany;
#[Fillable(['tenant_code', 'title', 'subtitle', 'description', 'location', 'date_text', 'published_at'])]
class Event extends Model
{
use HasFactory;
protected function casts(): array
{
return ['published_at' => 'datetime'];
}
/** @return BelongsTo<Tenant, $this> */
public function tenant(): BelongsTo
{
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
}
/** @return HasMany<EventDate, $this> */
public function dates(): HasMany
{
return $this->hasMany(EventDate::class)->orderBy('date')->orderBy('time_start');
}
/** @return BelongsToMany<SocialMedia, $this> */
public function socialMedia(): BelongsToMany
{
return $this->belongsToMany(
SocialMedia::class,
'event_social_media',
'event_id',
'social_media_code',
'id',
'code',
)->withPivot(['url', 'orden'])->withTimestamps()->orderByPivot('orden');
}
}

View File

@@ -20,6 +20,7 @@ use Illuminate\Support\Carbon;
#[Fillable([ #[Fillable([
'tenant_code', 'tenant_code',
'event_id',
'date', 'date',
'time_start', 'time_start',
'time_end', 'time_end',
@@ -37,16 +38,16 @@ class EventDate extends Model
protected static function booted(): void protected static function booted(): void
{ {
static::creating(fn (self $eventDate) => $eventDate->syncValidityTime()); static::creating(fn (self $eventDate) => $eventDate->syncValidityTime());
static::created(fn (self $eventDate) => $eventDate->syncTenantDateText()); static::created(fn (self $eventDate) => $eventDate->syncEventDateText());
static::updated(function (self $eventDate): void { static::updated(function (self $eventDate): void {
if ($eventDate->wasChanged(['date', 'time_start', 'time_end'])) { if ($eventDate->wasChanged(['date', 'time_start', 'time_end'])) {
$eventDate->syncValidityTime(); $eventDate->syncValidityTime();
} }
$eventDate->syncTenantDateText(); $eventDate->syncEventDateText();
}); });
static::deleted(function (self $eventDate): void { static::deleted(function (self $eventDate): void {
$eventDate->syncTenantDateText(); $eventDate->syncEventDateText();
ValidityTime::query() ValidityTime::query()
->whereKey($eventDate->validity_time_id) ->whereKey($eventDate->validity_time_id)
->delete(); ->delete();
@@ -58,6 +59,7 @@ class EventDate extends Model
return [ return [
'date' => 'date:Y-m-d', 'date' => 'date:Y-m-d',
'validity_time_id' => 'integer', 'validity_time_id' => 'integer',
'event_id' => 'integer',
'rescheduled_to_event_date_id' => 'integer', 'rescheduled_to_event_date_id' => 'integer',
'suspended_at' => 'datetime', 'suspended_at' => 'datetime',
]; ];
@@ -69,6 +71,12 @@ class EventDate extends Model
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo'); return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
} }
/** @return BelongsTo<Event, $this> */
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
/** @return BelongsTo<ValidityTime, $this> */ /** @return BelongsTo<ValidityTime, $this> */
public function validityTime(): BelongsTo public function validityTime(): BelongsTo
{ {
@@ -156,17 +164,17 @@ class EventDate extends Model
return EventDateStatus::Completed; return EventDateStatus::Completed;
} }
private function syncTenantDateText(): void private function syncEventDateText(): void
{ {
$tenant = $this->tenant()->first(); $event = $this->event()->first();
if (! $tenant) { if (! $event) {
return; return;
} }
$tenant->update([ $event->update([
'event_date_text' => app(EventDateTextFormatter::class)->format( 'date_text' => app(EventDateTextFormatter::class)->format(
$tenant->eventDates() $event->dates()
->whereNull('rescheduled_to_event_date_id') ->whereNull('rescheduled_to_event_date_id')
->whereNull('suspended_at') ->whereNull('suspended_at')
->pluck('date') ->pluck('date')

View File

@@ -3,11 +3,11 @@
namespace App\Domains\Ticketing\Event\Resources; namespace App\Domains\Ticketing\Event\Resources;
use App\Domains\Ticketing\Event\Services\EventDateGroupingService; use App\Domains\Ticketing\Event\Services\EventDateGroupingService;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Ticketing\Event\Models\Event;
use Illuminate\Http\Request; use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource; use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin Tenant */ /** @mixin Event */
class EventResource extends JsonResource class EventResource extends JsonResource
{ {
/** @return array<string, mixed> */ /** @return array<string, mixed> */
@@ -17,14 +17,18 @@ class EventResource extends JsonResource
return [ return [
'id' => $this->id, 'id' => $this->id,
'title' => $this->event_title, 'title' => $this->title,
'location' => $this->event_location, 'subtitle' => $this->subtitle,
'allow_ticket_refund' => $this->allow_ticket_refund, 'description' => $this->description,
'allow_ticket_total_refund' => $this->allow_ticket_total_refund, 'location' => $this->location,
'allow_ticket_partial_refund' => $this->allow_ticket_partial_refund, 'date_text' => $this->date_text,
'ticket_partial_refund_percentage' => $this->ticket_partial_refund_percentage, 'published_at' => $this->published_at?->toIso8601String(),
'allow_ticket_refund' => $this->tenant->allow_ticket_refund,
'allow_ticket_total_refund' => $this->tenant->allow_ticket_total_refund,
'allow_ticket_partial_refund' => $this->tenant->allow_ticket_partial_refund,
'ticket_partial_refund_percentage' => $this->tenant->ticket_partial_refund_percentage,
'dates' => EventDateResource::collection( 'dates' => EventDateResource::collection(
app(EventDateGroupingService::class)->group($this->eventDates) app(EventDateGroupingService::class)->group($this->dates)
), ),
'social_media' => $this->socialMedia->map(fn ($item): array => [ 'social_media' => $this->socialMedia->map(fn ($item): array => [
'code' => $item->code, 'code' => $item->code,

View File

@@ -12,6 +12,7 @@ use App\Domains\Ticketing\Event\Events\EventDateRescheduled;
use App\Domains\Ticketing\Event\Events\EventDateSuspended; use App\Domains\Ticketing\Event\Events\EventDateSuspended;
use App\Domains\Ticketing\Event\Models\EventDate; use App\Domains\Ticketing\Event\Models\EventDate;
use App\Domains\Ticketing\Event\Models\EventDateChange; use App\Domains\Ticketing\Event\Models\EventDateChange;
use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Core\Tenant\Models\Tenant; use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Ticket\Models\Ticket; use App\Domains\Ticketing\Ticket\Models\Ticket;
use Illuminate\Support\Collection; use Illuminate\Support\Collection;
@@ -33,19 +34,21 @@ class EventService
private readonly InvalidateEventDateCartsService $invalidateEventDateCarts, private readonly InvalidateEventDateCartsService $invalidateEventDateCarts,
) {} ) {}
public function forTenant(Tenant $tenant): Tenant public function forTenant(Tenant $tenant): Event
{ {
return $tenant->load(['eventDates.validityTime', 'socialMedia']); return $tenant->activeEvent->load(['dates.validityTime', 'socialMedia']);
} }
/** @param array<string, mixed> $data */ /** @param array<string, mixed> $data */
public function updateForTenant(Tenant $tenant, array $data): Tenant public function updateForTenant(Tenant $tenant, array $data): Event
{ {
return DB::transaction(function () use ($tenant, $data): Tenant { return DB::transaction(function () use ($tenant, $data): Event {
$tenant = Tenant::query()->whereKey($tenant->getKey())->lockForUpdate()->firstOrFail(); $event = $tenant->activeEvent;
$event->update([
'title' => $data['title'],
'location' => $data['location'],
]);
$tenant->update([ $tenant->update([
'event_title' => $data['title'],
'event_location' => $data['location'],
...array_intersect_key($data, array_flip([ ...array_intersect_key($data, array_flip([
'allow_ticket_refund', 'allow_ticket_refund',
'allow_ticket_total_refund', 'allow_ticket_total_refund',
@@ -55,12 +58,12 @@ class EventService
]); ]);
if (array_key_exists('social_media', $data)) { if (array_key_exists('social_media', $data)) {
$this->syncSocialMedia($tenant, $data['social_media']); $this->syncSocialMedia($event, $data['social_media']);
} else { } elseif (array_key_exists('contact', $data)) {
$this->syncLegacyContact($tenant, $data['contact']); $this->syncLegacyContact($event, $data['contact']);
} }
return $tenant->load(['eventDates.validityTime', 'socialMedia']); return $event->load(['dates.validityTime', 'socialMedia']);
}); });
} }
@@ -68,15 +71,16 @@ class EventService
public function createDateForTenant(Tenant $tenant, array $data): EventDate public function createDateForTenant(Tenant $tenant, array $data): EventDate
{ {
return DB::transaction(function () use ($tenant, $data): EventDate { return DB::transaction(function () use ($tenant, $data): EventDate {
$event = $tenant->activeEvent;
$attributes = $this->dateAttributes($data); $attributes = $this->dateAttributes($data);
if ($tenant->eventDates()->where($attributes)->exists()) { if ($event->dates()->where($attributes)->exists()) {
throw ValidationException::withMessages([ throw ValidationException::withMessages([
'date' => ['La fecha y el horario ya existen.'], 'date' => ['La fecha y el horario ya existen.'],
]); ]);
} }
return $tenant->eventDates()->create($attributes)->load('validityTime'); return $event->dates()->create([...$attributes, 'tenant_code' => $tenant->codigo])->load('validityTime');
}); });
} }
@@ -102,13 +106,14 @@ class EventService
]); ]);
} }
$destination = $tenant->eventDates() $destination = $source->event->dates()
->whereDate('date', $data['date']) ->whereDate('date', $data['date'])
->lockForUpdate() ->lockForUpdate()
->first(); ->first();
if ($destination === null) { if ($destination === null) {
$destination = $tenant->eventDates()->create([ $destination = $source->event->dates()->create([
'tenant_code' => $tenant->codigo,
'date' => $data['date'], 'date' => $data['date'],
'time_start' => $source->time_start, 'time_start' => $source->time_start,
'time_end' => $source->time_end, 'time_end' => $source->time_end,
@@ -250,7 +255,7 @@ class EventService
$frontier = $affectedDateIds; $frontier = $affectedDateIds;
while ($frontier->isNotEmpty()) { while ($frontier->isNotEmpty()) {
$predecessors = $tenant->eventDates() $predecessors = $eventDate->event->dates()
->whereIn('rescheduled_to_event_date_id', $frontier) ->whereIn('rescheduled_to_event_date_id', $frontier)
->pluck('id') ->pluck('id')
->diff($affectedDateIds) ->diff($affectedDateIds)
@@ -313,27 +318,27 @@ class EventService
} }
/** @param array<string, string|null> $contact */ /** @param array<string, string|null> $contact */
private function syncLegacyContact(Tenant $tenant, array $contact): void private function syncLegacyContact(Event $event, array $contact): void
{ {
foreach (self::CONTACT_CODES as $field => $code) { foreach (self::CONTACT_CODES as $field => $code) {
$url = $contact[$field] ?? null; $url = $contact[$field] ?? null;
if ($url === null || $url === '') { if ($url === null || $url === '') {
$tenant->socialMedia()->detach($code); $event->socialMedia()->detach($code);
continue; continue;
} }
$tenant->socialMedia()->syncWithoutDetaching([ $event->socialMedia()->syncWithoutDetaching([
$code => ['url' => $url], $code => ['url' => $url],
]); ]);
} }
$tenant->unsetRelation('socialMedia'); $event->unsetRelation('socialMedia');
} }
/** @param array<int, array{code: string, url: string, orden?: int}> $socialMedia */ /** @param array<int, array{code: string, url: string, orden?: int}> $socialMedia */
private function syncSocialMedia(Tenant $tenant, array $socialMedia): void private function syncSocialMedia(Event $event, array $socialMedia): void
{ {
$associations = []; $associations = [];
@@ -344,7 +349,7 @@ class EventService
]; ];
} }
$tenant->socialMedia()->sync($associations); $event->socialMedia()->sync($associations);
$tenant->unsetRelation('socialMedia'); $event->unsetRelation('socialMedia');
} }
} }

View File

@@ -11,8 +11,9 @@ class EventFormService
/** @return array{social_media: Collection<int, SocialMedia>} */ /** @return array{social_media: Collection<int, SocialMedia>} */
public function get(Tenant $tenant): array public function get(Tenant $tenant): array
{ {
$urls = $tenant->socialMedia() $event = $tenant->activeEvent;
->pluck('tenant_social_media.url', 'social_media.code'); $urls = $event?->socialMedia()
->pluck('event_social_media.url', 'social_media.code') ?? collect();
return [ return [
'social_media' => SocialMedia::query() 'social_media' => SocialMedia::query()

View 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');
}
};

View File

@@ -56,15 +56,12 @@ class DesfilePuraTendenciaSeeder extends Seeder
private function createTenant(Client $client): void private function createTenant(Client $client): void
{ {
$this->tenantService->create([ $tenant = $this->tenantService->create([
'client_id' => $client->id, 'client_id' => $client->id,
'codigo' => self::TENANT_CODE, 'codigo' => self::TENANT_CODE,
'nombre' => 'Desfile Pura Tendencia', 'nombre' => 'Desfile Pura Tendencia',
'dominio' => 'desfile-pura-tendencia.localhost', 'dominio' => 'desfile-pura-tendencia.localhost',
'site_title' => 'Desfile Pura Tendencia', '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', 'primary_color' => '#BA69A9',
'secondary_color' => '#A0A0A0', 'secondary_color' => '#A0A0A0',
'danger_color' => '#FF8888', '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 private function createEventDate(): void
@@ -124,6 +129,7 @@ class DesfilePuraTendenciaSeeder extends Seeder
DB::table('event_dates')->insert([ DB::table('event_dates')->insert([
'tenant_code' => self::TENANT_CODE, 'tenant_code' => self::TENANT_CODE,
'event_id' => DB::table('events')->where('tenant_code', self::TENANT_CODE)->value('id'),
'validity_time_id' => $validityTimeId, 'validity_time_id' => $validityTimeId,
'date' => '2026-10-16', 'date' => '2026-10-16',
'time_start' => '20:30:00', 'time_start' => '20:30:00',

View File

@@ -44,17 +44,37 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
'tenant_code' => $tenant->codigo, 'tenant_code' => $tenant->codigo,
])); ]));
$tenant->update([ $event = $tenant->events()->firstOrCreate(
'event_title' => 'Fiesta Nacional del Fútbol Infantil', ['title' => 'Fiesta Nacional del Fútbol Infantil'],
'event_location' => 'Sunchales, Santa Fe', ['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'); $existingValidityTimeIds = $tenant->eventDates()->pluck('validity_time_id');
$tenant->eventDates()->delete(); $event->dates()->delete();
ValidityTime::query()->whereKey($existingValidityTimeIds)->delete(); ValidityTime::query()->whereKey($existingValidityTimeIds)->delete();
$eventDates = collect(['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12']) $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([ $validityTime = ValidityTime::query()->create([
'type' => ValidityTimeType::FixedWindow, 'type' => ValidityTimeType::FixedWindow,
'start_time' => null, 'start_time' => null,
@@ -69,9 +89,10 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
'time_start' => '00:00:00', 'time_start' => '00:00:00',
'time_end' => '23:59:59', 'time_end' => '23:59:59',
'validity_time_id' => $validityTime->id, '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(); $dateIds = $eventDates->pluck('id')->map(fn ($id): int => (int) $id)->values();