feat(validity-time): add ValidityTimeResource and integrate into catalog and ticket resources
This commit is contained in:
@@ -7,6 +7,7 @@ use App\Domains\Catalog\Enums\ProductLayout;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
@@ -30,6 +31,8 @@ class CatalogFeaturedItemResource extends JsonResource
|
||||
'nombre' => $catalogItem->nombre,
|
||||
'descripcion' => $catalogItem->descripcion,
|
||||
'precio' => $catalogItem->precio,
|
||||
'validity_time_id' => $catalogItem->validity_time_id,
|
||||
'validity_time' => ValidityTimeResource::make($catalogItem->validityTime),
|
||||
'stock_tecnico' => $catalogItem->availableStock(),
|
||||
'variants' => $catalogItem->variants
|
||||
->map(fn (Variant $variant): array => [
|
||||
@@ -62,6 +65,8 @@ class CatalogFeaturedItemResource extends JsonResource
|
||||
'type' => $catalogItem->type->value,
|
||||
'nombre' => $catalogItem->nombre,
|
||||
'precio' => $catalogItem->precio,
|
||||
'validity_time_id' => $catalogItem->validity_time_id,
|
||||
'validity_time' => ValidityTimeResource::make($catalogItem->validityTime),
|
||||
'image' => $attachment?->getTemporaryUrl(1440),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\ItemAttribute;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
use Illuminate\Support\Collection;
|
||||
@@ -35,6 +36,7 @@ class CatalogItemDetailResource extends JsonResource
|
||||
'inventory_policy' => $this->inventory_policy?->value,
|
||||
'has_tickets' => $this->has_tickets,
|
||||
'validity_time_id' => $this->validity_time_id,
|
||||
'validity_time' => ValidityTimeResource::make($this->validityTime),
|
||||
'attributes' => $this->itemAttributes
|
||||
->map(fn (ItemAttribute $itemAttribute): array => $this->attributeData($itemAttribute))
|
||||
->values(),
|
||||
@@ -101,6 +103,7 @@ class CatalogItemDetailResource extends JsonResource
|
||||
'label' => $option->label,
|
||||
'sort_order' => $option->sort_order,
|
||||
'validity_time_id' => $option->validity_time_id,
|
||||
'validity_time' => ValidityTimeResource::make($option->validityTime),
|
||||
'metadata' => $option->metadata,
|
||||
])
|
||||
->values(),
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
namespace App\Domains\Catalog\Resources;
|
||||
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
@@ -26,6 +27,10 @@ class CatalogItemResource extends JsonResource
|
||||
'inventory_policy' => $this->inventory_policy?->value,
|
||||
'has_tickets' => $this->has_tickets,
|
||||
'validity_time_id' => $this->validity_time_id,
|
||||
'validity_time' => $this->whenLoaded(
|
||||
'validityTime',
|
||||
fn () => ValidityTimeResource::make($this->validityTime),
|
||||
),
|
||||
'real_stock' => $this->whenLoaded('inventory', fn () => $this->inventory?->real_stock),
|
||||
'images' => $this->whenLoaded('attachments', fn () => $this->attachments
|
||||
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
||||
|
||||
@@ -5,6 +5,7 @@ namespace App\Domains\Catalog\Resources;
|
||||
use App\Domains\Catalog\Enums\InventoryPolicy;
|
||||
use App\Domains\Catalog\Models\CatalogItem;
|
||||
use App\Domains\Catalog\Models\Variant;
|
||||
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
@@ -25,6 +26,8 @@ class CatalogSearchItemResource extends JsonResource
|
||||
'nombre' => $this->nombre,
|
||||
'descripcion' => $this->descripcion,
|
||||
'precio' => $this->precio,
|
||||
'validity_time_id' => $this->validity_time_id,
|
||||
'validity_time' => ValidityTimeResource::make($this->validityTime),
|
||||
'image' => $attachment?->getTemporaryUrl(1440),
|
||||
'stock_tecnico' => $this->availableStock(),
|
||||
'variants' => $this->variants
|
||||
|
||||
@@ -128,6 +128,7 @@ class CatalogService
|
||||
'category',
|
||||
'brand',
|
||||
'event',
|
||||
'validityTime',
|
||||
'itemAttributes.attribute',
|
||||
'variants.inventory',
|
||||
'variants.attachments',
|
||||
@@ -147,7 +148,8 @@ class CatalogService
|
||||
'category',
|
||||
'brand',
|
||||
'event',
|
||||
'itemAttributes.attribute.options',
|
||||
'validityTime',
|
||||
'itemAttributes.attribute.options.validityTime',
|
||||
'variants' => fn ($query) => $query->orderBy('id'),
|
||||
'variants.inventory',
|
||||
'variants.attachments',
|
||||
@@ -203,6 +205,7 @@ class CatalogService
|
||||
->with([
|
||||
'attachments',
|
||||
'inventory',
|
||||
'validityTime',
|
||||
'variants.inventory',
|
||||
'variants.attachments',
|
||||
'variants.eventDate',
|
||||
@@ -235,6 +238,7 @@ class CatalogService
|
||||
->with([
|
||||
'attachments',
|
||||
'inventory',
|
||||
'validityTime',
|
||||
'variants.inventory',
|
||||
'variants.attachments',
|
||||
'variants.eventDate',
|
||||
|
||||
@@ -40,6 +40,7 @@ class FeaturedGroupService
|
||||
->with([
|
||||
'inventory',
|
||||
'attachments',
|
||||
'validityTime',
|
||||
'variants.inventory',
|
||||
'variants.attachments',
|
||||
'variants.eventDate',
|
||||
|
||||
@@ -20,6 +20,8 @@ class TicketResource extends JsonResource
|
||||
'description' => $this->description,
|
||||
'source_catalog_item_id' => $this->source_catalog_item_id,
|
||||
'source_variant_id' => $this->source_variant_id,
|
||||
'validity_time_id' => $this->validity_time_id,
|
||||
'validity_time' => ValidityTimeResource::make($this->validityTime),
|
||||
'starts_at' => $this->getEffectiveStartsAt(),
|
||||
'expires_at' => $this->getEffectiveExpiresAt(),
|
||||
'used_at' => $this->used_at,
|
||||
|
||||
34
app/Domains/Ticket/Resources/ValidityTimeResource.php
Normal file
34
app/Domains/Ticket/Resources/ValidityTimeResource.php
Normal file
@@ -0,0 +1,34 @@
|
||||
<?php
|
||||
|
||||
namespace App\Domains\Ticket\Resources;
|
||||
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use App\Domains\Ticket\Models\ValidityTime;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Http\Resources\Json\JsonResource;
|
||||
|
||||
/** @mixin ValidityTime */
|
||||
class ValidityTimeResource extends JsonResource
|
||||
{
|
||||
/** @return array<string, mixed> */
|
||||
public function toArray(Request $request): array
|
||||
{
|
||||
$fields = match ($this->type) {
|
||||
ValidityTimeType::TimeWindow => [
|
||||
'start_time' => $this->start_time,
|
||||
'end_time' => $this->end_time,
|
||||
],
|
||||
ValidityTimeType::FixedWindow => [
|
||||
'fixed_starts_at' => $this->fixed_starts_at,
|
||||
'fixed_expires_at' => $this->fixed_expires_at,
|
||||
],
|
||||
};
|
||||
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'type' => $this->type->value,
|
||||
'is_valid' => $this->isValid(),
|
||||
...array_filter($fields, fn (mixed $value): bool => $value !== null),
|
||||
];
|
||||
}
|
||||
}
|
||||
55
tests/Unit/Ticket/ValidityTimeResourceTest.php
Normal file
55
tests/Unit/Ticket/ValidityTimeResourceTest.php
Normal file
@@ -0,0 +1,55 @@
|
||||
<?php
|
||||
|
||||
namespace Tests\Unit\Ticket;
|
||||
|
||||
use App\Domains\Ticket\Enums\ValidityTimeType;
|
||||
use App\Domains\Ticket\Models\ValidityTime;
|
||||
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||
use Illuminate\Support\Carbon;
|
||||
use Tests\TestCase;
|
||||
|
||||
class ValidityTimeResourceTest extends TestCase
|
||||
{
|
||||
protected function tearDown(): void
|
||||
{
|
||||
Carbon::setTestNow();
|
||||
|
||||
parent::tearDown();
|
||||
}
|
||||
|
||||
public function test_time_window_only_contains_non_null_time_fields(): void
|
||||
{
|
||||
Carbon::setTestNow('2026-08-20 12:00:00');
|
||||
$resource = ValidityTimeResource::make(new ValidityTime([
|
||||
'type' => ValidityTimeType::TimeWindow,
|
||||
'start_time' => '11:00:00',
|
||||
'end_time' => null,
|
||||
'fixed_starts_at' => '2026-08-20 10:00:00',
|
||||
]))->resolve();
|
||||
|
||||
$this->assertSame('time_window', $resource['type']);
|
||||
$this->assertTrue($resource['is_valid']);
|
||||
$this->assertSame('11:00:00', $resource['start_time']);
|
||||
$this->assertArrayNotHasKey('end_time', $resource);
|
||||
$this->assertArrayNotHasKey('fixed_starts_at', $resource);
|
||||
$this->assertArrayNotHasKey('fixed_expires_at', $resource);
|
||||
}
|
||||
|
||||
public function test_fixed_window_only_contains_non_null_datetime_fields(): void
|
||||
{
|
||||
Carbon::setTestNow('2026-08-20 11:00:00');
|
||||
$resource = ValidityTimeResource::make(new ValidityTime([
|
||||
'type' => ValidityTimeType::FixedWindow,
|
||||
'start_time' => '11:00:00',
|
||||
'fixed_starts_at' => '2026-08-20 10:00:00',
|
||||
'fixed_expires_at' => null,
|
||||
]))->resolve();
|
||||
|
||||
$this->assertSame('fixed_window', $resource['type']);
|
||||
$this->assertTrue($resource['is_valid']);
|
||||
$this->assertArrayHasKey('fixed_starts_at', $resource);
|
||||
$this->assertArrayNotHasKey('fixed_expires_at', $resource);
|
||||
$this->assertArrayNotHasKey('start_time', $resource);
|
||||
$this->assertArrayNotHasKey('end_time', $resource);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user