feat(event): add EventDateTextFormatter for formatting event dates; update Tenant model and resource to include event_date_text; create migration for event_date_text column; enhance tests for event date formatting and tenant updates

This commit is contained in:
2026-08-10 10:42:12 -03:00
parent 1cd60f7021
commit 4aaa66dc38
9 changed files with 181 additions and 0 deletions

View 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;
}
}

View File

@@ -13,6 +13,8 @@ class EventService
'facebook_url' => 'facebook',
];
public function __construct(protected EventDateTextFormatter $eventDateTextFormatter) {}
public function forTenant(Tenant $tenant): Tenant
{
return $tenant->load(['eventDates', 'socialMedia']);
@@ -26,6 +28,9 @@ class EventService
$tenant->update([
'event_title' => $data['title'],
'event_location' => $data['location'],
'event_date_text' => $this->eventDateTextFormatter->format(
array_column($data['dates'], 'date')
),
]);
$this->syncDates($tenant, $data['dates']);

View File

@@ -35,6 +35,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
'search_items_per_page',
'event_title',
'event_location',
'event_date_text',
])]
class Tenant extends Model
{

View File

@@ -32,6 +32,7 @@ class TenantResource extends JsonResource
'header_bg_color' => $this->header_bg_color,
'footer_bg_color' => $this->footer_bg_color,
'website_type_code' => $this->website_type_code,
'event_date_text' => $this->event_date_text,
'event' => $this->whenLoaded('eventDates', fn () => $this->event_title === null
? null
: [