feat(variants): add minimum and maximum use dates to variants and update related logic
This commit is contained in:
@@ -32,11 +32,14 @@ class CatalogItemControllerTest extends TestCase
|
||||
'slug' => 'shirt',
|
||||
'nombre' => 'Shirt',
|
||||
'precio' => 100,
|
||||
'minimum_use_date' => '2026-08-01 09:00:00',
|
||||
'maximum_use_date' => '2026-08-31 18:00:00',
|
||||
'attribute_codes' => [$attribute->codigo],
|
||||
'images' => [$image, $image],
|
||||
'variants' => [
|
||||
[
|
||||
'real_stock' => 5,
|
||||
'maximum_use_date' => '2026-08-15 18:00:00',
|
||||
'values' => ['size' => 'M'],
|
||||
'images' => [$image],
|
||||
],
|
||||
@@ -48,7 +51,20 @@ class CatalogItemControllerTest extends TestCase
|
||||
->assertJsonPath('data.nombre', 'Shirt')
|
||||
->assertJsonCount(2, 'data.images')
|
||||
->assertJsonCount(1, 'data.variants')
|
||||
->assertJsonCount(1, 'data.variants.0.images');
|
||||
->assertJsonCount(1, 'data.variants.0.images')
|
||||
->assertJsonPath('data.variants.0.minimum_use_date', null)
|
||||
->assertJsonPath(
|
||||
'data.variants.0.maximum_use_date',
|
||||
fn (string $value): bool => str_starts_with($value, '2026-08-15T18:00:00'),
|
||||
)
|
||||
->assertJsonPath(
|
||||
'data.variants.0.effective_minimum_use_date',
|
||||
fn (string $value): bool => str_starts_with($value, '2026-08-01T09:00:00'),
|
||||
)
|
||||
->assertJsonPath(
|
||||
'data.variants.0.effective_maximum_use_date',
|
||||
fn (string $value): bool => str_starts_with($value, '2026-08-15T18:00:00'),
|
||||
);
|
||||
|
||||
$item = CatalogItem::query()->where('slug', 'shirt')->firstOrFail();
|
||||
$variant = $item->variants()->firstOrFail();
|
||||
|
||||
@@ -174,4 +174,12 @@ class CatalogSchemaTest extends TestCase
|
||||
'orden' => 3,
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_variants_can_override_catalog_item_use_dates(): void
|
||||
{
|
||||
$this->assertTrue(Schema::hasColumns('variantes', [
|
||||
'minimum_use_date',
|
||||
'maximum_use_date',
|
||||
]));
|
||||
}
|
||||
}
|
||||
|
||||
@@ -13,6 +13,7 @@ use App\Domains\Catalog\Services\CatalogService;
|
||||
use App\Domains\Shared\Enums\FieldType;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
use Illuminate\Foundation\Testing\RefreshDatabase;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
@@ -93,6 +94,73 @@ class CatalogServiceTest extends TestCase
|
||||
}
|
||||
}
|
||||
|
||||
public function test_variant_use_dates_override_or_inherit_catalog_item_dates(): void
|
||||
{
|
||||
$attribute = $this->createAttribute('day');
|
||||
$itemMinimum = Carbon::parse('2026-08-01 09:00:00');
|
||||
$itemMaximum = Carbon::parse('2026-08-31 18:00:00');
|
||||
$variantMaximum = Carbon::parse('2026-08-15 18:00:00');
|
||||
|
||||
$item = $this->service->create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'slug' => 'dated-variants',
|
||||
'nombre' => 'Dated variants',
|
||||
'precio' => 100,
|
||||
'minimum_use_date' => $itemMinimum,
|
||||
'maximum_use_date' => $itemMaximum,
|
||||
'attribute_codes' => [$attribute->codigo],
|
||||
'variants' => [
|
||||
[
|
||||
'real_stock' => 5,
|
||||
'maximum_use_date' => $variantMaximum,
|
||||
'values' => [$attribute->codigo => 'Saturday'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$variant = $item->variants->firstOrFail();
|
||||
|
||||
$this->assertNull($variant->minimum_use_date);
|
||||
$this->assertTrue($variant->maximum_use_date->equalTo($variantMaximum));
|
||||
$this->assertTrue($variant->getMinimumUseDate()->equalTo($itemMinimum));
|
||||
$this->assertTrue($variant->getMaximumUseDate()->equalTo($variantMaximum));
|
||||
}
|
||||
|
||||
public function test_it_rejects_an_invalid_effective_variant_use_date_range(): void
|
||||
{
|
||||
$attribute = $this->createAttribute('day');
|
||||
|
||||
try {
|
||||
$this->service->create([
|
||||
'tenant_code' => $this->tenant->codigo,
|
||||
'slug' => 'invalid-dated-variant',
|
||||
'nombre' => 'Invalid dated variant',
|
||||
'precio' => 100,
|
||||
'minimum_use_date' => '2026-08-10 09:00:00',
|
||||
'maximum_use_date' => '2026-08-31 18:00:00',
|
||||
'attribute_codes' => [$attribute->codigo],
|
||||
'variants' => [
|
||||
[
|
||||
'real_stock' => 5,
|
||||
'maximum_use_date' => '2026-08-09 18:00:00',
|
||||
'values' => [$attribute->codigo => 'Saturday'],
|
||||
],
|
||||
],
|
||||
]);
|
||||
|
||||
$this->fail('A validation exception was not thrown.');
|
||||
} catch (ValidationException $exception) {
|
||||
$this->assertArrayHasKey(
|
||||
'variants.0.maximum_use_date',
|
||||
$exception->errors(),
|
||||
);
|
||||
}
|
||||
|
||||
$this->assertDatabaseMissing('catalog_items', [
|
||||
'slug' => 'invalid-dated-variant',
|
||||
]);
|
||||
}
|
||||
|
||||
public function test_it_rejects_direct_inventory_together_with_variants(): void
|
||||
{
|
||||
$attribute = $this->createAttribute('size');
|
||||
|
||||
@@ -80,6 +80,39 @@ class FiestaFutbolInfantilProductSeederTest extends TestCase
|
||||
['2026-10-09', '2026-10-10', '2026-10-11', '2026-10-12'],
|
||||
$generalAdmission->variants->map(fn ($variant) => $variant->definitions->sole()->value)->all()
|
||||
);
|
||||
$this->assertSame(
|
||||
[
|
||||
['2026-10-09 00:00:00', '2026-10-09 23:59:59'],
|
||||
['2026-10-10 00:00:00', '2026-10-10 23:59:59'],
|
||||
['2026-10-11 00:00:00', '2026-10-11 23:59:59'],
|
||||
['2026-10-12 00:00:00', '2026-10-12 23:59:59'],
|
||||
],
|
||||
$generalAdmission->variants
|
||||
->sortBy(fn ($variant) => $variant->definitions->sole()->value)
|
||||
->map(fn ($variant): array => [
|
||||
$variant->minimum_use_date->format('Y-m-d H:i:s'),
|
||||
$variant->maximum_use_date->format('Y-m-d H:i:s'),
|
||||
])
|
||||
->values()
|
||||
->all()
|
||||
);
|
||||
|
||||
$standardItems = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
->where('type', CatalogItemType::Standard->value)
|
||||
->get();
|
||||
|
||||
$this->assertCount(7, $standardItems);
|
||||
foreach ($standardItems as $standardItem) {
|
||||
$this->assertSame(
|
||||
'2026-10-09 00:00:00',
|
||||
$standardItem->minimum_use_date->format('Y-m-d H:i:s'),
|
||||
);
|
||||
$this->assertSame(
|
||||
'2026-10-12 23:59:59',
|
||||
$standardItem->maximum_use_date->format('Y-m-d H:i:s'),
|
||||
);
|
||||
}
|
||||
|
||||
$allDaysItem = CatalogItem::query()
|
||||
->where('tenant_code', $tenant->codigo)
|
||||
|
||||
@@ -7,6 +7,7 @@ use App\Domains\Attachable\Models\Attachment;
|
||||
use App\Domains\Auth\Models\User;
|
||||
use App\Domains\Catalog\Enums\CatalogItemType;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Inventory;
|
||||
use App\Domains\Notification\Events\TicketsAvailable;
|
||||
use App\Domains\Purchase\Models\Purchase;
|
||||
use App\Domains\Tenant\Models\Tenant;
|
||||
@@ -93,6 +94,27 @@ class TicketGeneratorServiceTest extends TestCase
|
||||
$this->service->generate($item, $this->user);
|
||||
}
|
||||
|
||||
public function test_variant_dates_override_and_inherit_catalog_item_dates(): void
|
||||
{
|
||||
$item = $this->createTicketableItem(
|
||||
'variant-dates',
|
||||
now()->subDay(),
|
||||
now()->addMonth(),
|
||||
);
|
||||
$inventory = Inventory::query()->create();
|
||||
$variant = $item->variants()->create([
|
||||
'inventory_id' => $inventory->id,
|
||||
'maximum_use_date' => now()->addWeek(),
|
||||
]);
|
||||
|
||||
$ticket = $this->service
|
||||
->generate($item, $this->user, sourceVariantId: $variant->id)
|
||||
->firstOrFail();
|
||||
|
||||
$this->assertTrue($ticket->starts_at->equalTo($item->minimum_use_date));
|
||||
$this->assertTrue($ticket->expires_at->equalTo($variant->maximum_use_date));
|
||||
}
|
||||
|
||||
public function test_it_generates_tickets_for_every_bundle_component_and_quantity(): void
|
||||
{
|
||||
$first = $this->createTicketableItem('first', maximumUseDate: now()->addDay());
|
||||
@@ -111,6 +133,34 @@ class TicketGeneratorServiceTest extends TestCase
|
||||
$this->assertCount(2, $tickets->where('name', $second->nombre));
|
||||
}
|
||||
|
||||
public function test_bundle_component_uses_its_variant_dates(): void
|
||||
{
|
||||
$component = $this->createTicketableItem(
|
||||
'variant-component',
|
||||
now()->subDay(),
|
||||
now()->addMonth(),
|
||||
);
|
||||
$inventory = Inventory::query()->create();
|
||||
$variant = $component->variants()->create([
|
||||
'inventory_id' => $inventory->id,
|
||||
'minimum_use_date' => now()->addDay(),
|
||||
'maximum_use_date' => now()->addWeek(),
|
||||
]);
|
||||
$bundle = $this->createBundle('variant-bundle');
|
||||
$bundle->bundleComponents()->create([
|
||||
'component_catalog_item_id' => $component->id,
|
||||
'component_variant_id' => $variant->id,
|
||||
'quantity' => 1,
|
||||
]);
|
||||
|
||||
$ticket = $this->service
|
||||
->generate($bundle, $this->user)
|
||||
->firstOrFail();
|
||||
|
||||
$this->assertTrue($ticket->starts_at->equalTo($variant->minimum_use_date));
|
||||
$this->assertTrue($ticket->expires_at->equalTo($variant->maximum_use_date));
|
||||
}
|
||||
|
||||
public function test_bundle_generation_is_rolled_back_when_a_component_is_invalid(): void
|
||||
{
|
||||
$valid = $this->createTicketableItem('valid');
|
||||
@@ -152,13 +202,15 @@ class TicketGeneratorServiceTest extends TestCase
|
||||
public function test_a_ticket_generated_from_a_purchase_keeps_its_source_ids(): void
|
||||
{
|
||||
$item = $this->createTicketableItem('sourced-ticket');
|
||||
$purchase = $this->createPurchase($item, 1, 1234);
|
||||
$inventory = Inventory::query()->create();
|
||||
$variant = $item->variants()->create(['inventory_id' => $inventory->id]);
|
||||
$purchase = $this->createPurchase($item, 1, $variant->id);
|
||||
|
||||
$purchase->markAsPaid();
|
||||
|
||||
$this->assertDatabaseHas('tickets', [
|
||||
'source_catalog_item_id' => $item->id,
|
||||
'source_variant_id' => 1234,
|
||||
'source_variant_id' => $variant->id,
|
||||
]);
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user