feat(event): add EventDateInfoFormatter to format event date messages and implement related tests

This commit is contained in:
2026-09-15 16:08:42 -03:00
parent dcf4383fbf
commit 51937ca93f
4 changed files with 108 additions and 5 deletions

View File

@@ -0,0 +1,37 @@
<?php
namespace App\Domains\Event\Services;
use App\Domains\Event\Enums\EventDateChangeType;
use App\Domains\Event\Models\EventDate;
use App\Domains\Event\Models\EventDateChange;
use Illuminate\Support\Collection;
class EventDateInfoFormatter
{
/** @param Collection<int, EventDateChange> $changes */
public function format(EventDate $eventDate, Collection $changes): ?string
{
$messages = collect();
if ($eventDate->suspended_at !== null) {
$messages->push('Esta fecha fue cancelada.');
}
$sourceDates = $changes
->where('change_type', EventDateChangeType::Rescheduled)
->where('destination_event_date_id', $eventDate->getKey())
->pluck('previous_date')
->filter()
->map(fn ($date): string => $date->format('d/m/Y'))
->unique()
->values();
if ($sourceDates->isNotEmpty()) {
$verb = $sourceDates->count() === 1 ? 'se reprogramó' : 'se reprogramaron';
$messages->push("{$sourceDates->join(', ', ' y ')} {$verb} para este día.");
}
return $messages->isEmpty() ? null : $messages->join(' ');
}
}

View File

@@ -7,6 +7,7 @@ use App\Domains\Attachable\Models\AttachmentCrop;
use App\Domains\Catalog\Models\Category;
use App\Domains\Event\Models\EventDate;
use App\Domains\Event\Resources\EventDateChangeResource;
use App\Domains\Event\Services\EventDateInfoFormatter;
use App\Domains\Event\Services\EventDateNoticeFormatter;
use App\Domains\Menu\Models\Menu;
use App\Domains\Tenant\Models\Tenant;
@@ -25,6 +26,10 @@ class TenantResource extends JsonResource
*/
public function toArray(Request $request): array
{
$eventDateChanges = $this->relationLoaded('eventDateChanges')
? $this->eventDateChanges
: collect();
return [
'id' => $this->id,
'client_id' => $this->client_id,
@@ -54,14 +59,17 @@ class TenantResource extends JsonResource
'title' => $this->event_title,
'location' => $this->event_location,
'dates' => $this->eventDates
->filter(fn (EventDate $eventDate): bool => $eventDate->rescheduled_to_event_date_id === null
&& $eventDate->suspended_at === null
)
->filter(fn (EventDate $eventDate): bool => $eventDate->rescheduled_to_event_date_id === null)
->map(fn (EventDate $eventDate): array => [
'id' => $eventDate->id,
'date' => $eventDate->date->format('Y-m-d'),
'time_start' => $eventDate->time_start,
'time_end' => $eventDate->time_end,
'info_text' => app(EventDateInfoFormatter::class)->format(
$eventDate,
$eventDateChanges,
),
'isCanceled' => $eventDate->suspended_at !== null,
])->values(),
'date_changes' => $this->whenLoaded(
'eventDateChanges',