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\CatalogItem;
|
||||||
use App\Domains\Catalog\Models\FeaturedGroup;
|
use App\Domains\Catalog\Models\FeaturedGroup;
|
||||||
use App\Domains\Catalog\Models\Variant;
|
use App\Domains\Catalog\Models\Variant;
|
||||||
|
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
@@ -30,6 +31,8 @@ class CatalogFeaturedItemResource extends JsonResource
|
|||||||
'nombre' => $catalogItem->nombre,
|
'nombre' => $catalogItem->nombre,
|
||||||
'descripcion' => $catalogItem->descripcion,
|
'descripcion' => $catalogItem->descripcion,
|
||||||
'precio' => $catalogItem->precio,
|
'precio' => $catalogItem->precio,
|
||||||
|
'validity_time_id' => $catalogItem->validity_time_id,
|
||||||
|
'validity_time' => ValidityTimeResource::make($catalogItem->validityTime),
|
||||||
'stock_tecnico' => $catalogItem->availableStock(),
|
'stock_tecnico' => $catalogItem->availableStock(),
|
||||||
'variants' => $catalogItem->variants
|
'variants' => $catalogItem->variants
|
||||||
->map(fn (Variant $variant): array => [
|
->map(fn (Variant $variant): array => [
|
||||||
@@ -62,6 +65,8 @@ class CatalogFeaturedItemResource extends JsonResource
|
|||||||
'type' => $catalogItem->type->value,
|
'type' => $catalogItem->type->value,
|
||||||
'nombre' => $catalogItem->nombre,
|
'nombre' => $catalogItem->nombre,
|
||||||
'precio' => $catalogItem->precio,
|
'precio' => $catalogItem->precio,
|
||||||
|
'validity_time_id' => $catalogItem->validity_time_id,
|
||||||
|
'validity_time' => ValidityTimeResource::make($catalogItem->validityTime),
|
||||||
'image' => $attachment?->getTemporaryUrl(1440),
|
'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\CatalogItem;
|
||||||
use App\Domains\Catalog\Models\ItemAttribute;
|
use App\Domains\Catalog\Models\ItemAttribute;
|
||||||
use App\Domains\Catalog\Models\Variant;
|
use App\Domains\Catalog\Models\Variant;
|
||||||
|
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
@@ -35,6 +36,7 @@ class CatalogItemDetailResource extends JsonResource
|
|||||||
'inventory_policy' => $this->inventory_policy?->value,
|
'inventory_policy' => $this->inventory_policy?->value,
|
||||||
'has_tickets' => $this->has_tickets,
|
'has_tickets' => $this->has_tickets,
|
||||||
'validity_time_id' => $this->validity_time_id,
|
'validity_time_id' => $this->validity_time_id,
|
||||||
|
'validity_time' => ValidityTimeResource::make($this->validityTime),
|
||||||
'attributes' => $this->itemAttributes
|
'attributes' => $this->itemAttributes
|
||||||
->map(fn (ItemAttribute $itemAttribute): array => $this->attributeData($itemAttribute))
|
->map(fn (ItemAttribute $itemAttribute): array => $this->attributeData($itemAttribute))
|
||||||
->values(),
|
->values(),
|
||||||
@@ -101,6 +103,7 @@ class CatalogItemDetailResource extends JsonResource
|
|||||||
'label' => $option->label,
|
'label' => $option->label,
|
||||||
'sort_order' => $option->sort_order,
|
'sort_order' => $option->sort_order,
|
||||||
'validity_time_id' => $option->validity_time_id,
|
'validity_time_id' => $option->validity_time_id,
|
||||||
|
'validity_time' => ValidityTimeResource::make($option->validityTime),
|
||||||
'metadata' => $option->metadata,
|
'metadata' => $option->metadata,
|
||||||
])
|
])
|
||||||
->values(),
|
->values(),
|
||||||
|
|||||||
@@ -3,6 +3,7 @@
|
|||||||
namespace App\Domains\Catalog\Resources;
|
namespace App\Domains\Catalog\Resources;
|
||||||
|
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
|
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
@@ -26,6 +27,10 @@ class CatalogItemResource extends JsonResource
|
|||||||
'inventory_policy' => $this->inventory_policy?->value,
|
'inventory_policy' => $this->inventory_policy?->value,
|
||||||
'has_tickets' => $this->has_tickets,
|
'has_tickets' => $this->has_tickets,
|
||||||
'validity_time_id' => $this->validity_time_id,
|
'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),
|
'real_stock' => $this->whenLoaded('inventory', fn () => $this->inventory?->real_stock),
|
||||||
'images' => $this->whenLoaded('attachments', fn () => $this->attachments
|
'images' => $this->whenLoaded('attachments', fn () => $this->attachments
|
||||||
->map(fn ($attachment) => $attachment->getTemporaryUrl(1440))
|
->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\Enums\InventoryPolicy;
|
||||||
use App\Domains\Catalog\Models\CatalogItem;
|
use App\Domains\Catalog\Models\CatalogItem;
|
||||||
use App\Domains\Catalog\Models\Variant;
|
use App\Domains\Catalog\Models\Variant;
|
||||||
|
use App\Domains\Ticket\Resources\ValidityTimeResource;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
|
||||||
@@ -25,6 +26,8 @@ class CatalogSearchItemResource extends JsonResource
|
|||||||
'nombre' => $this->nombre,
|
'nombre' => $this->nombre,
|
||||||
'descripcion' => $this->descripcion,
|
'descripcion' => $this->descripcion,
|
||||||
'precio' => $this->precio,
|
'precio' => $this->precio,
|
||||||
|
'validity_time_id' => $this->validity_time_id,
|
||||||
|
'validity_time' => ValidityTimeResource::make($this->validityTime),
|
||||||
'image' => $attachment?->getTemporaryUrl(1440),
|
'image' => $attachment?->getTemporaryUrl(1440),
|
||||||
'stock_tecnico' => $this->availableStock(),
|
'stock_tecnico' => $this->availableStock(),
|
||||||
'variants' => $this->variants
|
'variants' => $this->variants
|
||||||
|
|||||||
@@ -128,6 +128,7 @@ class CatalogService
|
|||||||
'category',
|
'category',
|
||||||
'brand',
|
'brand',
|
||||||
'event',
|
'event',
|
||||||
|
'validityTime',
|
||||||
'itemAttributes.attribute',
|
'itemAttributes.attribute',
|
||||||
'variants.inventory',
|
'variants.inventory',
|
||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
@@ -147,7 +148,8 @@ class CatalogService
|
|||||||
'category',
|
'category',
|
||||||
'brand',
|
'brand',
|
||||||
'event',
|
'event',
|
||||||
'itemAttributes.attribute.options',
|
'validityTime',
|
||||||
|
'itemAttributes.attribute.options.validityTime',
|
||||||
'variants' => fn ($query) => $query->orderBy('id'),
|
'variants' => fn ($query) => $query->orderBy('id'),
|
||||||
'variants.inventory',
|
'variants.inventory',
|
||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
@@ -203,6 +205,7 @@ class CatalogService
|
|||||||
->with([
|
->with([
|
||||||
'attachments',
|
'attachments',
|
||||||
'inventory',
|
'inventory',
|
||||||
|
'validityTime',
|
||||||
'variants.inventory',
|
'variants.inventory',
|
||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
@@ -235,6 +238,7 @@ class CatalogService
|
|||||||
->with([
|
->with([
|
||||||
'attachments',
|
'attachments',
|
||||||
'inventory',
|
'inventory',
|
||||||
|
'validityTime',
|
||||||
'variants.inventory',
|
'variants.inventory',
|
||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
|
|||||||
@@ -40,6 +40,7 @@ class FeaturedGroupService
|
|||||||
->with([
|
->with([
|
||||||
'inventory',
|
'inventory',
|
||||||
'attachments',
|
'attachments',
|
||||||
|
'validityTime',
|
||||||
'variants.inventory',
|
'variants.inventory',
|
||||||
'variants.attachments',
|
'variants.attachments',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
|
|||||||
@@ -20,6 +20,8 @@ class TicketResource extends JsonResource
|
|||||||
'description' => $this->description,
|
'description' => $this->description,
|
||||||
'source_catalog_item_id' => $this->source_catalog_item_id,
|
'source_catalog_item_id' => $this->source_catalog_item_id,
|
||||||
'source_variant_id' => $this->source_variant_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(),
|
'starts_at' => $this->getEffectiveStartsAt(),
|
||||||
'expires_at' => $this->getEffectiveExpiresAt(),
|
'expires_at' => $this->getEffectiveExpiresAt(),
|
||||||
'used_at' => $this->used_at,
|
'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