feat(event): restore event model and migrate tenant event data
This commit is contained in:
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