feat: refactor cart item handling to use mapped buyable types and improve method signatures

This commit is contained in:
2026-07-14 16:43:00 -03:00
parent 480ee70393
commit c05206cc51
7 changed files with 50 additions and 31 deletions

View File

@@ -25,7 +25,7 @@ class PurchaseItemResource extends JsonResource
$imageUrl = null;
$attributes = [];
if ($this->buyable_type === 'variant' && $buyable) {
if ($this->buyable_type === \App\Domains\Catalog\Models\ProductVariant::class && $buyable) {
$imageUrl = $this->resolveImageUrl($buyable);
$attributes = $this->resolveAttributes($buyable);
}
@@ -35,7 +35,7 @@ class PurchaseItemResource extends JsonResource
'quantity' => $quantity,
'unit_price' => $this->formatMoney($unitPrice),
'line_total' => $this->formatMoney($lineTotal),
'buyable_type' => $this->buyable_type,
'buyable_type' => $this->mapBuyableTypeToAlias($this->buyable_type),
'buyable_id' => $this->buyable_id,
'item_details' => $buyable === null ? null : [
'nombre' => $buyable->getName(),
@@ -45,6 +45,15 @@ class PurchaseItemResource extends JsonResource
];
}
protected function mapBuyableTypeToAlias(?string $type): string
{
return match ($type) {
\App\Domains\Catalog\Models\ProductVariant::class => 'variant',
\App\Domains\Bundle\Models\Bundle::class => 'bundle',
default => 'unknown',
};
}
protected function resolveUnitPrice(): float
{
if ($this->resource instanceof PurchaseItem) {