feat: Update product seeder to include inventory policy and improve code readability

This commit is contained in:
2026-07-16 09:07:05 -03:00
parent 10157d7c63
commit 18c80b075b
2 changed files with 30 additions and 34 deletions

View File

@@ -2,6 +2,7 @@
namespace Database\Seeders;
use App\Domains\Catalog\Enums\InventoryPolicy;
use App\Domains\Catalog\Models\Attribute;
use App\Domains\Catalog\Models\Brand;
use App\Domains\Catalog\Models\Category;
@@ -64,9 +65,7 @@ class ProductCatalogFromImagesSeeder extends Seeder
'istockphoto-1675347112-2048x2048.jpg',
];
public function __construct(private readonly ProductService $productService)
{
}
public function __construct(private readonly ProductService $productService) {}
/**
* Run the database seeds.
@@ -162,9 +161,9 @@ class ProductCatalogFromImagesSeeder extends Seeder
}
/**
* @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} $pricing
* @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
* @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} $pricing
* @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 $variantGroups): void
{
@@ -206,6 +205,7 @@ class ProductCatalogFromImagesSeeder extends Seeder
'nombre' => $metadata['product_name'],
'descripcion' => $metadata['description'],
'precio' => $pricing['price'],
'inventory_policy' => InventoryPolicy::Tracked->value,
'attribute_ids' => array_values($attributeIds->all()),
]);
@@ -226,9 +226,11 @@ class ProductCatalogFromImagesSeeder extends Seeder
if ($attributeIds->isEmpty()) {
$this->productService->createVariant($product, [
'stock' => $variantGroup['stock'],
'inventory_policy' => InventoryPolicy::Tracked->value,
'definitions' => [],
'images' => $images,
]);
continue;
}
@@ -280,6 +282,7 @@ class ProductCatalogFromImagesSeeder extends Seeder
$this->productService->createVariant($product, [
'stock' => $stock,
'inventory_policy' => InventoryPolicy::Tracked->value,
'definitions' => $definitions,
'images' => $images,
]);
@@ -288,7 +291,7 @@ class ProductCatalogFromImagesSeeder extends Seeder
}
/**
* @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
* @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