feat: add is_default field to product variants and implement default variant management logic in ProductService
This commit is contained in:
@@ -13,6 +13,7 @@ use Illuminate\Database\Eloquent\Relations\HasMany;
|
||||
#[Fillable([
|
||||
'producto_id',
|
||||
'stock',
|
||||
'is_default',
|
||||
])]
|
||||
class ProductVariant extends Model
|
||||
{
|
||||
@@ -25,6 +26,7 @@ class ProductVariant extends Model
|
||||
return [
|
||||
'producto_id' => 'integer',
|
||||
'stock' => 'integer',
|
||||
'is_default' => 'boolean',
|
||||
];
|
||||
}
|
||||
|
||||
|
||||
@@ -44,6 +44,7 @@ class ProductService
|
||||
// Create default variant with stock
|
||||
$this->createVariant($product, [
|
||||
'stock' => $stock,
|
||||
'is_default' => true,
|
||||
'definitions' => [],
|
||||
]);
|
||||
|
||||
@@ -116,11 +117,14 @@ class ProductService
|
||||
$images = $data['images'] ?? [];
|
||||
unset($data['images']);
|
||||
|
||||
// If the variant being created has definitions, it is a real variant.
|
||||
// Remove any default variants (those without definitions).
|
||||
// Determine if the variant being created is a default one
|
||||
$hasDefinitions = ! empty($data['definitions']);
|
||||
if ($hasDefinitions) {
|
||||
$defaultVariants = $product->variants()->whereDoesntHave('definitions')->get();
|
||||
$isDefault = $data['is_default'] ?? (! $hasDefinitions);
|
||||
$data['is_default'] = $isDefault;
|
||||
|
||||
// If the variant being created is not a default variant, remove any existing default variants
|
||||
if (! $isDefault) {
|
||||
$defaultVariants = $product->variants()->where('is_default', true)->get();
|
||||
foreach ($defaultVariants as $defaultVariant) {
|
||||
$this->deleteVariantAttachments($defaultVariant);
|
||||
$product->deleteVariant($defaultVariant);
|
||||
@@ -176,6 +180,7 @@ class ProductService
|
||||
if ($product->variants()->count() === 0) {
|
||||
$product->createVariant([
|
||||
'stock' => 0,
|
||||
'is_default' => true,
|
||||
'definitions' => [],
|
||||
]);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,28 @@
|
||||
<?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::table('productos_variantes', function (Blueprint $table) {
|
||||
$table->boolean('is_default')->default(false)->after('stock');
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Reverse the migrations.
|
||||
*/
|
||||
public function down(): void
|
||||
{
|
||||
Schema::table('productos_variantes', function (Blueprint $table) {
|
||||
$table->dropColumn('is_default');
|
||||
});
|
||||
}
|
||||
};
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user