feat(merchandise): implement MerchandiseController, MerchandiseService, and UpsertMerchandiseRequest for managing merchandise items and variants

This commit is contained in:
2026-08-07 15:05:50 -03:00
parent b05314b1dd
commit 63f98b5da2
6 changed files with 843 additions and 0 deletions

View File

@@ -0,0 +1,53 @@
<?php
namespace App\Domains\FiestaFutbolInfantil\Resources;
use App\Domains\Catalog\Models\CatalogItem;
use Illuminate\Http\Request;
use Illuminate\Http\Resources\Json\JsonResource;
/** @mixin CatalogItem */
class MerchandiseResource extends JsonResource
{
/** @return array<string, mixed> */
public function toArray(Request $request): array
{
$itemAttributes = $this->itemAttributes->keyBy(
fn ($itemAttribute) => $itemAttribute->attribute?->codigo
);
$colorAttribute = $itemAttributes->get('color');
$sizeAttribute = $itemAttributes->get('talle');
$colorOptions = $colorAttribute?->attribute?->options?->keyBy('value') ?? collect();
$sizeOptions = $sizeAttribute?->attribute?->options?->keyBy('value') ?? collect();
return [
'id' => $this->id,
'title' => $this->nombre,
'description' => $this->descripcion,
'max_units_per_user' => $this->max_units_per_user,
'variants' => $this->variants->map(function ($variant) use (
$colorAttribute,
$sizeAttribute,
$colorOptions,
$sizeOptions,
): array {
$colorValue = $variant->definitions
->firstWhere('item_attribute_id', $colorAttribute?->id)
?->value;
$sizeValue = $variant->definitions
->firstWhere('item_attribute_id', $sizeAttribute?->id)
?->value;
return [
'id' => $variant->id,
'color' => $colorOptions->get($colorValue)?->label ?? $colorValue,
'color_value' => $colorValue,
'size' => $sizeOptions->get($sizeValue)?->label ?? $sizeValue,
'size_value' => $sizeValue,
'stock' => $variant->inventory->real_stock,
'price' => number_format($variant->getPrice(), 2, '.', ''),
];
})->values(),
];
}
}