feat: lock pooled rooms once the pool is used up
This commit is contained in:
1 parent
a1645784fe
commit
ac23baf625
5 files changed
+157
-11
No files matched your search
@@ -3,14 +3,22 @@
|
||||
namespace App\Form;
|
||||
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Service\RoomContingentPoolResolver;
|
||||
use Symfony\Component\Form\AbstractType;
|
||||
use Symfony\Component\Form\Extension\Core\Type\CollectionType;
|
||||
use Symfony\Component\Form\FormBuilderInterface;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
|
||||
/** @extends AbstractType<BookingDto> */
|
||||
class BookingCreateStep1Type extends AbstractType
|
||||
{
|
||||
public function __construct(
|
||||
private readonly RoomContingentPoolResolver $poolResolver,
|
||||
) {
|
||||
}
|
||||
|
||||
public function buildForm(FormBuilderInterface $builder, array $options): void
|
||||
{
|
||||
$builder
|
||||
@@ -28,6 +36,33 @@ class BookingCreateStep1Type extends AbstractType
|
||||
;
|
||||
}
|
||||
|
||||
/**
|
||||
* Caps the step buttons of rooms sharing a contingent at what the other rooms leave of it.
|
||||
*
|
||||
* Runs on every render, including the htmx refresh after each change, so selecting one room
|
||||
* of a pool locks the others once the pool is used up. Only the view changes: the choices
|
||||
* keep the room's own count, and RoomSelectionValidator stays the check on submit.
|
||||
*/
|
||||
public function finishView(FormView $view, FormInterface $form, array $options): void
|
||||
{
|
||||
$bookingDto = $form->getData();
|
||||
$selectionViews = $view['roomSelections'];
|
||||
|
||||
foreach ($this->poolResolver->findSharedPools($bookingDto->travel) as $pool) {
|
||||
$remaining = max(0, $pool->getAvailable() - $pool->getSelectedQuantity($bookingDto->roomSelections));
|
||||
|
||||
foreach (array_keys($pool->rooms) as $roomId) {
|
||||
if (!isset($selectionViews[$roomId]) || !isset($selectionViews[$roomId]['quantity'])) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$quantityView = $selectionViews[$roomId]['quantity'];
|
||||
$quantity = $bookingDto->roomSelections[$roomId]->quantity ?? 0;
|
||||
$quantityView->vars['max_value'] = min($quantityView->vars['max_value'], $quantity + $remaining);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public function configureOptions(OptionsResolver $resolver): void
|
||||
{
|
||||
$resolver->setDefaults([
|
||||
|
||||
@@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||
namespace App\Model;
|
||||
|
||||
use App\BusProNet\Model\Room;
|
||||
use App\Form\Model\RoomSelectionDto;
|
||||
|
||||
/**
|
||||
* Offered rooms of one travel that draw on the same BusPro contingent.
|
||||
@@ -33,6 +34,23 @@ final readonly class RoomContingentPool
|
||||
return min(array_map(static fn (Room $room): int => $room->available ?? 0, $this->rooms));
|
||||
}
|
||||
|
||||
/**
|
||||
* Rooms the given selections take from the pool.
|
||||
*
|
||||
* @param iterable<RoomSelectionDto> $roomSelections
|
||||
*/
|
||||
public function getSelectedQuantity(iterable $roomSelections): int
|
||||
{
|
||||
$quantity = 0;
|
||||
foreach ($roomSelections as $roomSelection) {
|
||||
if (null !== $roomSelection->id && $this->contains($roomSelection->id)) {
|
||||
$quantity += $roomSelection->quantity ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
return $quantity;
|
||||
}
|
||||
|
||||
public function contains(int $roomId): bool
|
||||
{
|
||||
return isset($this->rooms[$roomId]);
|
||||
|
||||
@@ -57,17 +57,8 @@ class RoomSelectionValidator extends ConstraintValidator
|
||||
*/
|
||||
private function assertSharedContingentsNotExceeded(BookingDto $bookingDto, RoomSelection $constraint): void
|
||||
{
|
||||
$selectedRooms = $bookingDto->getSelectedRooms();
|
||||
|
||||
foreach ($this->poolResolver->findSharedPools($bookingDto->travel) as $pool) {
|
||||
$selectedQuantity = 0;
|
||||
foreach ($selectedRooms as $roomSelection) {
|
||||
if (null !== $roomSelection->id && $pool->contains($roomSelection->id)) {
|
||||
$selectedQuantity += $roomSelection->quantity ?? 0;
|
||||
}
|
||||
}
|
||||
|
||||
if ($selectedQuantity > $pool->getAvailable()) {
|
||||
if ($pool->getSelectedQuantity($bookingDto->getSelectedRooms()) > $pool->getAvailable()) {
|
||||
$this->context->buildViolation($constraint->sharedContingentExceededMessage)
|
||||
->setParameter('{{ available }}', (string) $pool->getAvailable())
|
||||
->setParameter('{{ rooms }}', implode(', ', $pool->getLabels()))
|
||||
|
||||
@@ -3,7 +3,8 @@
|
||||
{% set htmx_change_trigger = 'change delay:300ms' %}
|
||||
|
||||
{% macro stepFormField(form, roomId, pool = null) %}
|
||||
<div class="grid grid-cols-3 pb-2">
|
||||
{# A room whose shared contingent the other selected rooms have used up #}
|
||||
<div class="grid grid-cols-3 pb-2{{ pool is not null and form.quantity.vars.max_value == 0 ? ' opacity-50' }}">
|
||||
<div class="col-span-2 pr-2">
|
||||
<div class="font-semibold">
|
||||
{{ form.vars.label_room }}
|
||||
|
||||
@@ -0,0 +1,101 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Tests\Form;
|
||||
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Room;
|
||||
use App\BusProNet\Model\Travel;
|
||||
use App\Form\BookingCreateStep1Type;
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\RoomSelectionDto;
|
||||
use App\Model\RoomContingentPool;
|
||||
use App\Service\RoomContingentPoolResolver;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Symfony\Component\Form\Forms;
|
||||
use Symfony\Component\Form\FormView;
|
||||
use Symfony\Component\Form\PreloadedExtension;
|
||||
|
||||
class BookingCreateStep1TypeTest extends TestCase
|
||||
{
|
||||
public function testSelectingOneRoomOfAnExhaustedPoolLocksTheOthers(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 1, quantities: [2366 => 1, 2367 => 0, 27 => 0]);
|
||||
|
||||
self::assertSame(1, $this->maxValue($view, 2366), 'the selected room keeps its quantity');
|
||||
self::assertSame(0, $this->maxValue($view, 2367));
|
||||
self::assertSame(3, $this->maxValue($view, 27), 'a room outside the pool is untouched');
|
||||
}
|
||||
|
||||
public function testRoomsOfAPoolWithRoomLeftShareWhatRemains(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 2, quantities: [2366 => 1, 2367 => 0, 27 => 0]);
|
||||
|
||||
self::assertSame(2, $this->maxValue($view, 2366));
|
||||
self::assertSame(1, $this->maxValue($view, 2367));
|
||||
}
|
||||
|
||||
public function testNothingSelectedLeavesEachRoomAtThePoolCount(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 1, quantities: [2366 => 0, 2367 => 0, 27 => 0]);
|
||||
|
||||
self::assertSame(1, $this->maxValue($view, 2366));
|
||||
self::assertSame(1, $this->maxValue($view, 2367));
|
||||
}
|
||||
|
||||
public function testAnOverSelectedPoolNeverCapsBelowTheSelection(): void
|
||||
{
|
||||
$view = $this->createView(poolAvailable: 1, quantities: [2366 => 1, 2367 => 1, 27 => 0]);
|
||||
|
||||
self::assertSame(1, $this->maxValue($view, 2366));
|
||||
self::assertSame(1, $this->maxValue($view, 2367));
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $quantities
|
||||
*/
|
||||
private function createView(int $poolAvailable, array $quantities): FormView
|
||||
{
|
||||
$eight = $this->createRoom(2366, $poolAvailable);
|
||||
$seven = $this->createRoom(2367, $poolAvailable);
|
||||
$double = $this->createRoom(27, 3);
|
||||
|
||||
$travel = new Travel();
|
||||
$travel->rooms = [$eight, $seven, $double];
|
||||
|
||||
$bookingDto = new BookingDto($travel, 163113);
|
||||
foreach ([$eight, $seven, $double] as $room) {
|
||||
$selection = new RoomSelectionDto();
|
||||
$selection->id = $room->id;
|
||||
$selection->maxQuantity = (int) $room->available;
|
||||
$selection->quantity = $quantities[$room->id];
|
||||
$bookingDto->roomSelections[$room->id] = $selection;
|
||||
}
|
||||
|
||||
$poolResolver = $this->createStub(RoomContingentPoolResolver::class);
|
||||
$poolResolver->method('findSharedPools')->willReturn([new RoomContingentPool([2366 => $eight, 2367 => $seven])]);
|
||||
|
||||
return Forms::createFormFactoryBuilder()
|
||||
->addExtension(new PreloadedExtension([new BookingCreateStep1Type($poolResolver)], []))
|
||||
->getFormFactory()
|
||||
->create(BookingCreateStep1Type::class, $bookingDto)
|
||||
->createView();
|
||||
}
|
||||
|
||||
private function maxValue(FormView $view, int $roomId): int
|
||||
{
|
||||
return $view['roomSelections'][$roomId]['quantity']->vars['max_value'];
|
||||
}
|
||||
|
||||
private function createRoom(int $id, int $available): Room
|
||||
{
|
||||
$room = new Room();
|
||||
$room->id = $id;
|
||||
$room->label = 'Zimmer '.$id;
|
||||
$room->available = $available;
|
||||
$room->status = Constants::STATUS_AVAILABLE;
|
||||
|
||||
return $room;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user