Add tests for ticket validity and event date formatting
- Create TicketValiditySchemaTest to verify database schema for ticket validity. - Update CatalogModelsTest to include tests for event date attributes and selection options. - Introduce EventDateTextFormatterTest for formatting event dates in Spanish. - Refactor EventModelsTest to include validity time relationships. - Add SaleDetailResourceTest to ensure correct serialization of purchase items. - Enhance TicketTest with validity time checks and status management. - Implement ValidityTimeResourceTest to validate resource output for different validity types. - Add ValidityTimeTest to verify casting and validity checks for validity time types.
This commit is contained in:
73
app/Domains/Event/Services/EventDateTextFormatter.php
Normal file
73
app/Domains/Event/Services/EventDateTextFormatter.php
Normal file
@@ -0,0 +1,73 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Event\Services;
|
||||
|
||||
use DateTimeImmutable;
|
||||
|
||||
class EventDateTextFormatter
|
||||
{
|
||||
/** @var array<int, string> */
|
||||
private const MONTHS = [
|
||||
1 => 'Enero',
|
||||
2 => 'Febrero',
|
||||
3 => 'Marzo',
|
||||
4 => 'Abril',
|
||||
5 => 'Mayo',
|
||||
6 => 'Junio',
|
||||
7 => 'Julio',
|
||||
8 => 'Agosto',
|
||||
9 => 'Septiembre',
|
||||
10 => 'Octubre',
|
||||
11 => 'Noviembre',
|
||||
12 => 'Diciembre',
|
||||
];
|
||||
|
||||
/** @param iterable<string> $dates */
|
||||
public function format(iterable $dates): ?string
|
||||
{
|
||||
$normalizedDates = collect($dates)
|
||||
->map(fn (string $date): DateTimeImmutable => new DateTimeImmutable($date))
|
||||
->unique(fn (DateTimeImmutable $date): string => $date->format('Y-m-d'))
|
||||
->sortBy(fn (DateTimeImmutable $date): string => $date->format('Y-m-d'))
|
||||
->values();
|
||||
|
||||
if ($normalizedDates->isEmpty()) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$years = $normalizedDates
|
||||
->groupBy(fn (DateTimeImmutable $date): string => $date->format('Y'))
|
||||
->map(function ($yearDates, string $year): string {
|
||||
$months = $yearDates
|
||||
->groupBy(fn (DateTimeImmutable $date): string => $date->format('n'))
|
||||
->map(function ($monthDates, string $month): string {
|
||||
$days = $monthDates
|
||||
->map(fn (DateTimeImmutable $date): string => (string) ((int) $date->format('j')))
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return $this->join($days).' de '.self::MONTHS[(int) $month];
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return $this->join($months).' '.$year;
|
||||
})
|
||||
->values()
|
||||
->all();
|
||||
|
||||
return $this->join($years);
|
||||
}
|
||||
|
||||
/** @param array<int, string> $parts */
|
||||
private function join(array $parts): string
|
||||
{
|
||||
if (count($parts) <= 1) {
|
||||
return $parts[0] ?? '';
|
||||
}
|
||||
|
||||
$last = array_pop($parts);
|
||||
|
||||
return implode(', ', $parts).' y '.$last;
|
||||
}
|
||||
}
|
||||
@@ -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.validityTime', '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.validityTime', '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 */
|
||||
|
||||
Reference in New Issue
Block a user