feat(event): restore event model and migrate tenant event data
This commit is contained in:
@@ -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');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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
|
||||
{
|
||||
|
||||
@@ -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) {
|
||||
|
||||
@@ -18,7 +18,8 @@ class TenantInformationService
|
||||
'footerBackgroundImage',
|
||||
'socialMedia',
|
||||
'websiteExtras.websiteTypeExtra',
|
||||
'eventDates',
|
||||
'activeEvent.dates',
|
||||
'activeEvent.socialMedia',
|
||||
];
|
||||
|
||||
/**
|
||||
|
||||
@@ -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
|
||||
|
||||
48
app/Domains/Ticketing/Event/Models/Event.php
Normal file
48
app/Domains/Ticketing/Event/Models/Event.php
Normal 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');
|
||||
}
|
||||
}
|
||||
@@ -20,6 +20,7 @@ use Illuminate\Support\Carbon;
|
||||
|
||||
#[Fillable([
|
||||
'tenant_code',
|
||||
'event_id',
|
||||
'date',
|
||||
'time_start',
|
||||
'time_end',
|
||||
@@ -37,16 +38,16 @@ class EventDate extends Model
|
||||
protected static function booted(): void
|
||||
{
|
||||
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 {
|
||||
if ($eventDate->wasChanged(['date', 'time_start', 'time_end'])) {
|
||||
$eventDate->syncValidityTime();
|
||||
}
|
||||
|
||||
$eventDate->syncTenantDateText();
|
||||
$eventDate->syncEventDateText();
|
||||
});
|
||||
static::deleted(function (self $eventDate): void {
|
||||
$eventDate->syncTenantDateText();
|
||||
$eventDate->syncEventDateText();
|
||||
ValidityTime::query()
|
||||
->whereKey($eventDate->validity_time_id)
|
||||
->delete();
|
||||
@@ -58,6 +59,7 @@ class EventDate extends Model
|
||||
return [
|
||||
'date' => 'date:Y-m-d',
|
||||
'validity_time_id' => 'integer',
|
||||
'event_id' => 'integer',
|
||||
'rescheduled_to_event_date_id' => 'integer',
|
||||
'suspended_at' => 'datetime',
|
||||
];
|
||||
@@ -69,6 +71,12 @@ class EventDate extends Model
|
||||
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> */
|
||||
public function validityTime(): BelongsTo
|
||||
{
|
||||
@@ -156,17 +164,17 @@ class EventDate extends Model
|
||||
return EventDateStatus::Completed;
|
||||
}
|
||||
|
||||
private function syncTenantDateText(): void
|
||||
private function syncEventDateText(): void
|
||||
{
|
||||
$tenant = $this->tenant()->first();
|
||||
$event = $this->event()->first();
|
||||
|
||||
if (! $tenant) {
|
||||
if (! $event) {
|
||||
return;
|
||||
}
|
||||
|
||||
$tenant->update([
|
||||
'event_date_text' => app(EventDateTextFormatter::class)->format(
|
||||
$tenant->eventDates()
|
||||
$event->update([
|
||||
'date_text' => app(EventDateTextFormatter::class)->format(
|
||||
$event->dates()
|
||||
->whereNull('rescheduled_to_event_date_id')
|
||||
->whereNull('suspended_at')
|
||||
->pluck('date')
|
||||
|
||||
@@ -3,11 +3,11 @@
|
||||
namespace App\Domains\Ticketing\Event\Resources;
|
||||
|
||||
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\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin Tenant */
|
||||
/** @mixin Event */
|
||||
class EventResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
@@ -17,14 +17,18 @@ class EventResource extends JsonResource
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'title' => $this->event_title,
|
||||
'location' => $this->event_location,
|
||||
'allow_ticket_refund' => $this->allow_ticket_refund,
|
||||
'allow_ticket_total_refund' => $this->allow_ticket_total_refund,
|
||||
'allow_ticket_partial_refund' => $this->allow_ticket_partial_refund,
|
||||
'ticket_partial_refund_percentage' => $this->ticket_partial_refund_percentage,
|
||||
'title' => $this->title,
|
||||
'subtitle' => $this->subtitle,
|
||||
'description' => $this->description,
|
||||
'location' => $this->location,
|
||||
'date_text' => $this->date_text,
|
||||
'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(
|
||||
app(EventDateGroupingService::class)->group($this->eventDates)
|
||||
app(EventDateGroupingService::class)->group($this->dates)
|
||||
),
|
||||
'social_media' => $this->socialMedia->map(fn ($item): array => [
|
||||
'code' => $item->code,
|
||||
|
||||
@@ -12,6 +12,7 @@ use App\Domains\Ticketing\Event\Events\EventDateRescheduled;
|
||||
use App\Domains\Ticketing\Event\Events\EventDateSuspended;
|
||||
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\Tenant\Models\Tenant;
|
||||
use App\Domains\Ticketing\Ticket\Models\Ticket;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -33,19 +34,21 @@ class EventService
|
||||
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 */
|
||||
public function updateForTenant(Tenant $tenant, array $data): Tenant
|
||||
public function updateForTenant(Tenant $tenant, array $data): Event
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $data): Tenant {
|
||||
$tenant = Tenant::query()->whereKey($tenant->getKey())->lockForUpdate()->firstOrFail();
|
||||
return DB::transaction(function () use ($tenant, $data): Event {
|
||||
$event = $tenant->activeEvent;
|
||||
$event->update([
|
||||
'title' => $data['title'],
|
||||
'location' => $data['location'],
|
||||
]);
|
||||
$tenant->update([
|
||||
'event_title' => $data['title'],
|
||||
'event_location' => $data['location'],
|
||||
...array_intersect_key($data, array_flip([
|
||||
'allow_ticket_refund',
|
||||
'allow_ticket_total_refund',
|
||||
@@ -55,12 +58,12 @@ class EventService
|
||||
]);
|
||||
|
||||
if (array_key_exists('social_media', $data)) {
|
||||
$this->syncSocialMedia($tenant, $data['social_media']);
|
||||
} else {
|
||||
$this->syncLegacyContact($tenant, $data['contact']);
|
||||
$this->syncSocialMedia($event, $data['social_media']);
|
||||
} elseif (array_key_exists('contact', $data)) {
|
||||
$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
|
||||
{
|
||||
return DB::transaction(function () use ($tenant, $data): EventDate {
|
||||
$event = $tenant->activeEvent;
|
||||
$attributes = $this->dateAttributes($data);
|
||||
|
||||
if ($tenant->eventDates()->where($attributes)->exists()) {
|
||||
if ($event->dates()->where($attributes)->exists()) {
|
||||
throw ValidationException::withMessages([
|
||||
'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'])
|
||||
->lockForUpdate()
|
||||
->first();
|
||||
|
||||
if ($destination === null) {
|
||||
$destination = $tenant->eventDates()->create([
|
||||
$destination = $source->event->dates()->create([
|
||||
'tenant_code' => $tenant->codigo,
|
||||
'date' => $data['date'],
|
||||
'time_start' => $source->time_start,
|
||||
'time_end' => $source->time_end,
|
||||
@@ -250,7 +255,7 @@ class EventService
|
||||
$frontier = $affectedDateIds;
|
||||
|
||||
while ($frontier->isNotEmpty()) {
|
||||
$predecessors = $tenant->eventDates()
|
||||
$predecessors = $eventDate->event->dates()
|
||||
->whereIn('rescheduled_to_event_date_id', $frontier)
|
||||
->pluck('id')
|
||||
->diff($affectedDateIds)
|
||||
@@ -313,27 +318,27 @@ class EventService
|
||||
}
|
||||
|
||||
/** @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) {
|
||||
$url = $contact[$field] ?? null;
|
||||
|
||||
if ($url === null || $url === '') {
|
||||
$tenant->socialMedia()->detach($code);
|
||||
$event->socialMedia()->detach($code);
|
||||
|
||||
continue;
|
||||
}
|
||||
|
||||
$tenant->socialMedia()->syncWithoutDetaching([
|
||||
$event->socialMedia()->syncWithoutDetaching([
|
||||
$code => ['url' => $url],
|
||||
]);
|
||||
}
|
||||
|
||||
$tenant->unsetRelation('socialMedia');
|
||||
$event->unsetRelation('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 = [];
|
||||
|
||||
@@ -344,7 +349,7 @@ class EventService
|
||||
];
|
||||
}
|
||||
|
||||
$tenant->socialMedia()->sync($associations);
|
||||
$tenant->unsetRelation('socialMedia');
|
||||
$event->socialMedia()->sync($associations);
|
||||
$event->unsetRelation('socialMedia');
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user