feat: update product and variant resources to improve variant selection handling and response structure
This commit is contained in:
@@ -59,7 +59,7 @@ class ProductController extends Controller
|
||||
protected function resolveScopedProduct(Tenant $tenant, Product $product): Product
|
||||
{
|
||||
if ($product->tenant_codigo !== $tenant->codigo) {
|
||||
throw new NotFoundHttpException('Product not found for tenant.');
|
||||
throw new NotFoundHttpException('Product not found.');
|
||||
}
|
||||
|
||||
return $product;
|
||||
|
||||
@@ -34,19 +34,7 @@ class ProductResource extends JsonResource
|
||||
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
|
||||
'variant' => $this->when(
|
||||
$this->getSelectedVariant() !== null,
|
||||
function () {
|
||||
$selectedVariant = $this->getSelectedVariant();
|
||||
|
||||
return [
|
||||
'variant_id' => $selectedVariant->id,
|
||||
'images' => $selectedVariant->attachments->isNotEmpty()
|
||||
? $selectedVariant->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values()
|
||||
: $this->attachments->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))->values(),
|
||||
'attributes' => $selectedVariant->definitions->mapWithKeys(function ($definition) {
|
||||
return [$definition->productAttribute?->attribute?->codigo => $definition->value];
|
||||
})->toArray(),
|
||||
];
|
||||
}
|
||||
fn () => ProductVariantResource::make($this->getSelectedVariant())
|
||||
),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -17,10 +17,17 @@ class ProductVariantResource extends JsonResource
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'producto_id' => $this->producto_id,
|
||||
'stock' => $this->stock,
|
||||
'product' => ProductResource::make($this->whenLoaded('product')),
|
||||
'definitions' => ProductVariantDefinitionResource::collection($this->whenLoaded('definitions')),
|
||||
'definitions' => $this->whenLoaded(
|
||||
'definitions',
|
||||
fn () => $this->definitions
|
||||
->mapWithKeys(fn ($definition) => [
|
||||
$definition->productAttribute?->attribute?->codigo => $definition->value,
|
||||
])
|
||||
->filter(fn ($value, $key) => $key !== null)
|
||||
->toArray()
|
||||
),
|
||||
'images' => $this->whenLoaded('attachments', function () {
|
||||
if ($this->attachments->isNotEmpty()) {
|
||||
return $this->attachments
|
||||
|
||||
@@ -286,6 +286,7 @@ class ProductService
|
||||
'brand',
|
||||
'category',
|
||||
'attributes.options',
|
||||
'variants.definitions.productAttribute.attribute.options',
|
||||
]);
|
||||
|
||||
$selectedVariantQuery = $product->variants()
|
||||
|
||||
Reference in New Issue
Block a user