feat: enhance product detail retrieval to support variant selection and update resource fields

This commit is contained in:
2026-06-30 16:21:15 -03:00
parent 1d0f9e323d
commit 191a40fcb9
4 changed files with 36 additions and 20 deletions

View File

@@ -10,6 +10,7 @@ use App\Domains\Catalog\Services\ProductService;
use App\Domains\Tenant\Models\Tenant; use App\Domains\Tenant\Models\Tenant;
use App\Http\Controllers\Controller; use App\Http\Controllers\Controller;
use Illuminate\Http\JsonResponse; use Illuminate\Http\JsonResponse;
use Illuminate\Http\Request;
use Illuminate\Http\Response; use Illuminate\Http\Response;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException; use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -29,10 +30,12 @@ class ProductController extends Controller
return ProductResource::make($product)->response()->setStatusCode(201); return ProductResource::make($product)->response()->setStatusCode(201);
} }
public function show(Tenant $tenant, Product $producto, ProductService $productService): ProductResource public function show(Request $request, Tenant $tenant, Product $producto, ProductService $productService): ProductResource
{ {
$producto = $this->resolveScopedProduct($tenant, $producto); $producto = $this->resolveScopedProduct($tenant, $producto);
$producto = $productService->getProductDetail($tenant, $producto); $variantId = $request->query('variant_id');
$variantId = is_numeric($variantId) ? (int) $variantId : null;
$producto = $productService->getProductDetail($tenant, $producto, $variantId);
return ProductResource::make($producto); return ProductResource::make($producto);
} }

View File

@@ -28,7 +28,7 @@ class Product extends Model
protected $table = 'productos'; protected $table = 'productos';
protected ?ProductVariant $defaultVariant = null; protected ?ProductVariant $selectedVariant = null;
protected function casts(): array protected function casts(): array
{ {
return [ return [
@@ -278,13 +278,13 @@ class Product extends Model
public function setDefaultVariant(ProductVariant $variant): void public function setSelectedVariant(ProductVariant $variant): void
{ {
$this->defaultVariant = $variant; $this->selectedVariant = $variant;
} }
public function getDefaultVariant(): ?ProductVariant public function getSelectedVariant(): ?ProductVariant
{ {
return $this->defaultVariant; return $this->selectedVariant;
} }
} }

View File

@@ -32,16 +32,17 @@ class ProductResource extends JsonResource
), ),
'attributes' => AttributeResource::collection($this->whenLoaded('attributes')), 'attributes' => AttributeResource::collection($this->whenLoaded('attributes')),
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')), 'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
'default_variant' => $this->when( 'variant' => $this->when(
$this->getDefaultVariant() !== null, $this->getSelectedVariant() !== null,
function () { function () {
$defaultVariant = $this->getDefaultVariant(); $selectedVariant = $this->getSelectedVariant();
return [ return [
'variant_id' => $defaultVariant->id, 'variant_id' => $selectedVariant->id,
'images' => $defaultVariant->attachments->isNotEmpty() 'images' => $selectedVariant->attachments->isNotEmpty()
? $defaultVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values() ? $selectedVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
: $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(), : $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(),
'attributes' => $defaultVariant->definitions->mapWithKeys(function ($definition) { 'attributes' => $selectedVariant->definitions->mapWithKeys(function ($definition) {
return [$definition->attribute?->codigo => $definition->value]; return [$definition->attribute?->codigo => $definition->value];
})->toArray(), })->toArray(),
]; ];

View File

@@ -279,7 +279,7 @@ class ProductService
return $products; return $products;
} }
public function getProductDetail(Tenant $tenant, Product $product): Product public function getProductDetail(Tenant $tenant, Product $product, ?int $variantId = null): Product
{ {
$product->load([ $product->load([
'attachments', 'attachments',
@@ -288,15 +288,27 @@ class ProductService
'attributes.options', 'attributes.options',
]); ]);
$defaultVariant = $product->variants() $selectedVariantQuery = $product->variants()
->with([ ->with([
'attachments', 'attachments',
'definitions.attribute.options', 'definitions.attribute.options',
]) ]);
->first();
if ($defaultVariant) { $selectedVariant = $variantId !== null
$product->setDefaultVariant($defaultVariant); ? $selectedVariantQuery->whereKey($variantId)->first()
: null;
if ($selectedVariant === null) {
$selectedVariant = $product->variants()
->with([
'attachments',
'definitions.attribute.options',
])
->first();
}
if ($selectedVariant) {
$product->setSelectedVariant($selectedVariant);
} }
return $product; return $product;