feat: special treatment of participants with age 0-2 (babies)

This commit is contained in:
Björn Fromme
2026-03-16 11:59:12 +01:00
parent cd1ffc3e9a
commit 2e40cbc7c7
12 changed files with 615 additions and 15 deletions
+4
View File
@@ -64,4 +64,8 @@ final class Constants
// Payment type IDs for API
public const PAYMENT_TYPE_ID_TRANSFER = 2;
public const PAYMENT_TYPE_ID_DEBIT = 5;
// Baby room constraints
public const BABY_MAX_AGE = 2;
public const BABY_ROOM_CODE = 'Baby';
}
+2 -1
View File
@@ -78,7 +78,8 @@ class ParticipantDto
public array $courses = [];
public array $additionalServices = [];
#[Assert\NotNull(message: 'Bitte auswählen', groups: ['strict_required'])]
// Note: Ski pass validation is conditional - see ParticipantEditDto::validateSkiPassRequired()
// Babies (0-2 years) are exempt from ski pass requirement
public ?Service $skiPass = null;
public array $board = [];
+34
View File
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Form\Model;
use App\BusProNet\Constants;
use App\Validator\Constraints as AppAssert;
use Symfony\Component\Validator\Constraints as Assert;
use Symfony\Component\Validator\Context\ExecutionContextInterface;
@@ -27,6 +28,39 @@ class ParticipantEditDto
) {
}
/**
* Validates that ski pass is selected when required.
*
* Ski pass is required for all participants in create mode, EXCEPT for babies
* (participants aged 0-2 years at travel date). Babies don't qualify for any
* ski pass and the field is hidden for them.
*
* In edit mode, this validation is skipped as the ski pass is readonly.
*
* This validation only runs when strict_required group is active.
*/
#[Assert\Callback(groups: ['strict_required'])]
public function validateSkiPassRequired(ExecutionContextInterface $context): void
{
// Skip validation in edit mode - ski pass is readonly
if (BookingDto::MODE_EDIT === $this->bookingContext->getMode()) {
return;
}
// Baby age exemption: skip validation for participants at or under BABY_MAX_AGE
$age = $this->participant->getAge($this->bookingContext->travel->dateFrom);
if (null !== $age && $age <= Constants::BABY_MAX_AGE) {
return;
}
// Ski pass is required for non-baby participants
if (null === $this->participant->skiPass) {
$context->buildViolation('Bitte auswählen')
->atPath('participant.skiPass')
->addViolation();
}
}
/**
* Validates that insurance is selected when required.
*
@@ -0,0 +1,75 @@
<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\BusProNet\Constants;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that evaluates if a participant is at or under baby age (0-2 years).
*
* This condition checks if a participant's age at travel start date is at or under
* the BABY_MAX_AGE threshold (2 years). It's used to conditionally hide fields
* that are not applicable to babies, such as ski pass selection.
*
* Age is calculated at the travel start date, consistent with other age-dependent
* services in the system (insurance, ski pass, etc.).
*/
class BabyAgeCondition implements FieldConditionInterface
{
/**
* Evaluates if the participant is at or under baby age.
*
* Calculates the participant's age at travel start date and checks if it's
* at or under BABY_MAX_AGE (2 years). Returns false if the participant has
* no date of birth set.
*
* @param BookingDto $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated
* @param array<string, mixed> $formData Current form data (unused)
*
* @return bool True if the participant is at or under baby age, false otherwise
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
$participant = $bookingDto->getParticipant($participantIndex);
if (null === $participant || null === $participant->dateOfBirth) {
return false;
}
$age = $participant->getAge($bookingDto->travel->dateFrom);
if (null === $age) {
return false;
}
return $age <= Constants::BABY_MAX_AGE;
}
/**
* Returns field names that affect baby age calculation.
*
* The condition depends on the participant's date of birth field.
* When this field changes, any conditions based on baby age should be re-evaluated.
*
* @return string[] Array containing 'dateOfBirth' field name
*/
public function getDependentFields(): array
{
return ['dateOfBirth'];
}
/**
* Returns a human-readable description of the condition.
*
* @return string Description of the baby age condition
*/
public function getDescription(): string
{
return sprintf('Participant is at or under baby age (%d years)', Constants::BABY_MAX_AGE);
}
}
@@ -0,0 +1,41 @@
<?php
declare(strict_types=1);
namespace App\Form\Service\Condition;
use App\Form\Model\BookingDto;
use App\Form\Service\Contract\FieldConditionInterface;
/**
* Condition that checks if the booking has multiple participants.
*
* This is used to hide fields that are only relevant when there are
* multiple participants, such as the bulk insurance assignment option.
*/
class MultipleParticipantsCondition implements FieldConditionInterface
{
/**
* Evaluates if the booking has more than one participant.
*
* @param BookingDto $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated (unused)
* @param array<string, mixed> $formData Current form data (unused)
*
* @return bool True if the booking has 2+ participants, false otherwise
*/
public function evaluate(BookingDto $bookingDto, int $participantIndex, array $formData): bool
{
return count($bookingDto->participants) > 1;
}
public function getDependentFields(): array
{
return [];
}
public function getDescription(): string
{
return 'Checks if the booking has multiple participants (2+)';
}
}
+16 -4
View File
@@ -8,12 +8,14 @@ use App\BusProNet\Utility\DirectionMapper;
use App\Form\Service\Abstract\AbstractFieldStateProvider;
use App\Form\Service\Condition\ApplicantCondition;
use App\Form\Service\Condition\AuthenticatedUserPersonalDataCondition;
use App\Form\Service\Condition\BabyAgeCondition;
use App\Form\Service\Condition\BookingEligibilityCondition;
use App\Form\Service\Condition\BulkInsuranceBookingCondition;
use App\Form\Service\Condition\CompositeCondition;
use App\Form\Service\Condition\DateOfBirthProvidedCondition;
use App\Form\Service\Condition\FieldValueCondition;
use App\Form\Service\Condition\FinalBookingOnlyCondition;
use App\Form\Service\Condition\MultipleParticipantsCondition;
use App\Form\Service\Condition\RentalSelectionCondition;
use App\Form\Service\Condition\RoomSelectionCondition;
use App\Form\Service\Condition\ServiceSubTypeCondition;
@@ -142,6 +144,9 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
// Hide age-dependent fields when no date of birth is provided
$dateOfBirthProvidedCondition = new DateOfBirthProvidedCondition();
// Baby age condition - hide ski pass for babies (0-2 years)
$babyAgeCondition = new BabyAgeCondition();
// Age-dependent service fields are hidden until birth date is provided OR when participant is ineligible
$this->fieldStateConditions['courses'] = [
'hidden' => CompositeCondition::or(
@@ -157,11 +162,15 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
),
];
// Skipass field - hidden when participant is ineligible OR when no date of birth
// Skipass field - hidden when:
// - No date of birth provided
// - Participant is ineligible (no skipasses for their age)
// - Participant is baby age (0-2 years) - babies don't need ski passes
$this->fieldStateConditions['skiPass'] = [
'hidden' => CompositeCondition::or(
CompositeCondition::not($dateOfBirthProvidedCondition),
$bookingEligibilityCondition
$bookingEligibilityCondition,
$babyAgeCondition
),
];
@@ -184,11 +193,14 @@ class CreateFieldStateProvider extends AbstractFieldStateProvider
// Bulk insurance booking conditions
$bulkInsuranceBookingCondition = new BulkInsuranceBookingCondition();
// Show bulk insurance booking checkbox ONLY for applicant (index 0) and when insurance field is visible
// Show bulk insurance booking checkbox ONLY for applicant (index 0) when:
// - Date of birth is provided
// - There are multiple participants (bulk assignment is meaningless for single participant)
$this->fieldStateConditions['bulkInsuranceBooking'] = [
'hidden' => CompositeCondition::or(
CompositeCondition::not($dateOfBirthProvidedCondition), // Hide until date of birth provided
CompositeCondition::not(new ApplicantCondition()) // Hide for non-applicants
CompositeCondition::not(new ApplicantCondition()), // Hide for non-applicants
CompositeCondition::not(new MultipleParticipantsCondition()) // Hide when only one participant
),
];
@@ -16,7 +16,6 @@ use App\Form\Service\Factory\ParticipantRoomChoiceLoaderFactory;
use App\Service\BookingPriceCalculatorService;
use App\Service\InsuranceService;
use App\Service\ServiceAvailabilityCalculator;
use App\Validator\Constraints\RoomSelection;
/**
* Provides dynamic field options for participant form fields.
@@ -580,9 +579,9 @@ class ParticipantFieldOptionsProvider extends AbstractFieldOptionsProvider
return false;
}
// Baby rooms only available for participants 2 years or younger
if (RoomSelection::BABY_ROOM_CODE === $room->code) {
return $age <= 2;
// Baby rooms only available for participants at or under BABY_MAX_AGE
if (Constants::BABY_ROOM_CODE === $room->code) {
return $age <= Constants::BABY_MAX_AGE;
}
// All other rooms available for all ages
+11 -2
View File
@@ -31,12 +31,15 @@ class ParticipantEligibilityService
* Returns true when the participant can book services (at least one skipass available).
* Returns false when the participant cannot book (no skipasses available for their age).
*
* Baby age exemption: Participants aged 0-2 years (at travel date) are always eligible
* regardless of skipass availability, as babies don't require ski passes.
*
* Results are cached per request to avoid redundant calculations.
*
* @param BookingDto $bookingDto The current booking data
* @param int $participantIndex The index of the participant being evaluated
*
* @return bool True if participant is eligible (has available skipasses)
* @return bool True if participant is eligible (has available skipasses or is baby age)
*/
public function isParticipantEligible(BookingDto $bookingDto, int $participantIndex): bool
{
@@ -54,7 +57,13 @@ class ParticipantEligibilityService
$participantIndex
);
return $this->eligibilityCache[$cacheKey] ??= (function () use ($bookingDto, $participantIndex) {
return $this->eligibilityCache[$cacheKey] ??= (function () use ($bookingDto, $participantIndex, $participant) {
// Baby age exemption: participants at or under BABY_MAX_AGE are always eligible
$age = $participant->getAge($bookingDto->travel->dateFrom);
if (null !== $age && $age <= Constants::BABY_MAX_AGE) {
return true;
}
$allSkiPasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true, true);
$availableSkiPasses = array_filter(
$allSkiPasses,
@@ -9,8 +9,6 @@ use Symfony\Component\Validator\Constraint;
#[\Attribute]
class RoomSelection extends Constraint
{
public const BABY_ROOM_CODE = 'Baby';
public string $noRoomSelectedMessage = 'Bitte mindestens ein Zimmer/Bett auswählen';
public string $onlyBabyRoomsMessage = 'Baby-Zimmer können nur in Kombination mit regulären Zimmern gebucht werden.';
@@ -4,6 +4,7 @@ declare(strict_types=1);
namespace App\Validator\Constraints;
use App\BusProNet\Constants;
use App\Form\Model\BookingDto;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
@@ -50,14 +51,14 @@ class RoomSelectionValidator extends ConstraintValidator
continue;
}
if (RoomSelection::BABY_ROOM_CODE === $room->code) {
if (Constants::BABY_ROOM_CODE === $room->code) {
$hasBabyRoom = true;
} else {
$hasRegularRoom = true;
}
}
if ($hasBabyRoom && !$hasRegularRoom) {
if ($hasBabyRoom && false === $hasRegularRoom) {
$this->context->buildViolation($constraint->onlyBabyRoomsMessage)
->addViolation();
}