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