feat(seeder): implement Mutual SMEP attribute seeder and associated tests
This commit is contained in:
122
database/seeders/MutualSmepAttributeSeeder.php
Normal file
122
database/seeders/MutualSmepAttributeSeeder.php
Normal file
@@ -0,0 +1,122 @@
|
||||
<?php
|
||||
|
||||
namespace Database\Seeders;
|
||||
|
||||
use App\Domains\Commerce\Catalog\Models\Attribute;
|
||||
use App\Domains\Core\Tenant\Models\Tenant;
|
||||
use App\Shared\Enums\FieldType;
|
||||
use Illuminate\Database\Seeder;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
class MutualSmepAttributeSeeder extends Seeder
|
||||
{
|
||||
private const TENANT_CODE = 'mutual_smep';
|
||||
|
||||
/** @var list<array<string, mixed>> */
|
||||
private const ATTRIBUTES = [
|
||||
[
|
||||
'codigo' => 'color',
|
||||
'nombre' => 'Color',
|
||||
'metadata_schema' => [
|
||||
'hex' => ['type' => 'string'],
|
||||
],
|
||||
'options' => [
|
||||
['value' => 'Negro', 'label' => 'Negro', 'metadata' => ['hex' => '#000000']],
|
||||
['value' => 'Blanco', 'label' => 'Blanco', 'metadata' => ['hex' => '#FFFFFF']],
|
||||
['value' => 'Gris', 'label' => 'Gris', 'metadata' => ['hex' => '#808080']],
|
||||
['value' => 'Plata', 'label' => 'Plata', 'metadata' => ['hex' => '#C0C0C0']],
|
||||
['value' => 'Azul', 'label' => 'Azul', 'metadata' => ['hex' => '#0000FF']],
|
||||
['value' => 'Rojo', 'label' => 'Rojo', 'metadata' => ['hex' => '#DC3545']],
|
||||
['value' => 'Rosa', 'label' => 'Rosa', 'metadata' => ['hex' => '#FFC0CB']],
|
||||
['value' => 'Multicolor', 'label' => 'Multicolor'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'codigo' => 'medida_cama',
|
||||
'nombre' => 'Medida',
|
||||
'options' => [
|
||||
['value' => 'Cuna', 'label' => 'Cuna'],
|
||||
['value' => '1 Plaza', 'label' => '1 Plaza'],
|
||||
['value' => '1 1/2 Plazas', 'label' => '1 1/2 Plazas'],
|
||||
['value' => '2 Plazas', 'label' => '2 Plazas'],
|
||||
['value' => '2 1/2 Plazas', 'label' => '2 1/2 Plazas'],
|
||||
['value' => 'Queen', 'label' => 'Queen'],
|
||||
['value' => 'King', 'label' => 'King'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'codigo' => 'almacenamiento',
|
||||
'nombre' => 'Almacenamiento',
|
||||
'options' => [
|
||||
['value' => '128 GB', 'label' => '128 GB'],
|
||||
['value' => '256 GB', 'label' => '256 GB'],
|
||||
['value' => '512 GB', 'label' => '512 GB'],
|
||||
['value' => '1 TB', 'label' => '1 TB'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'codigo' => 'memoria_ram',
|
||||
'nombre' => 'Memoria RAM',
|
||||
'options' => [
|
||||
['value' => '4 GB', 'label' => '4 GB'],
|
||||
['value' => '8 GB', 'label' => '8 GB'],
|
||||
['value' => '16 GB', 'label' => '16 GB'],
|
||||
],
|
||||
],
|
||||
[
|
||||
'codigo' => 'rodado',
|
||||
'nombre' => 'Rodado',
|
||||
'options' => [
|
||||
['value' => '16', 'label' => '16'],
|
||||
['value' => '26', 'label' => '26'],
|
||||
['value' => '29', 'label' => '29'],
|
||||
],
|
||||
],
|
||||
];
|
||||
|
||||
public function run(): void
|
||||
{
|
||||
if (! Tenant::query()->where('codigo', self::TENANT_CODE)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::transaction(function (): void {
|
||||
foreach (self::ATTRIBUTES as $definition) {
|
||||
$options = $definition['options'];
|
||||
unset($definition['options']);
|
||||
|
||||
$attribute = Attribute::query()->updateOrCreate(
|
||||
[
|
||||
'tenant_codigo' => self::TENANT_CODE,
|
||||
'codigo' => $definition['codigo'],
|
||||
],
|
||||
[
|
||||
...$definition,
|
||||
'type' => FieldType::Select->value,
|
||||
'is_required' => false,
|
||||
'metadata_schema' => $definition['metadata_schema'] ?? null,
|
||||
],
|
||||
);
|
||||
|
||||
$attribute->options()->delete();
|
||||
$attribute->options()->createMany(
|
||||
collect($options)
|
||||
->values()
|
||||
->map(fn (array $option, int $index): array => [
|
||||
...$option,
|
||||
'sort_order' => $index + 1,
|
||||
])
|
||||
->all()
|
||||
);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
Attribute::query()
|
||||
->where('tenant_codigo', self::TENANT_CODE)
|
||||
->whereIn('codigo', array_column(self::ATTRIBUTES, 'codigo'))
|
||||
->delete();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user