diff --git a/app/Domains/Catalog/Models/ProductVariant.php b/app/Domains/Catalog/Models/ProductVariant.php index 15aee27..1d6b812 100644 --- a/app/Domains/Catalog/Models/ProductVariant.php +++ b/app/Domains/Catalog/Models/ProductVariant.php @@ -2,10 +2,12 @@ namespace App\Domains\Catalog\Models; +use App\Domains\Attachable\Models\Attachment; use Illuminate\Database\Eloquent\Attributes\Fillable; use Illuminate\Database\Eloquent\Factories\HasFactory; use Illuminate\Database\Eloquent\Model; use Illuminate\Database\Eloquent\Relations\BelongsTo; +use Illuminate\Database\Eloquent\Relations\BelongsToMany; use Illuminate\Database\Eloquent\Relations\HasMany; #[Fillable([ @@ -46,4 +48,17 @@ class ProductVariant extends Model { return $this->hasMany(ProductVariantDefinition::class, 'producto_variante_id'); } + + /** + * @return BelongsToMany + */ + public function attachments(): BelongsToMany + { + return $this->belongsToMany( + Attachment::class, + 'variantes_attachments', + 'variante_id', + 'attachment_id' + )->withTimestamps(); + } } diff --git a/database/migrations/2026_06_29_000000_create_variantes_attachments_table.php b/database/migrations/2026_06_29_000000_create_variantes_attachments_table.php new file mode 100644 index 0000000..5fbfbf7 --- /dev/null +++ b/database/migrations/2026_06_29_000000_create_variantes_attachments_table.php @@ -0,0 +1,35 @@ +id(); + $table->foreignId('variante_id') + ->constrained('productos_variantes') + ->cascadeOnDelete(); + $table->foreignId('attachment_id') + ->constrained('attachments') + ->cascadeOnDelete(); + $table->timestamps(); + + $table->unique(['variante_id', 'attachment_id']); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('variantes_attachments'); + } +}; diff --git a/tests/Feature/Catalog/ProductVariantAttachmentTest.php b/tests/Feature/Catalog/ProductVariantAttachmentTest.php new file mode 100644 index 0000000..875bbea --- /dev/null +++ b/tests/Feature/Catalog/ProductVariantAttachmentTest.php @@ -0,0 +1,93 @@ + $hdrKey, + 'path' => 'tenants/' . $hdrKey . '.png', + 'filename' => 'logo_header.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + $footerAttachment = Attachment::create([ + 'key' => $ftrKey, + 'path' => 'tenants/' . $ftrKey . '.png', + 'filename' => 'logo_footer.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + + $tenant = Tenant::create([ + 'codigo' => 'acme', + 'nombre' => 'Acme Inc.', + 'dominio' => 'acme.com', + 'primary_color' => '#ffffff', + 'secondary_color' => '#ffffff', + 'danger_color' => '#ffffff', + 'header_footer_bg_color' => '#ffffff', + 'header_logo_id' => $headerAttachment->id, + 'footer_logo_id' => $footerAttachment->id, + ]); + + // 2. Create Product + $product = Product::create([ + 'tenant_codigo' => $tenant->codigo, + 'categoria_id' => 1, + 'slug' => 'test-product', + 'nombre' => 'Test Product', + 'descripcion' => 'A test product description', + 'precio' => 99.99, + ]); + + // 3. Create Variant + $variant = ProductVariant::create([ + 'producto_id' => $product->id, + 'slug' => 'test-variant-1', + 'nombre' => 'Test Variant 1', + 'stock' => 10, + 'precio' => 99.99, + ]); + + // 4. Create Attachments + $attachment1 = Attachment::create([ + 'key' => (string) Str::uuid(), + 'path' => 'attachments/image1.png', + 'filename' => 'image1.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + + $attachment2 = Attachment::create([ + 'key' => (string) Str::uuid(), + 'path' => 'attachments/image2.png', + 'filename' => 'image2.png', + 'type' => \App\Domains\Attachable\Enums\AttachmentType::Image, + 'mime_type' => 'image/png', + ]); + + // 5. Associate + $variant->attachments()->attach([$attachment1->id, $attachment2->id]); + + // 6. Assert relations + $this->assertCount(2, $variant->attachments); + $this->assertTrue($variant->attachments->contains($attachment1)); + $this->assertTrue($variant->attachments->contains($attachment2)); + } +}