feat(seeder): implement Mutual SMEP attribute seeder and associated tests
This commit is contained in:
@@ -0,0 +1,139 @@
|
||||
<?php
|
||||
|
||||
use Illuminate\Database\Migrations\Migration;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
|
||||
return new class extends Migration
|
||||
{
|
||||
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 up(): void
|
||||
{
|
||||
if (! DB::table('tenants')->where('codigo', self::TENANT_CODE)->exists()) {
|
||||
return;
|
||||
}
|
||||
|
||||
DB::transaction(function (): void {
|
||||
foreach (self::ATTRIBUTES as $definition) {
|
||||
$attributeId = $this->upsertAttribute($definition);
|
||||
|
||||
DB::table('attribute_options')->where('attribute_id', $attributeId)->delete();
|
||||
|
||||
foreach ($definition['options'] as $index => $option) {
|
||||
DB::table('attribute_options')->insert([
|
||||
'attribute_id' => $attributeId,
|
||||
'value' => $option['value'],
|
||||
'label' => $option['label'],
|
||||
'sort_order' => $index + 1,
|
||||
'metadata' => isset($option['metadata'])
|
||||
? json_encode($option['metadata'], JSON_THROW_ON_ERROR)
|
||||
: null,
|
||||
'created_at' => now(),
|
||||
'updated_at' => now(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
public function down(): void
|
||||
{
|
||||
DB::table('attribute')
|
||||
->where('tenant_codigo', self::TENANT_CODE)
|
||||
->whereIn('codigo', array_column(self::ATTRIBUTES, 'codigo'))
|
||||
->delete();
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $definition */
|
||||
private function upsertAttribute(array $definition): int
|
||||
{
|
||||
$attributeId = DB::table('attribute')
|
||||
->where('tenant_codigo', self::TENANT_CODE)
|
||||
->where('codigo', $definition['codigo'])
|
||||
->value('id');
|
||||
$values = [
|
||||
'nombre' => $definition['nombre'],
|
||||
'is_required' => false,
|
||||
'metadata_schema' => isset($definition['metadata_schema'])
|
||||
? json_encode($definition['metadata_schema'], JSON_THROW_ON_ERROR)
|
||||
: null,
|
||||
'type' => 'select',
|
||||
'updated_at' => now(),
|
||||
];
|
||||
|
||||
if ($attributeId !== null) {
|
||||
DB::table('attribute')->where('id', $attributeId)->update($values);
|
||||
|
||||
return (int) $attributeId;
|
||||
}
|
||||
|
||||
return (int) DB::table('attribute')->insertGetId([
|
||||
'tenant_codigo' => self::TENANT_CODE,
|
||||
'codigo' => $definition['codigo'],
|
||||
...$values,
|
||||
'created_at' => now(),
|
||||
]);
|
||||
}
|
||||
};
|
||||
Reference in New Issue
Block a user