fix: resync room selections with travel data before step 2

This commit is contained in:
fromme committed 2026-09-29 10:59:13 +02:00
1 parent be0e15b3d4
commit d0e7ba017c
5 files changed
+708 -1

No files matched your search

+49
View File
@@ -15,6 +15,7 @@ use App\Form\Model\BookingDto;
use App\Form\Model\ParticipantDto;
use App\Form\Model\RoomSelectionDto;
use App\Form\Service\ServiceAgeEvaluator;
use App\Model\RoomSelectionAdjustment;
use Symfony\Component\DependencyInjection\Attribute\Autowire;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;
@@ -91,6 +92,54 @@ class BookingConfigurator
return $bookingCreateDto;
}
/**
* Brings the room selections back in line with the rooms the travel currently offers.
*
* Room selections are built once when the booking session starts, while the Travel is re-fetched
* from BusPro on every request. A room that sells out or leaves a bookable status in between drops
* out of getAvailableRooms() and leaves a selection behind that points at a room that no longer
* exists; one that only loses part of its contingent leaves a quantity nothing else re-checks
* before the booking is submitted; and one that becomes bookable again has no selection at all.
* Rebuilding the selections from the current rooms, with the chosen quantities carried over and
* capped at what is left, keeps both sides in step.
*
* @return list<RoomSelectionAdjustment> The selections that had to give up quantity, in selection order
*/
public function reconcileRoomSelections(BookingDto $bookingDto): array
{
$availableRooms = $bookingDto->travel->getAvailableRooms();
$adjustments = [];
$quantitiesByRoomId = [];
foreach ($bookingDto->roomSelections as $roomSelection) {
if (null === $roomSelection->id) {
continue;
}
$requestedQuantity = $roomSelection->quantity ?? 0;
$bookableQuantity = min($requestedQuantity, $availableRooms[$roomSelection->id]->available ?? 0);
$quantitiesByRoomId[$roomSelection->id] = $bookableQuantity;
if (0 < $requestedQuantity && $bookableQuantity < $requestedQuantity) {
$adjustments[] = new RoomSelectionAdjustment(
$roomSelection->id,
$roomSelection->label,
$requestedQuantity,
$bookableQuantity
);
}
}
$bookingDto->roomSelections = array_map(
fn (Room $room) => $this->createRoomSelection($room, $quantitiesByRoomId),
$availableRooms
);
return $adjustments;
}
/**
* Creates a room selection DTO from room data and quantities.
*