3 Commits

13 changed files with 227 additions and 2 deletions

View File

@@ -9,6 +9,7 @@ use Illuminate\Database\Eloquent\Relations\BelongsTo;
#[Fillable([
'attribute_id',
'value',
'label',
'sort_order',
'metadata',

View File

@@ -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<Attachment, $this>
*/
public function attachments(): BelongsToMany
{
return $this->belongsToMany(
Attachment::class,
'variantes_attachments',
'variante_id',
'attachment_id'
)->withTimestamps();
}
}

View File

@@ -43,6 +43,7 @@ class StoreProductAttributeRequest extends FormRequest
], true)),
'array',
],
'options.*.value' => ['required', 'string', 'max:255'],
'options.*.label' => ['required', 'string', 'max:255'],
'options.*.sort_order' => ['sometimes', 'integer'],
'options.*.metadata' => ['nullable', 'array'],

View File

@@ -37,6 +37,7 @@ class UpdateProductAttributeRequest extends FormRequest
'metadata_schema' => ['nullable', 'array'],
'type' => ['required', Rule::enum(FieldType::class)],
'options' => ['sometimes', 'array'],
'options.*.value' => ['required', 'string', 'max:255'],
'options.*.label' => ['required', 'string', 'max:255'],
'options.*.sort_order' => ['sometimes', 'integer'],
'options.*.metadata' => ['nullable', 'array'],

View File

@@ -17,6 +17,7 @@ class ProductAttributeOptionResource extends JsonResource
{
return [
'id' => $this->id,
'value' => $this->value,
'label' => $this->label,
'sort_order' => $this->sort_order,
'metadata' => $this->metadata,

View File

@@ -14,6 +14,7 @@ return new class extends Migration
Schema::create('attribute_options', function (Blueprint $table) {
$table->id();
$table->foreignId('attribute_id')->constrained('productos_attributes')->cascadeOnUpdate()->cascadeOnDelete();
$table->string('value');
$table->string('label');
$table->unsignedInteger('sort_order')->default(0);
$table->json('metadata')->nullable();

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration
{
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('variantes_attachments', function (Blueprint $table) {
$table->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');
}
};

View File

@@ -0,0 +1,40 @@
<?php
namespace Database\Seeders;
use App\Domains\Catalog\Models\Brand;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Database\Seeder;
class BrandSeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$brands = [
'Le Coq',
'Adidas',
'Under Armour',
'Rosario Central',
'Topper',
'Nike',
];
$tenants = Tenant::all();
if ($tenants->isEmpty()) {
return;
}
foreach ($tenants as $tenant) {
foreach ($brands as $nombre) {
Brand::firstOrCreate([
'tenant_codigo' => $tenant->codigo,
'nombre' => $nombre,
]);
}
}
}
}

View File

@@ -0,0 +1,29 @@
<?php
namespace Database\Seeders;
use App\Domains\Catalog\Models\Category;
use Illuminate\Database\Seeder;
class CategorySeeder extends Seeder
{
/**
* Run the database seeds.
*/
public function run(): void
{
$categories = [
'Remeras',
'Pantalones',
'Zapatillas',
'Buzos'
];
foreach ($categories as $nombre) {
Category::firstOrCreate([
'nombre' => $nombre,
'tenant_code' => null,
]);
}
}
}

View File

@@ -22,6 +22,10 @@ class DatabaseSeeder extends Seeder
'email' => 'test@example.com',
]);
$this->call(TenantSeeder::class);
$this->call([
TenantSeeder::class,
CategorySeeder::class,
BrandSeeder::class,
]);
}
}

View File

@@ -37,7 +37,7 @@ class CartControllerTest extends TestCase
'tenant_codigo' => 'acme',
'codigo' => 'color',
'nombre' => 'Color',
'type' => 'text',
'type' => 'string',
]);
ProductVariantDefinition::query()->create([

View File

@@ -24,11 +24,13 @@ class ProductAttributeControllerTest extends TestCase
],
'options' => [
[
'value' => 'red',
'label' => 'Red',
'sort_order' => 1,
'metadata' => ['hex' => '#ff0000'],
],
[
'value' => 'blue',
'label' => 'Blue',
'sort_order' => 2,
'metadata' => ['hex' => '#0000ff'],
@@ -41,6 +43,7 @@ class ProductAttributeControllerTest extends TestCase
->assertJsonPath('data.tenant_codigo', 'acme')
->assertJsonPath('data.codigo', 'color')
->assertJsonPath('data.type', 'select')
->assertJsonPath('data.options.0.value', 'red')
->assertJsonPath('data.options.0.label', 'Red')
->assertJsonPath('data.options.1.metadata.hex', '#0000ff');
@@ -51,6 +54,7 @@ class ProductAttributeControllerTest extends TestCase
]);
$this->assertDatabaseHas('attribute_options', [
'value' => 'red',
'label' => 'Red',
'sort_order' => 1,
]);

View File

@@ -0,0 +1,93 @@
<?php
namespace Tests\Feature\Catalog;
use App\Domains\Attachable\Models\Attachment;
use App\Domains\Catalog\Models\Product;
use App\Domains\Catalog\Models\ProductVariant;
use App\Domains\Tenant\Models\Tenant;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Str;
use Tests\TestCase;
class ProductVariantAttachmentTest extends TestCase
{
use RefreshDatabase;
public function test_it_can_associate_attachments_to_product_variants(): void
{
// 1. Create Tenant
$hdrKey = (string) Str::uuid();
$ftrKey = (string) Str::uuid();
$headerAttachment = Attachment::create([
'key' => $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));
}
}