feat(variant): refactor selection values to expose options with labels; update related resources and tests for consistency

This commit is contained in:
2026-08-10 12:27:00 -03:00
parent c6351d706f
commit e7c8807a67
11 changed files with 113 additions and 43 deletions

View File

@@ -7,7 +7,6 @@ use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\Variant;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
use Illuminate\Support\Collection;
/**
* @mixin CartItem
@@ -46,35 +45,13 @@ class CartItemResource extends JsonResource
'stock_tecnico' => $this->catalogItem->inventory_policy === InventoryPolicy::Unlimited
? null
: $variant->inventory->availableStock(),
'values' => $this->variantValues($variant),
'values' => $variant->selectionOptions(),
])
->values(),
],
];
}
/** @return Collection<string, string|array<int, string>> */
private function variantValues(Variant $variant): Collection
{
$values = $variant->selectionValues();
$eventDates = $variant->selectedEventDates();
if ($eventDates->isNotEmpty()) {
$labels = $eventDates
->map(fn ($eventDate): string => $eventDate->date->format('d/m/Y').' · '
.substr($eventDate->time_start, 0, 5).' a '
.substr($eventDate->time_end, 0, 5))
->values();
$values->put(
'event_date',
$labels->count() === 1 ? $labels->first() : $labels->all(),
);
}
return $values;
}
protected function formatMoney(float|int|string|null $amount): string
{
return number_format((float) ($amount ?? 0), 2, '.', '');