- Added `ticket_generation_policy` column to `catalog_items` table to manage ticket generation strategies. - Created `ticket_validity_times` table to allow multiple validity times per ticket. - Introduced `validity_time_id` column in `event_dates` table to associate event dates with validity times. - Established `ticket_validity_groups` and `ticket_validity_group_times` tables to group validity times for tickets. - Updated seeder to set default ticket generation policy for specific tenant. - Enhanced tests to cover new functionality, including ticket generation based on event dates and validity times. - Refactored ticket validity checks to accommodate multiple validity times in a group.
103 lines
3.2 KiB
PHP
103 lines
3.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Event\Services;
|
|
|
|
use App\Domains\Tenant\Models\Tenant;
|
|
use Illuminate\Support\Facades\DB;
|
|
|
|
class EventService
|
|
{
|
|
private const CONTACT_CODES = [
|
|
'whatsapp_url' => 'whatsapp',
|
|
'instagram_url' => 'instagram',
|
|
'facebook_url' => 'facebook',
|
|
];
|
|
|
|
public function forTenant(Tenant $tenant): Tenant
|
|
{
|
|
return $tenant->load(['eventDates.validityTime', 'socialMedia']);
|
|
}
|
|
|
|
/** @param array<string, mixed> $data */
|
|
public function updateForTenant(Tenant $tenant, array $data): Tenant
|
|
{
|
|
return DB::transaction(function () use ($tenant, $data): Tenant {
|
|
$tenant = Tenant::query()->whereKey($tenant->getKey())->lockForUpdate()->firstOrFail();
|
|
$tenant->update([
|
|
'event_title' => $data['title'],
|
|
'event_location' => $data['location'],
|
|
]);
|
|
|
|
$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 $tenant->load(['eventDates.validityTime', 'socialMedia']);
|
|
});
|
|
}
|
|
|
|
/** @param array<int, array{date: string, start_time: string, end_time: string}> $dates */
|
|
private function syncDates(Tenant $tenant, array $dates): void
|
|
{
|
|
$existingDates = $tenant->eventDates()->get()->values();
|
|
|
|
foreach (array_values($dates) as $index => $date) {
|
|
$attributes = [
|
|
'date' => $date['date'],
|
|
'time_start' => $date['start_time'],
|
|
'time_end' => $date['end_time'],
|
|
];
|
|
|
|
$existingDate = $existingDates->get($index);
|
|
|
|
if ($existingDate) {
|
|
$existingDate->update($attributes);
|
|
} else {
|
|
$tenant->eventDates()->create($attributes);
|
|
}
|
|
}
|
|
|
|
$existingDates->slice(count($dates))->each->delete();
|
|
$tenant->unsetRelation('eventDates');
|
|
}
|
|
|
|
/** @param array<string, string|null> $contact */
|
|
private function syncLegacyContact(Tenant $tenant, array $contact): void
|
|
{
|
|
foreach (self::CONTACT_CODES as $field => $code) {
|
|
$url = $contact[$field] ?? null;
|
|
|
|
if ($url === null || $url === '') {
|
|
$tenant->socialMedia()->detach($code);
|
|
|
|
continue;
|
|
}
|
|
|
|
$tenant->socialMedia()->syncWithoutDetaching([
|
|
$code => ['url' => $url],
|
|
]);
|
|
}
|
|
|
|
$tenant->unsetRelation('socialMedia');
|
|
}
|
|
|
|
/** @param array<int, array{code: string, url: string, orden?: int}> $socialMedia */
|
|
private function syncSocialMedia(Tenant $tenant, array $socialMedia): void
|
|
{
|
|
$associations = [];
|
|
|
|
foreach (array_values($socialMedia) as $index => $item) {
|
|
$associations[$item['code']] = [
|
|
'url' => $item['url'],
|
|
'orden' => $item['orden'] ?? $index,
|
|
];
|
|
}
|
|
|
|
$tenant->socialMedia()->sync($associations);
|
|
$tenant->unsetRelation('socialMedia');
|
|
}
|
|
}
|