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;
use App\Domains\Ticketing\Event\Models\Event;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
@@ -32,4 +33,17 @@ class SocialMedia extends Model
->withTimestamps()
->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\Ticketing\Event\Models\EventDate;
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\TenantMenu;
use App\Domains\Core\Tenant\Enums\CartEditingPolicy;
@@ -58,9 +59,7 @@ use Illuminate\Support\Facades\Schema;
'allow_ticket_total_refund',
'allow_ticket_partial_refund',
'ticket_partial_refund_percentage',
'event_title',
'event_location',
'event_date_text',
'active_event_id',
])]
class Tenant extends Model
{
@@ -229,6 +228,18 @@ class Tenant extends Model
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> */
public function eventDates(): HasMany
{

View File

@@ -6,6 +6,7 @@ use App\Shared\Attachable\Models\Attachment;
use App\Shared\Attachable\Models\AttachmentCrop;
use App\Domains\Commerce\Catalog\Models\Category;
use App\Domains\Ticketing\Event\Models\EventDate;
use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Ticketing\Event\Services\EventDateInfoFormatter;
use App\Domains\Core\Menu\Models\Menu;
use App\Domains\Core\Tenant\Models\Tenant;
@@ -49,26 +50,9 @@ class TenantResource extends JsonResource
'admin_website_type_code' => $this->admin_website_type_code,
'storefront_website_type_code' => $this->storefront_website_type_code,
'header_type' => $this->storefrontWebsiteType?->header_type ?? 'standard',
'event_date_text' => $this->event_date_text,
'event' => $this->whenLoaded('eventDates', fn () => $this->event_title === null
? null
: [
'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(),
]),
'active_event_id' => $this->active_event_id,
'event' => $this->whenLoaded('activeEvent', fn () => $this->activeEvent === null
? null : $this->formatEvent($this->activeEvent, $eventDateChanges)),
'extras' => $this->whenLoaded(
'websiteExtras',
fn () => $this->websiteExtras
@@ -100,7 +84,9 @@ class TenantResource extends JsonResource
'ticket_partial_refund_percentage' => $this->ticket_partial_refund_percentage,
'social_media' => $this->whenLoaded(
'socialMedia',
fn () => $this->socialMedia
fn () => ($this->relationLoaded('activeEvent') && $this->activeEvent !== null
? $this->activeEvent->socialMedia
: $this->socialMedia)
->map(fn ($socialMedia) => [
'code' => $socialMedia->code,
'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
{
if ($value instanceof Attachment) {

View File

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

View File

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