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 App\Domains\Catalog\Models\CatalogItem;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Http\Resources\Json\JsonResource;
|
use Illuminate\Http\Resources\Json\JsonResource;
|
||||||
|
use Illuminate\Validation\ValidationException;
|
||||||
|
|
||||||
/** @mixin CatalogItem */
|
/** @mixin CatalogItem */
|
||||||
class EntryResource extends JsonResource
|
class EntryResource extends JsonResource
|
||||||
@@ -12,14 +13,27 @@ class EntryResource extends JsonResource
|
|||||||
/** @return array<string, mixed> */
|
/** @return array<string, mixed> */
|
||||||
public function toArray(Request $request): array
|
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 [
|
return [
|
||||||
'id' => $this->id,
|
'id' => $this->id,
|
||||||
'title' => $this->nombre,
|
'title' => $this->nombre,
|
||||||
'description' => $this->descripcion,
|
'description' => $this->descripcion,
|
||||||
'variants' => $this->variants
|
'event_date_ids' => $variant->selectedEventDates()->pluck('id')->values(),
|
||||||
->whereNull('replaced_by_variant_id')
|
'stock' => $variant->inventory->real_stock,
|
||||||
->values()
|
|
||||||
->toArray(),
|
|
||||||
'price' => $this->precio,
|
'price' => $this->precio,
|
||||||
];
|
];
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,6 +25,7 @@ class EntryService
|
|||||||
->where('tenant_code', $tenant->codigo)
|
->where('tenant_code', $tenant->codigo)
|
||||||
->whereHas('category', fn ($query) => $query->where('nombre', 'Entradas'))
|
->whereHas('category', fn ($query) => $query->where('nombre', 'Entradas'))
|
||||||
->with([
|
->with([
|
||||||
|
'variants' => fn ($query) => $query->whereNull('replaced_by_variant_id'),
|
||||||
'variants.inventory',
|
'variants.inventory',
|
||||||
'variants.eventDate',
|
'variants.eventDate',
|
||||||
'variants.eventDates',
|
'variants.eventDates',
|
||||||
|
|||||||
@@ -8,7 +8,7 @@ use App\Domains\Catalog\Models\Variant;
|
|||||||
use App\Domains\Event\Models\EventDate;
|
use App\Domains\Event\Models\EventDate;
|
||||||
use App\Domains\FiestaFutbolInfantil\Resources\EntryResource;
|
use App\Domains\FiestaFutbolInfantil\Resources\EntryResource;
|
||||||
use Illuminate\Http\Request;
|
use Illuminate\Http\Request;
|
||||||
use Illuminate\Support\MultipleItemsFoundException;
|
use Illuminate\Validation\ValidationException;
|
||||||
use Tests\TestCase;
|
use Tests\TestCase;
|
||||||
|
|
||||||
class EntryResourceTest extends TestCase
|
class EntryResourceTest extends TestCase
|
||||||
@@ -38,7 +38,7 @@ class EntryResourceTest extends TestCase
|
|||||||
$entry = new CatalogItem;
|
$entry = new CatalogItem;
|
||||||
$entry->setRelation('variants', collect([new Variant, new Variant]));
|
$entry->setRelation('variants', collect([new Variant, new Variant]));
|
||||||
|
|
||||||
$this->expectException(MultipleItemsFoundException::class);
|
$this->expectException(ValidationException::class);
|
||||||
|
|
||||||
(new EntryResource($entry))->resolve(Request::create('/'));
|
(new EntryResource($entry))->resolve(Request::create('/'));
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user