feat: add is_default field to product variants and implement default variant management logic in ProductService

This commit is contained in:
2026-07-01 10:02:26 -03:00
parent 83f42f3cf9
commit d6a17cb449
4 changed files with 47 additions and 6 deletions

View File

@@ -635,6 +635,7 @@ class ProductControllerTest extends TestCase
$variant = $product->variants()->first();
$this->assertEquals(15, $variant->stock);
$this->assertTrue($variant->is_default);
// Should have no definitions
$this->assertEquals(0, $variant->definitions()->count());
}
@@ -653,6 +654,7 @@ class ProductControllerTest extends TestCase
$defaultVariant = $product->variants()->create([
'stock' => 10,
'is_default' => true,
]);
$this->assertEquals(1, $product->variants()->count());
@@ -678,7 +680,9 @@ class ProductControllerTest extends TestCase
// Only the new variant should remain
$this->assertEquals(1, $product->variants()->count());
$this->assertEquals(5, $product->variants()->first()->stock);
$newVariant = $product->variants()->first();
$this->assertEquals(5, $newVariant->stock);
$this->assertFalse($newVariant->is_default);
}
public function test_it_restores_default_variant_when_all_variants_are_deleted(): void
@@ -695,6 +699,7 @@ class ProductControllerTest extends TestCase
$realVariant = $product->createVariant([
'stock' => 5,
'is_default' => false,
'definitions' => [
[
'products_attribute_id' => $productAttributes['size']->id,
@@ -709,10 +714,11 @@ class ProductControllerTest extends TestCase
$response = $this->deleteJson("/api/tenants/{$this->tenant->codigo}/productos/{$product->id}/variants/{$realVariant->id}");
$response->assertNoContent();
// A default variant should be recreated with stock 0
// A default variant should be recreated with stock 0 and is_default = true
$this->assertEquals(1, $product->variants()->count());
$defaultVariant = $product->variants()->first();
$this->assertEquals(0, $defaultVariant->stock);
$this->assertTrue($defaultVariant->is_default);
$this->assertEquals(0, $defaultVariant->definitions()->count());
}