feat(event): add EventDateInfoFormatter to format event date messages and implement related tests
This commit is contained in:
37
app/Domains/Event/Services/EventDateInfoFormatter.php
Normal file
37
app/Domains/Event/Services/EventDateInfoFormatter.php
Normal 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(' ');
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user