41 lines
1.2 KiB
PHP
41 lines
1.2 KiB
PHP
<?php
|
|
|
|
namespace App\Domains\FiestaFutbolInfantil\Resources;
|
|
|
|
use App\Domains\Catalog\Models\CatalogItem;
|
|
use Illuminate\Http\Request;
|
|
use Illuminate\Http\Resources\Json\JsonResource;
|
|
use Illuminate\Validation\ValidationException;
|
|
|
|
/** @mixin CatalogItem */
|
|
class EntryResource extends JsonResource
|
|
{
|
|
/** @return array<string, mixed> */
|
|
public function toArray(Request $request): array
|
|
{
|
|
$variants = $this->variants->whereNull('replaced_by_variant_id');
|
|
|
|
if ($variants->count() !== 1) {
|
|
throw ValidationException::withMessages([
|
|
'entries' => [sprintf(
|
|
'La entrada %s tiene %d variantes sin reemplazar (IDs: %s). Se esperaba una.',
|
|
$this->id,
|
|
$variants->count(),
|
|
$variants->pluck('id')->implode(', '),
|
|
)],
|
|
]);
|
|
}
|
|
|
|
$variant = $variants->first();
|
|
|
|
return [
|
|
'id' => $this->id,
|
|
'title' => $this->nombre,
|
|
'description' => $this->descripcion,
|
|
'event_date_ids' => $variant->selectedEventDates()->pluck('id')->values(),
|
|
'stock' => $variant->inventory->real_stock,
|
|
'price' => $this->precio,
|
|
];
|
|
}
|
|
}
|