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

@@ -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,
];
}
}