feat(catalog): add event_id to catalog items and backfill existing records

This commit is contained in:
2026-09-18 12:19:03 -03:00
parent ee9f2ba538
commit cdc17a13e2
7 changed files with 101 additions and 0 deletions

View File

@@ -8,6 +8,7 @@ use App\Domains\Commerce\Catalog\Enums\InventoryPolicy;
use App\Domains\Commerce\Catalog\Enums\InventorySubject;
use App\Domains\Commerce\Catalog\Services\CatalogInventoryService;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Event\Models\Event;
use App\Domains\Ticketing\Ticket\Models\Ticket;
use Illuminate\Database\Eloquent\Attributes\Fillable;
use Illuminate\Database\Eloquent\Builder;
@@ -54,6 +55,7 @@ class CatalogItem extends Model
protected function casts(): array
{
return [
'event_id' => 'integer',
'category_id' => 'integer',
'brand_id' => 'integer',
'inventory_id' => 'integer',
@@ -73,6 +75,12 @@ class CatalogItem extends Model
return $this->belongsTo(Tenant::class, 'tenant_code', 'codigo');
}
/** @return BelongsTo<Event, $this> */
public function event(): BelongsTo
{
return $this->belongsTo(Event::class);
}
/** @return BelongsTo<Category, $this> */
public function category(): BelongsTo
{

View File

@@ -25,6 +25,7 @@ class StoreCatalogItemRequest extends FormRequest
return [
'tenant_code' => ['prohibited'],
'event_id' => ['prohibited'],
'type' => ['sometimes', Rule::enum(CatalogItemType::class)],
'category_id' => [
'sometimes',

View File

@@ -0,0 +1,26 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
public function up(): void
{
Schema::table('catalog_items', function (Blueprint $table): void {
$table->foreignId('event_id')
->nullable()
->after('tenant_code')
->constrained('events')
->nullOnDelete();
});
}
public function down(): void
{
Schema::table('catalog_items', function (Blueprint $table): void {
$table->dropConstrainedForeignId('event_id');
});
}
};

View File

@@ -0,0 +1,25 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Support\Facades\DB;
return new class extends Migration
{
public function up(): void
{
DB::table('tenants')
->whereIn('codigo', ['desfile_pura_tendencia', 'fiesta_futbol_infantil'])
->whereNotNull('active_event_id')
->get(['codigo', 'active_event_id'])
->each(function (object $tenant): void {
DB::table('catalog_items')
->where('tenant_code', $tenant->codigo)
->update(['event_id' => $tenant->active_event_id]);
});
}
public function down(): void
{
// The previous event associations cannot be recovered after this backfill.
}
};

View File

@@ -51,6 +51,15 @@ class DesfilePuraTendenciaSeeder extends Seeder
});
}
$activeEventId = DB::table('tenants')
->where('codigo', self::TENANT_CODE)
->value('active_event_id');
if ($activeEventId !== null) {
DB::table('catalog_items')
->where('tenant_code', self::TENANT_CODE)
->update(['event_id' => $activeEventId]);
}
$this->invitationPurchaseProvisioner->provision();
}

View File

@@ -15,6 +15,7 @@ use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Ticket\Enums\ValidityTimeType;
use App\Domains\Ticketing\Ticket\Models\ValidityTime;
use Illuminate\Database\Seeder;
use Illuminate\Support\Facades\DB;
use RuntimeException;
class FiestaFutbolInfantilProductSeeder extends Seeder
@@ -173,6 +174,12 @@ class FiestaFutbolInfantilProductSeeder extends Seeder
'group_name' => 'Productos',
'group_order' => 0,
]);
if ($tenant->active_event_id !== null) {
DB::table('catalog_items')
->where('tenant_code', $tenant->codigo)
->update(['event_id' => $tenant->active_event_id]);
}
}
private function deleteExistingCatalog(Tenant $tenant): void

View File

@@ -8,7 +8,9 @@ use App\Domains\Commerce\Catalog\Models\Attribute;
use App\Domains\Commerce\Catalog\Models\CatalogItem;
use App\Shared\Enums\FieldType;
use App\Domains\Core\Tenant\Models\Tenant;
use App\Domains\Ticketing\Event\Models\Event;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Schema;
use Illuminate\Support\Facades\Storage;
use Tests\TestCase;
@@ -48,6 +50,7 @@ class CatalogItemControllerTest extends TestCase
$response
->assertCreated()
->assertJsonPath('data.nombre', 'Shirt')
->assertJsonMissingPath('data.event_id')
->assertJsonMissingPath('data.group_order')
->assertJsonPath('data.max_units_per_user', 4)
->assertJsonCount(2, 'data.images')
@@ -60,6 +63,7 @@ class CatalogItemControllerTest extends TestCase
$this->assertSame([0, 1], $item->attachments()->get()->pluck('pivot.orden')->all());
$this->assertSame(4, $item->max_units_per_user);
$this->assertSame(7, $item->group_order);
$this->assertNull($item->event_id);
$this->assertSame([0], $variant->attachments()->get()->pluck('pivot.orden')->all());
$this->assertDatabaseHas('catalog_items_attachments', [
'catalog_item_id' => $item->id,
@@ -69,6 +73,27 @@ class CatalogItemControllerTest extends TestCase
]);
}
public function test_event_id_is_an_internal_nullable_catalog_item_link(): void
{
$this->assertTrue(Schema::hasColumn('catalog_items', 'event_id'));
$tenant = $this->createTenant('catalog-event-link');
$event = Event::query()->create([
'tenant_code' => $tenant->codigo,
'title' => 'Internal event',
]);
$this->postJson("/api/tenants/{$tenant->codigo}/catalog-items", [
'slug' => 'client-event-id',
'nombre' => 'Client event ID',
'precio' => 100,
'real_stock' => 1,
'event_id' => $event->id,
])->assertUnprocessable()->assertJsonValidationErrors('event_id');
$this->assertDatabaseMissing('catalog_items', ['slug' => 'client-event-id']);
}
public function test_it_validates_images_before_creating_the_catalog_item(): void
{
$tenant = $this->createTenant('validation');