feat(entry): add validation for single non-replaced variant in EntryResource and update tests
This commit is contained in:
@@ -5,6 +5,7 @@ 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
|
||||
@@ -12,14 +13,27 @@ 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,
|
||||
'variants' => $this->variants
|
||||
->whereNull('replaced_by_variant_id')
|
||||
->values()
|
||||
->toArray(),
|
||||
'event_date_ids' => $variant->selectedEventDates()->pluck('id')->values(),
|
||||
'stock' => $variant->inventory->real_stock,
|
||||
'price' => $this->precio,
|
||||
];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user