feat: lock pooled rooms once the pool is used up

This commit is contained in:
fromme committed 2026-11-11 17:16:42 +01:00
1 parent a1645784fe
commit ac23baf625
5 files changed
+157 -11

No files matched your search

+35
View File
@@ -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([
+18
View File
@@ -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()))