feat(entry): add validation for single non-replaced variant in EntryResource and update tests
This commit is contained in:
parent
e6b5da366c
commit
708037677a
|
|
@ -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,
|
||||
];
|
||||
}
|
||||
|
|
|
|||
|
|
@ -25,6 +25,7 @@ class EntryService
|
|||
->where('tenant_code', $tenant->codigo)
|
||||
->whereHas('category', fn ($query) => $query->where('nombre', 'Entradas'))
|
||||
->with([
|
||||
'variants' => fn ($query) => $query->whereNull('replaced_by_variant_id'),
|
||||
'variants.inventory',
|
||||
'variants.eventDate',
|
||||
'variants.eventDates',
|
||||
|
|
|
|||
|
|
@ -8,7 +8,7 @@ use App\Domains\Catalog\Models\Variant;
|
|||
use App\Domains\Event\Models\EventDate;
|
||||
use App\Domains\FiestaFutbolInfantil\Resources\EntryResource;
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\MultipleItemsFoundException;
|
||||
use Illuminate\Validation\ValidationException;
|
||||
use Tests\TestCase;
|
||||
|
||||
class EntryResourceTest extends TestCase
|
||||
|
|
@ -38,7 +38,7 @@ class EntryResourceTest extends TestCase
|
|||
$entry = new CatalogItem;
|
||||
$entry->setRelation('variants', collect([new Variant, new Variant]));
|
||||
|
||||
$this->expectException(MultipleItemsFoundException::class);
|
||||
$this->expectException(ValidationException::class);
|
||||
|
||||
(new EntryResource($entry))->resolve(Request::create('/'));
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in New Issue