refactor(catalog): Complete catalog refactor to simplify its data model and its querying.

source commits: refactor/catalog
This commit is contained in:
2026-07-21 09:23:19 -03:00
parent d3182fbd13
commit 29a2ca19a3
116 changed files with 5141 additions and 5217 deletions

View File

@@ -2,136 +2,114 @@
namespace App\Domains\Purchase\Resources;
use App\Domains\Bundle\Models\Bundle;
use App\Domains\Cart\Models\CartItem;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Catalog\Models\CatalogItem;
use App\Domains\Catalog\Models\Variant;
use App\Domains\Purchase\Models\PurchaseItem;
use App\Domains\Shared\Contracts\Buyable;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/**
* @mixin PurchaseItem|CartItem
*/
/** @mixin PurchaseItem|CartItem */
class PurchaseItemResource extends JsonResource
{
/**
* @return array<string, mixed>
*/
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
/** @var Buyable|null $buyable */
$buyable = $this->buyable;
$quantity = (int) ($this->cantidad ?? 0);
$unitPrice = $this->resolveUnitPrice();
$lineTotal = $this->resolveLineTotal($unitPrice, $quantity);
$variant = $buyable instanceof ProductVariant ? $buyable : null;
if ($this->resource instanceof PurchaseItem) {
$imageUrl = $this->imageAttachment?->getTemporaryUrl(1440);
$attributes = $this->variant_attributes ?? [];
$imageUrl = null;
$attributes = [];
if ($this->buyable_type === ProductVariant::class && $buyable) {
$imageUrl = $this->resolveImageUrl($buyable);
$attributes = $this->resolveAttributes($buyable);
return [
'id' => $this->id,
'quantity' => (int) $this->cantidad,
'unit_price' => $this->formatMoney($this->precio_unitario),
'line_total' => $this->formatMoney($this->total),
'source_catalog_item_id' => $this->source_catalog_item_id,
'source_variant_id' => $this->source_variant_id,
'item_details' => [
'nombre' => $this->item_nombre,
'descripcion' => $this->descripcion,
'slug' => $this->slug,
'imagen' => $imageUrl,
'attributes' => $attributes,
],
];
}
$selectedItem = $this->selectedItem();
$catalogItem = $this->catalogItem;
$variant = $this->variant;
$quantity = (int) ($this->cantidad ?? 0);
$unitPrice = $this->resolveUnitPrice($selectedItem);
$lineTotal = $unitPrice * $quantity;
$imageUrl = $this->resolveImageUrl($selectedItem, $catalogItem);
return [
'id' => $this->id,
'quantity' => $quantity,
'unit_price' => $this->formatMoney($unitPrice),
'line_total' => $this->formatMoney($lineTotal),
'buyable_type' => $this->mapBuyableTypeToAlias($this->buyable_type),
'buyable_id' => $this->buyable_id,
'product' => $variant === null ? null : [
'id' => $variant->product?->id,
'nombre' => $variant->product?->nombre,
'slug' => $variant->product?->slug,
'catalog_item_id' => $this->catalog_item_id,
'variant_id' => $this->variant_id,
'product' => $catalogItem === null ? null : [
'id' => $catalogItem->id,
'nombre' => $catalogItem->nombre,
'descripcion' => $catalogItem->descripcion,
'slug' => $catalogItem->slug,
'imagen' => $imageUrl,
],
'variant' => $variant === null ? null : [
'id' => $variant->id,
'attributes' => $attributes,
'attributes' => $this->resolveAttributes($variant),
],
'item_details' => $buyable === null ? null : [
'nombre' => $buyable->getName(),
'item_details' => $selectedItem === null ? null : [
'nombre' => $selectedItem->getName(),
'descripcion' => $catalogItem?->descripcion,
'imagen' => $imageUrl,
'attributes' => $attributes,
'attributes' => $variant === null ? [] : $this->resolveAttributes($variant),
],
];
}
protected function mapBuyableTypeToAlias(?string $type): string
private function resolveUnitPrice(CatalogItem|Variant|null $selectedItem): float
{
return match ($type) {
ProductVariant::class => 'variant',
Bundle::class => 'bundle',
default => 'unknown',
};
return (float) ($selectedItem?->getPrice() ?? 0);
}
protected function resolveUnitPrice(): float
{
if ($this->resource instanceof PurchaseItem) {
return (float) ($this->precio_unitario ?? 0);
private function resolveImageUrl(
CatalogItem|Variant|null $selectedItem,
?CatalogItem $catalogItem,
): ?string {
$attachment = $selectedItem?->relationLoaded('attachments')
? $selectedItem->attachments->first()
: null;
if ($attachment === null && $catalogItem?->relationLoaded('attachments')) {
$attachment = $catalogItem->attachments->first();
}
if ($this->resource instanceof CartItem) {
return (float) ($this->buyable?->getPrice() ?? 0);
}
return 0.0;
return $attachment?->getTemporaryUrl(1440);
}
protected function resolveLineTotal(float $unitPrice, int $quantity): float
/** @return array<int, array{name: string, value: mixed}> */
private function resolveAttributes(Variant $variant): array
{
if ($this->resource instanceof PurchaseItem) {
return (float) ($this->total ?? 0);
}
return $unitPrice * $quantity;
}
protected function resolveImageUrl($buyable): ?string
{
if (! $buyable->relationLoaded('attachments')) {
return null;
}
$attachment = $buyable->attachments->first();
if ($attachment === null) {
return null;
}
return $attachment->getTemporaryUrl(1440);
}
/**
* @return array<int, array{name: string, value: mixed}>
*/
protected function resolveAttributes($buyable): array
{
if (! $buyable->relationLoaded('definitions')) {
if (! $variant->relationLoaded('definitions')) {
return [];
}
return $buyable->definitions
->map(function ($definition): array {
return [
'name' => (string) ($definition->productAttribute?->attribute?->nombre ?? ''),
'value' => $definition->value,
];
})
return $variant->definitions
->map(fn ($definition): array => [
'name' => (string) ($definition->itemAttribute?->attribute?->nombre ?? ''),
'value' => $definition->value,
])
->filter(fn (array $attribute): bool => $attribute['name'] !== '' || $attribute['value'] !== null)
->values()
->all();
}
protected function formatMoney(float|int|string|null $amount): ?string
private function formatMoney(float|int|string|null $amount): string
{
if ($amount === null) {
return null;
}
return number_format((float) $amount, 2, '.', '');
return number_format((float) ($amount ?? 0), 2, '.', '');
}
}

View File

@@ -89,7 +89,7 @@ class PurchaseResource extends JsonResource
return (float) $item->precio_unitario * $item->cantidad;
}
return (float) ($item->buyable?->getPrice() ?? 0) * $item->cantidad;
return (float) ($item->selectedItem()?->getPrice() ?? 0) * $item->cantidad;
}
protected function resolveItemTotal(PurchaseItem|CartItem $item): float