feat: update product and variant resources to include attribute options and metadata resolution
This commit is contained in:
@@ -45,7 +45,7 @@ class ProductController extends Controller
|
||||
'attachments',
|
||||
'brand',
|
||||
'category',
|
||||
'variants.definitions.attribute',
|
||||
'variants.definitions.attribute.options',
|
||||
'variants.attachments',
|
||||
]));
|
||||
}
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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')),
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -40,8 +40,6 @@ class ProductVariantResource extends JsonResource
|
||||
?->values()
|
||||
?? collect();
|
||||
}),
|
||||
'created_at' => $this->created_at,
|
||||
'updated_at' => $this->updated_at,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -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']);
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user