feat(seeder): add Mutual SMEP category seeder and test cases
This commit is contained in:
@@ -30,6 +30,7 @@ class DatabaseSeeder extends Seeder
|
||||
DesfilePuraTendenciaSeeder::class,
|
||||
AttributeSeeder::class,
|
||||
CategorySeeder::class,
|
||||
MutualSmepCategorySeeder::class,
|
||||
BrandSeeder::class,
|
||||
ProductCatalogFromImagesSeeder::class,
|
||||
FiestaFutbolInfantilProductSeeder::class,
|
||||
|
||||
123
database/seeders/MutualSmepCategorySeeder.php
Normal file
123
database/seeders/MutualSmepCategorySeeder.php
Normal file
@@ -0,0 +1,123 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\Category;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MutualSmepCategorySeeder extends Seeder
|
||||
{
|
||||
private const TENANT_CODE = 'mutual_smep';
|
||||
|
||||
/** @var array<string, list<string>> */
|
||||
private const CATEGORIES = [
|
||||
'Dormitorio y Blanco' => [
|
||||
'Colchones',
|
||||
'Bases y Sommiers',
|
||||
'Acolchados y Edredones',
|
||||
'Frazadas y Mantas',
|
||||
'Infantil y Cuna',
|
||||
],
|
||||
'Electrodomésticos' => [
|
||||
'Heladeras',
|
||||
'Lavarropas y Secarropas',
|
||||
'Cocinas',
|
||||
'Microondas',
|
||||
'Hornos Eléctricos',
|
||||
'Calefones y Termotanques',
|
||||
'Purificadores y Extractores de Cocina',
|
||||
'Preparación de Alimentos',
|
||||
'Café y Desayuno',
|
||||
'Freidoras y Cocción',
|
||||
'Cuidado Personal',
|
||||
'Limpieza y Planchado',
|
||||
],
|
||||
'Climatización' => [
|
||||
'Aires Acondicionados',
|
||||
'Calefactores a Gas con Salida',
|
||||
'Calefactores a Gas sin Salida',
|
||||
'Calefacción Eléctrica',
|
||||
],
|
||||
'Tecnología' => [
|
||||
'Celulares',
|
||||
'Notebooks',
|
||||
'Smart TV',
|
||||
'Audio',
|
||||
'Accesorios de Computación',
|
||||
'Soportes para TV',
|
||||
],
|
||||
'Bicicletas y Aire Libre' => [
|
||||
'Bicicletas Infantiles',
|
||||
'Bicicletas para Adultos',
|
||||
'Piletas',
|
||||
],
|
||||
'Bazar' => [
|
||||
'Termos',
|
||||
],
|
||||
];
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if (! Tenant::query()->where('codigo', self::TENANT_CODE)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::transaction(function (): void {
|
||||
foreach (self::CATEGORIES as $parentName => $subcategoryNames) {
|
||||
$parent = Category::query()->updateOrCreate(
|
||||
[
|
||||
'tenant_code' => self::TENANT_CODE,
|
||||
'nombre' => $parentName,
|
||||
],
|
||||
[
|
||||
'categoria_id' => null,
|
||||
'is_enabled' => true,
|
||||
],
|
||||
);
|
||||
|
||||
foreach ($subcategoryNames as $subcategoryName) {
|
||||
Category::query()->updateOrCreate(
|
||||
[
|
||||
'tenant_code' => self::TENANT_CODE,
|
||||
'nombre' => $subcategoryName,
|
||||
],
|
||||
[
|
||||
'categoria_id' => $parent->id,
|
||||
'is_enabled' => true,
|
||||
],
|
||||
);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
DB::transaction(function (): void {
|
||||
foreach (self::CATEGORIES as $parentName => $subcategoryNames) {
|
||||
$parent = Category::query()
|
||||
->where('tenant_code', self::TENANT_CODE)
|
||||
->where('nombre', $parentName)
|
||||
->whereNull('categoria_id')
|
||||
->first();
|
||||
|
||||
if (! $parent instanceof Category) {
|
||||
continue;
|
||||
}
|
||||
|
||||
Category::query()
|
||||
->where('tenant_code', self::TENANT_CODE)
|
||||
->where('categoria_id', $parent->id)
|
||||
->whereIn('nombre', $subcategoryNames)
|
||||
->whereDoesntHave('subCategories')
|
||||
->delete();
|
||||
|
||||
if (! $parent->subCategories()->exists()) {
|
||||
$parent->delete();
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user