refactor(event): move event configuration onto tenant

This commit is contained in:
2026-08-07 09:08:14 -03:00
parent 60b8415c59
commit 7b7efeb4cc
27 changed files with 433 additions and 271 deletions

View File

@@ -2,7 +2,6 @@
namespace App\Domains\Event\Services;
use App\Domains\Event\Models\Event;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Support\Facades\DB;
@@ -14,53 +13,36 @@ class EventService
'facebook_url' => 'facebook',
];
public function activeForTenant(Tenant $tenant): Event
public function forTenant(Tenant $tenant): Tenant
{
return $tenant->events()
->whereKey($tenant->active_event_id)
->with(['dates', 'tenant.socialMedia'])
->firstOrFail();
return $tenant->load(['eventDates', 'socialMedia']);
}
/** @param array<string, mixed> $data */
public function updateActiveForTenant(Tenant $tenant, array $data): Event
public function updateForTenant(Tenant $tenant, array $data): Tenant
{
return DB::transaction(function () use ($tenant, $data): Event {
return DB::transaction(function () use ($tenant, $data): Tenant {
$tenant = Tenant::query()->whereKey($tenant->getKey())->lockForUpdate()->firstOrFail();
$event = $tenant->active_event_id === null
? $tenant->events()->create([
'name' => $data['title'],
'address' => $data['location'],
])
: $tenant->events()
->whereKey($tenant->active_event_id)
->lockForUpdate()
->firstOrFail();
$event->update([
'name' => $data['title'],
'address' => $data['location'],
$tenant->update([
'event_title' => $data['title'],
'event_location' => $data['location'],
]);
if ($tenant->active_event_id === null) {
$tenant->update(['active_event_id' => $event->id]);
}
$this->syncDates($event, $data['dates']);
$this->syncDates($tenant, $data['dates']);
if (array_key_exists('social_media', $data)) {
$this->syncSocialMedia($tenant, $data['social_media']);
} else {
$this->syncLegacyContact($tenant, $data['contact']);
}
return $event->load(['dates', 'tenant.socialMedia']);
return $tenant->load(['eventDates', 'socialMedia']);
});
}
/** @param array<int, array{date: string, start_time: string, end_time: string}> $dates */
private function syncDates(Event $event, array $dates): void
private function syncDates(Tenant $tenant, array $dates): void
{
$existingDates = $event->dates()->get()->values();
$existingDates = $tenant->eventDates()->get()->values();
foreach (array_values($dates) as $index => $date) {
$attributes = [
@@ -74,12 +56,12 @@ class EventService
if ($existingDate) {
$existingDate->update($attributes);
} else {
$event->dates()->create($attributes);
$tenant->eventDates()->create($attributes);
}
}
$existingDates->slice(count($dates))->each->delete();
$event->unsetRelation('dates');
$tenant->unsetRelation('eventDates');
}
/** @param array<string, string|null> $contact */