feat(event): include catalog items in event resource and update event service test(seeder): add FiestaTradicionArrufoSeeder for event and catalog item setup config: set event timezone for local event dates
71 lines
3.3 KiB
PHP
71 lines
3.3 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\Ticketing\Event\Resources;
|
|
|
|
use App\Domains\Commerce\Catalog\Enums\InventoryPolicy;
|
|
use App\Domains\Commerce\Catalog\Models\CatalogItem;
|
|
use App\Domains\Commerce\Catalog\Models\Variant;
|
|
use Illuminate\Support\Carbon;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
|
|
class PublicEventResource extends JsonResource
|
|
{
|
|
public function toArray(Request $request): array
|
|
{
|
|
return [
|
|
'id' => $this->id,
|
|
'title' => $this->title,
|
|
'subtitle' => $this->subtitle,
|
|
'description' => $this->description,
|
|
'location' => $this->location,
|
|
'exact_location' => $this->exact_location,
|
|
'date_text' => $this->date_text,
|
|
'start_time' => $this->whenLoaded('dates', fn () => $this->dates->first()?->time_start),
|
|
'starts_at' => $this->whenLoaded('dates', function (): ?string {
|
|
$first = $this->dates->first();
|
|
if ($first === null) {
|
|
return null;
|
|
}
|
|
|
|
return Carbon::parse(
|
|
$first->date->format('Y-m-d').' '.$first->time_start,
|
|
config('app.event_timezone'),
|
|
)->toISOString();
|
|
}),
|
|
'dates' => $this->whenLoaded('dates', fn () => $this->dates->map(fn ($date): array => [
|
|
'id' => $date->id,
|
|
'date' => $date->date->format('Y-m-d'),
|
|
'time_start' => $date->time_start,
|
|
])->values()),
|
|
'social_media' => $this->whenLoaded('socialMedia', fn () => $this->socialMedia->map(fn ($social): array => [
|
|
'code' => $social->code,
|
|
'url' => $social->pivot->url,
|
|
])->values()),
|
|
'attachment_id' => $this->attachment_id,
|
|
'image' => $this->attachment?->getTemporaryUrl(1440),
|
|
'catalog_items' => $this->whenLoaded('catalogItems', fn () => $this->catalogItems
|
|
->map(fn (CatalogItem $item): array => [
|
|
'id' => $item->id,
|
|
'name' => $item->nombre,
|
|
'description' => $item->descripcion,
|
|
'price' => $item->precio,
|
|
'image' => $item->attachments->first()?->getTemporaryUrl(1440),
|
|
'requires_selection' => $item->itemAttributes->contains(fn ($attribute): bool =>
|
|
$attribute->show_in_selector && $attribute->attribute?->codigo !== 'event_date'),
|
|
'maximum_quantity' => $item->inventory_policy === InventoryPolicy::Unlimited
|
|
? null
|
|
: $item->availableStock(),
|
|
'variants' => $item->visibleVariants()->map(fn (Variant $variant): array => [
|
|
'id' => $variant->id,
|
|
'price' => number_format($variant->getPrice(), 2, '.', ''),
|
|
'event_date_ids' => $variant->selectedEventDates()->pluck('id')->values(),
|
|
'maximum_quantity' => $item->inventory_policy === InventoryPolicy::Unlimited
|
|
? null
|
|
: $variant->inventory?->availableStock(),
|
|
])->values(),
|
|
])->values()),
|
|
];
|
|
}
|
|
}
|