feat: update product and variant resources to include attribute options and metadata resolution

This commit is contained in:
2026-06-29 14:42:14 -03:00
parent b05a574610
commit a7372f9d08
6 changed files with 26 additions and 14 deletions

View File

@@ -45,7 +45,7 @@ class ProductController extends Controller
'attachments',
'brand',
'category',
'variants.definitions.attribute',
'variants.definitions.attribute.options',
'variants.attachments',
]));
}

View File

@@ -21,7 +21,7 @@ class ProductVariantController extends Controller
{
$query = ProductVariant::query()
->whereHas('product', fn ($query) => $query->where('tenant_codigo', $tenant->codigo))
->with(['product', 'definitions.attribute', 'attachments'])
->with(['product', 'definitions.attribute.options', 'attachments'])
->latest();
return ProductVariantResource::collection($query->paginateFromRequest())->response();
@@ -41,7 +41,7 @@ class ProductVariantController extends Controller
{
$productVariant = $this->resolveScopedVariant($tenant, $productVariant);
return ProductVariantResource::make($productVariant->load(['product', 'definitions.attribute', 'attachments']));
return ProductVariantResource::make($productVariant->load(['product', 'definitions.attribute.options', 'attachments']));
}
public function update(UpdateProductVariantRequest $request, Tenant $tenant, ProductVariant $productVariant, ProductService $productService): ProductVariantResource

View File

@@ -31,12 +31,7 @@ class ProductResource extends JsonResource
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
->values()
),
'variants' => $this->when(
$request->route()?->getName() !== 'productos.index',
fn () => ProductVariantResource::collection($this->whenLoaded('variants'))
),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
'variants' => ProductVariantResource::collection($this->whenLoaded('variants')),
];
}
}

View File

@@ -20,7 +20,26 @@ class ProductVariantDefinitionResource extends JsonResource
'producto_variante_id' => $this->producto_variante_id,
'attribute_id' => $this->attribute_id,
'value' => $this->value,
'attribute' => AttributeResource::make($this->whenLoaded('attribute')),
'attribute' => $this->whenLoaded('attribute', fn () => $this->attribute?->nombre),
'metadata' => $this->resolveMetadata(),
];
}
/**
* @return array<string, mixed>|null
*/
protected function resolveMetadata(): ?array
{
if (! $this->relationLoaded('attribute') || ! $this->attribute?->relationLoaded('options')) {
return null;
}
$option = $this->attribute->options->firstWhere('value', $this->value);
if ($option === null || $option->metadata === null) {
return null;
}
return $option->metadata;
}
}

View File

@@ -40,8 +40,6 @@ class ProductVariantResource extends JsonResource
?->values()
?? collect();
}),
'created_at' => $this->created_at,
'updated_at' => $this->updated_at,
];
}
}

View File

@@ -112,7 +112,7 @@ class ProductService
$this->syncVariantImages($variant, $images);
}
return $variant->load(['product', 'definitions.attribute', 'attachments']);
return $variant->load(['product', 'definitions.attribute.options', 'attachments']);
});
}
@@ -136,7 +136,7 @@ class ProductService
$this->syncVariantImages($updatedVariant, $images);
}
return $updatedVariant->load(['product', 'definitions.attribute', 'attachments']);
return $updatedVariant->load(['product', 'definitions.attribute.options', 'attachments']);
});
}