feat: update product and variant resources to include attribute options and metadata resolution
This commit is contained in:
@@ -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,
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user