fix: reset room assignments after updated selection if required
This commit is contained in:
@@ -74,6 +74,9 @@ class Step2Controller extends AbstractController
|
||||
fn ($user, $participant) => $this->prepopulationService->prepopulateApplicantFromUser($user, $participant)
|
||||
);
|
||||
|
||||
// Validate room assignments against current selection (handles back-navigation from step 2 to step 1)
|
||||
$this->roomAssignmentService->validateAndResetInvalidAssignments($bookingCreateDto);
|
||||
|
||||
// Auto-assign rooms if needed
|
||||
$this->roomAssignmentService->assignRoomsIfNeeded($bookingCreateDto);
|
||||
|
||||
|
||||
@@ -110,4 +110,48 @@ class RoomAssignmentService
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Validates and resets room assignments that reference rooms no longer selected.
|
||||
*
|
||||
* This method ensures room assignments remain valid after room selection changes in Step 1.
|
||||
* When a user goes back to Step 1 and changes room selections, existing assignments may
|
||||
* reference room IDs that are no longer in the selected rooms list. This method detects
|
||||
* such invalid assignments and resets them to null.
|
||||
*
|
||||
* @param BookingDto $dto The booking DTO to validate
|
||||
*
|
||||
* @return bool True if any assignments were reset, false if all were valid
|
||||
*/
|
||||
public function validateAndResetInvalidAssignments(BookingDto $dto): bool
|
||||
{
|
||||
$selectedRoomIds = $this->getSelectedRoomIds($dto);
|
||||
$resetOccurred = false;
|
||||
|
||||
foreach ($dto->participants as $participant) {
|
||||
if (null !== $participant->assignedRoomId && false === in_array($participant->assignedRoomId, $selectedRoomIds, true)) {
|
||||
$participant->assignedRoomId = null;
|
||||
$resetOccurred = true;
|
||||
}
|
||||
}
|
||||
|
||||
return $resetOccurred;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the list of room IDs from currently selected rooms.
|
||||
*
|
||||
* @param BookingDto $dto The booking DTO containing room selections
|
||||
*
|
||||
* @return array<int> Array of selected room IDs
|
||||
*/
|
||||
private function getSelectedRoomIds(BookingDto $dto): array
|
||||
{
|
||||
$ids = [];
|
||||
foreach ($dto->getSelectedRooms() as $roomSelection) {
|
||||
$ids[] = $roomSelection->id;
|
||||
}
|
||||
|
||||
return $ids;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user