feat: update product resource and seeder to support variant groups and remove unused fields

This commit is contained in:
2026-06-30 16:11:18 -03:00
parent fdeba7540b
commit 1d0f9e323d
3 changed files with 105 additions and 46 deletions

View File

@@ -17,7 +17,6 @@ class ProductResource extends JsonResource
{ {
return [ return [
'id' => $this->id, 'id' => $this->id,
'tenant_codigo' => $this->tenant_codigo,
'category_id' => $this->categoria_id, 'category_id' => $this->categoria_id,
'brand_id' => $this->brand_id, 'brand_id' => $this->brand_id,
'slug' => $this->slug, 'slug' => $this->slug,

View File

@@ -273,6 +273,7 @@ class ProductService
} }
$product->setRelation('attachments', $resolvedAttachment ? collect([$resolvedAttachment]) : collect()); $product->setRelation('attachments', $resolvedAttachment ? collect([$resolvedAttachment]) : collect());
$product->unsetRelation('variants');
} }
return $products; return $products;

View File

@@ -17,7 +17,7 @@ use RuntimeException;
class ProductCatalogFromImagesSeeder extends Seeder class ProductCatalogFromImagesSeeder extends Seeder
{ {
/** /**
* @var array<string, array{price: int, stock: int}> * @var array<string, array{price: int, stock?: int, variant_groups?: array<string, array{stock: int}>}>
*/ */
private const PRODUCT_CATALOG = [ private const PRODUCT_CATALOG = [
'buzo_blanco_adidas' => ['price' => 89999, 'stock' => 12], 'buzo_blanco_adidas' => ['price' => 89999, 'stock' => 12],
@@ -25,10 +25,20 @@ class ProductCatalogFromImagesSeeder extends Seeder
'buzo_gris_under_armour' => ['price' => 96999, 'stock' => 8], 'buzo_gris_under_armour' => ['price' => 96999, 'stock' => 8],
'buzo_negro_adidas' => ['price' => 91999, 'stock' => 9], 'buzo_negro_adidas' => ['price' => 91999, 'stock' => 9],
'camiseta_rosario_central' => ['price' => 69999, 'stock' => 20], 'camiseta_rosario_central' => ['price' => 69999, 'stock' => 20],
'pantalon_blanco' => ['price' => 55999, 'stock' => 14], 'pantalon' => [
'pantalon_negro' => ['price' => 57999, 'stock' => 14], 'price' => 55999,
'remera_blanca' => ['price' => 35999, 'stock' => 22], 'variant_groups' => [
'remera_negra' => ['price' => 36999, 'stock' => 22], 'pantalon_blanco' => ['stock' => 14],
'pantalon_negro' => ['stock' => 14],
],
],
'remera' => [
'price' => 35999,
'variant_groups' => [
'remera_blanca' => ['stock' => 22],
'remera_negra' => ['stock' => 22],
],
],
'zapatillas_lecoq' => ['price' => 109999, 'stock' => 6], 'zapatillas_lecoq' => ['price' => 109999, 'stock' => 6],
'zapatillas_topper' => ['price' => 104999, 'stock' => 7], 'zapatillas_topper' => ['price' => 104999, 'stock' => 7],
]; ];
@@ -84,28 +94,52 @@ class ProductCatalogFromImagesSeeder extends Seeder
}); });
$productsToSeed = collect(self::PRODUCT_CATALOG) $productsToSeed = collect(self::PRODUCT_CATALOG)
->map(function (array $pricing, string $groupKey) use ($groupedFiles, $tenant): array { ->map(function (array $catalogConfig, string $productKey) use ($groupedFiles): array {
$filesForGroup = $groupedFiles->get($groupKey); if (! isset($catalogConfig['variant_groups']) && ! array_key_exists('stock', $catalogConfig)) {
throw new RuntimeException("Missing stock configuration for catalog product '{$productKey}'.");
if ($filesForGroup === null || $filesForGroup->isEmpty()) {
throw new RuntimeException("No images found for catalog group '{$groupKey}'.");
} }
$metadata = $this->parseGroupKey($groupKey); $variantGroups = collect($catalogConfig['variant_groups'] ?? [
$productKey => ['stock' => $catalogConfig['stock']],
])
->map(function (array $variantConfig, string $groupKey) use ($groupedFiles): array {
$filesForGroup = $groupedFiles->get($groupKey);
if ($filesForGroup === null || $filesForGroup->isEmpty()) {
throw new RuntimeException("No images found for catalog group '{$groupKey}'.");
}
return [
'group_key' => $groupKey,
'stock' => $variantConfig['stock'],
'files' => $filesForGroup->all(),
'metadata' => $this->parseGroupKey($groupKey),
];
})
->values()
->all();
return [ return [
'group_key' => $groupKey, 'pricing' => ['price' => $catalogConfig['price']],
'pricing' => $pricing, 'metadata' => $this->buildProductMetadata($productKey, $variantGroups),
'tenant' => $tenant, 'variant_groups' => $variantGroups,
'files' => $filesForGroup->all(),
'metadata' => $metadata,
]; ];
}) })
->values(); ->values();
$productSlugsToDelete = $productsToSeed
->flatMap(fn (array $catalogProduct): array => [
$catalogProduct['metadata']['product_slug'],
...collect($catalogProduct['variant_groups'])
->pluck('group_key')
->all(),
])
->unique()
->values();
$existingProducts = Product::query() $existingProducts = Product::query()
->where('tenant_codigo', $tenant->codigo) ->where('tenant_codigo', $tenant->codigo)
->whereIn('slug', $productsToSeed->pluck('metadata.product_slug')) ->whereIn('slug', $productSlugsToDelete)
->with(['attachments', 'variants.attachments']) ->with(['attachments', 'variants.attachments'])
->get(); ->get();
@@ -118,17 +152,17 @@ class ProductCatalogFromImagesSeeder extends Seeder
$tenant, $tenant,
$catalogProduct['metadata'], $catalogProduct['metadata'],
$catalogProduct['pricing'], $catalogProduct['pricing'],
$catalogProduct['files'], $catalogProduct['variant_groups'],
); );
} }
} }
/** /**
* @param array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string} $metadata * @param array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string} $metadata
* @param array{price: int, stock: int} $pricing * @param array{price: int} $pricing
* @param array<int, \SplFileInfo> $files * @param array<int, array{files: array<int, \SplFileInfo>, group_key: string, metadata: array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string}, stock: int}> $variantGroups
*/ */
private function seedProduct(Tenant $tenant, array $metadata, array $pricing, array $files): void private function seedProduct(Tenant $tenant, array $metadata, array $pricing, array $variantGroups): void
{ {
$brand = null; $brand = null;
@@ -175,38 +209,63 @@ class ProductCatalogFromImagesSeeder extends Seeder
? self::SHOE_SIZES ? self::SHOE_SIZES
: self::CLOTHING_SIZES; : self::CLOTHING_SIZES;
$images = array_map( foreach ($variantGroups as $variantGroup) {
fn (\SplFileInfo $file): UploadedFile => $this->createUploadedFile($file->getPathname()), $images = array_map(
$files fn (\SplFileInfo $file): UploadedFile => $this->createUploadedFile($file->getPathname()),
); $variantGroup['files']
);
foreach ($sizes as $size) { foreach ($sizes as $size) {
$definitions = []; $definitions = [];
if (isset($attributeIds['color']) && $variantGroup['metadata']['color'] !== null) {
$definitions[] = [
'attribute_id' => $attributeIds['color'],
'value' => $variantGroup['metadata']['color'],
];
}
$sizeAttributeCode = $metadata['type'] === 'zapatillas'
? 'talle_numerico'
: 'talle';
if (isset($attributeIds['color']) && $metadata['color'] !== null) {
$definitions[] = [ $definitions[] = [
'attribute_id' => $attributeIds['color'], 'attribute_id' => $attributeIds[$sizeAttributeCode],
'value' => $metadata['color'], 'value' => $size,
]; ];
$this->productService->createVariant($product, [
'stock' => $variantGroup['stock'],
'definitions' => $definitions,
'images' => $images,
]);
} }
$sizeAttributeCode = $metadata['type'] === 'zapatillas'
? 'talle_numerico'
: 'talle';
$definitions[] = [
'attribute_id' => $attributeIds[$sizeAttributeCode],
'value' => $size,
];
$this->productService->createVariant($product, [
'stock' => $pricing['stock'],
'definitions' => $definitions,
'images' => $images,
]);
} }
} }
/**
* @param array<int, array{files: array<int, \SplFileInfo>, group_key: string, metadata: array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string}, stock: int}> $variantGroups
* @return array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string}
*/
private function buildProductMetadata(string $productKey, array $variantGroups): array
{
$metadata = $this->parseGroupKey($productKey);
$hasColorVariants = collect($variantGroups)
->contains(fn (array $variantGroup): bool => $variantGroup['metadata']['color'] !== null);
if ($hasColorVariants && ! in_array('color', $metadata['attribute_codes'], true)) {
array_unshift($metadata['attribute_codes'], 'color');
}
if ($hasColorVariants && $metadata['color'] === null) {
$category = Str::lower($metadata['category_name']);
$brandText = $metadata['brand_name'] !== null ? " {$metadata['brand_name']}" : '';
$metadata['description'] = "Producto seed de {$category}{$brandText} con variantes por color y talle.";
}
return $metadata;
}
/** /**
* @return array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string} * @return array{attribute_codes: array<int, string>, brand_name: string|null, category_name: string, color: string|null, description: string, product_name: string, product_slug: string, type: string}
*/ */