feat: auto-assign rooms

This commit is contained in:
Björn Fromme
2025-10-02 12:25:42 +02:00
parent d7929dd855
commit 0f3d9b87fa
2 changed files with 87 additions and 0 deletions
@@ -10,6 +10,7 @@ use App\Form\Model\BookingCreateDto;
use App\Form\Model\ParticipantDto;
use App\Service\BookingPriceCalculatorService;
use App\Service\BookingService;
use App\Service\RoomAssignmentService;
use App\Service\TravelDataService;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Request;
@@ -32,6 +33,7 @@ class CreateStep2Controller extends AbstractController
private readonly BookingService $bookingService,
private readonly BookingPriceCalculatorService $priceCalculator,
private readonly TravelDataService $travelDataService,
private readonly RoomAssignmentService $roomAssignmentService,
) {
}
@@ -59,6 +61,9 @@ class CreateStep2Controller extends AbstractController
$this->ensureCorrectNumberOfParticipants($bookingCreateDto);
$participantsCount = $this->getParticipantsCount($bookingCreateDto);
// Auto-assign participants to rooms if not already assigned
$this->autoAssignRoomsIfNeeded($bookingCreateDto);
// Pre-select mandatory services for participants with birth dates
$this->bookingService->preselectMandatoryServices($bookingCreateDto);
@@ -204,4 +209,28 @@ class CreateStep2Controller extends AbstractController
$this->travelDataService->patchAvailabilities($bookingCreateDto->travel, $availabilities);
}
}
/**
* Automatically assigns participants to rooms if they don't have room assignments yet.
*
* This is called when entering Step 2 to ensure all participants have room assignments
* based on the selected rooms from Step 1. Only assigns if participants are unassigned.
*
* @param BookingCreateDto $bookingCreateDto The booking DTO with participants and room selections
*/
private function autoAssignRoomsIfNeeded(BookingCreateDto $bookingCreateDto): void
{
// Check if any participants need room assignment
$needsAssignment = false;
foreach ($bookingCreateDto->participants as $participant) {
if (null === $participant->assignedRoomId) {
$needsAssignment = true;
break;
}
}
if ($needsAssignment) {
$this->roomAssignmentService->assignParticipantsToRooms($bookingCreateDto);
}
}
}
+58
View File
@@ -0,0 +1,58 @@
<?php
declare(strict_types=1);
namespace App\Service;
use App\Form\Model\BookingCreateDto;
/**
* Handles automatic room assignment for booking participants.
*
* This service automatically assigns participants to selected rooms based on room capacity
* and quantity, eliminating the need for manual room selection in the booking flow.
*/
class RoomAssignmentService
{
/**
* Automatically assigns participants to rooms based on selected room quantities and capacities.
*
* Assignment algorithm:
* - Iterates through selected rooms in order
* - For each room quantity, assigns minPax participants to that room ID
* - Ensures sequential assignment (participant 0, 1, 2, etc.)
*
* Example:
* - 2x "Doppelzimmer" (capacity 2) = participants 0-1 → room A, participants 2-3 → room A
* - 1x "3-Bett-Zimmer" (capacity 3) = participants 4-6 → room B
*
* @param BookingCreateDto $dto The booking DTO containing room selections and participants
*/
public function assignParticipantsToRooms(BookingCreateDto $dto): void
{
$participantIndex = 0;
$selectedRooms = $dto->getSelectedRooms();
$availableRooms = $dto->travel->getAvailableRooms();
foreach ($selectedRooms as $roomSelection) {
$room = $availableRooms[$roomSelection->roomId] ?? null;
if (null === $room) {
continue; // Skip if room not found
}
$capacityPerRoom = $room->minPax ?? 1;
// Assign participants for each room quantity
for ($i = 0; $i < $roomSelection->quantity; ++$i) {
// Assign participants according to room capacity
for ($j = 0; $j < $capacityPerRoom; ++$j) {
if (isset($dto->participants[$participantIndex])) {
$dto->participants[$participantIndex]->assignedRoomId = $room->id;
++$participantIndex;
}
}
}
}
}
}