fix: enable bookings without ski passes
This commit is contained in:
@@ -102,8 +102,8 @@ class ParticipantDto
|
||||
/** @var list<int> */
|
||||
public array $autoBookOptOutRentalIds = [];
|
||||
|
||||
// Note: Ski pass validation is conditional - see ParticipantEditDto::validateSkiPassRequired()
|
||||
// Babies (0-2 years) are exempt from ski pass requirement
|
||||
// Note: Ski pass validation is conditional - see App\Validator\Constraints\SkiPassSelectionValidator.
|
||||
// Babies (0-2 years) and travels that offer no ski passes at all are exempt.
|
||||
public ?Service $skiPass = null;
|
||||
|
||||
/** @var list<Service> */
|
||||
|
||||
@@ -4,7 +4,6 @@ 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;
|
||||
@@ -21,6 +20,7 @@ use function Symfony\Component\String\u;
|
||||
#[AppAssert\PurchaseVoucher(groups: ['booking_create', 'booking_edit'])]
|
||||
#[AppAssert\PromoVoucher(groups: ['booking_create', 'booking_edit'])]
|
||||
#[AppAssert\MandatoryAdditionalServicesSelected(groups: ['booking_create', 'booking_edit'])]
|
||||
#[AppAssert\SkiPassSelection(groups: ['strict_required'])]
|
||||
class ParticipantEditDto
|
||||
{
|
||||
public function __construct(
|
||||
@@ -30,39 +30,6 @@ 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 submissions, 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 submissions - ski pass is readonly there.
|
||||
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.
|
||||
*
|
||||
|
||||
@@ -7,15 +7,22 @@ namespace App\Service;
|
||||
use App\BusProNet\Constants;
|
||||
use App\BusProNet\Model\Service;
|
||||
use App\Form\Model\BookingDto;
|
||||
use Carbon\CarbonImmutable;
|
||||
use App\Form\Service\ServiceAgeEvaluator;
|
||||
|
||||
/**
|
||||
* Service for evaluating participant eligibility for booking.
|
||||
*
|
||||
* A participant is considered eligible when at least one skipass is available
|
||||
* for their age at the travel date. This service provides the business logic
|
||||
* for eligibility checks used by both the conditional field state system and
|
||||
* the view layer via Twig extension.
|
||||
* Ski passes gate the booking process: a participant who cannot obtain one cannot book.
|
||||
* This service owns that gate and distinguishes the two cases the travel data allows:
|
||||
*
|
||||
* - The travel offers ski passes, but none is selectable for the participant (age rules,
|
||||
* sold out, Buchungsstop) - the participant is ineligible and all service fields are hidden.
|
||||
* - The travel offers no ski passes at all - rare, but legitimate. There is nothing to gate
|
||||
* on, so the participant is eligible and books without a pass, exactly like a baby does.
|
||||
*
|
||||
* Selectability is evaluated through the same pipeline the form uses to build the ski pass
|
||||
* choices (ServiceAgeEvaluator + ServiceAvailabilityCalculator), so eligibility and the
|
||||
* rendered choices cannot disagree.
|
||||
*
|
||||
* Results are cached per request using instance-level arrays to avoid redundant calculations
|
||||
* when checking the same participant multiple times.
|
||||
@@ -25,11 +32,17 @@ class ParticipantEligibilityChecker
|
||||
/** @var array<string, bool> Request-scoped cache for participant eligibility */
|
||||
private array $eligibilityCache = [];
|
||||
|
||||
public function __construct(
|
||||
private readonly ServiceAgeEvaluator $serviceAgeEvaluator,
|
||||
private readonly ServiceAvailabilityCalculator $serviceAvailabilityCalculator,
|
||||
) {
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a participant is eligible for booking.
|
||||
*
|
||||
* 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).
|
||||
* Returns true when the participant can book services, false when the booking process
|
||||
* would be a dead end because no ski pass can be obtained for them.
|
||||
*
|
||||
* 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.
|
||||
@@ -39,7 +52,7 @@ class ParticipantEligibilityChecker
|
||||
* @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 or is baby age)
|
||||
* @return bool True if participant is eligible (has a selectable skipass or needs none)
|
||||
*/
|
||||
public function isParticipantEligible(BookingDto $bookingDto, int $participantIndex): bool
|
||||
{
|
||||
@@ -49,7 +62,9 @@ class ParticipantEligibilityChecker
|
||||
return false;
|
||||
}
|
||||
|
||||
// Create cache key based on participant's birth date, travel date, and index
|
||||
// Create cache key based on participant's birth date, travel date, and index.
|
||||
// Selections of the other participants feed into availability but cannot change
|
||||
// within a single request, so they need no representation in the key.
|
||||
$cacheKey = sprintf(
|
||||
'participant_eligibility_%s_%s_%d',
|
||||
$participant->dateOfBirth->format('Y-m-d'),
|
||||
@@ -57,72 +72,85 @@ class ParticipantEligibilityChecker
|
||||
$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 $this->eligibilityCache[$cacheKey] ??= (function () use ($bookingDto, $participantIndex) {
|
||||
// Nothing gates the booking when no ski pass is required - baby age or a travel
|
||||
// that does not offer ski passes at all
|
||||
if (false === $this->isSkiPassRequired($bookingDto, $participantIndex)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
$allSkiPasses = $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true);
|
||||
$availableSkiPasses = array_filter(
|
||||
$allSkiPasses,
|
||||
fn (Service $service) => $this->isSkiPassAvailableForParticipant($service, $bookingDto, $participantIndex)
|
||||
);
|
||||
|
||||
return !empty($availableSkiPasses);
|
||||
return [] !== $this->getSelectableSkiPasses($bookingDto, $participantIndex);
|
||||
})();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks if a skipass service is available for the given participant based on age constraints.
|
||||
* Checks whether the participant has to select a ski pass to complete the booking.
|
||||
*
|
||||
* Returns false for babies, who never qualify for a pass, and for travels that offer no
|
||||
* ski passes at all, where there is nothing to select.
|
||||
*
|
||||
* Note the asymmetry with eligibility: a travel that offers passes but has none left for
|
||||
* the participant does NOT turn the pass optional. Such a booking has to stay blocked,
|
||||
* not silently go through without a pass.
|
||||
*
|
||||
* @param BookingDto $bookingDto The current booking data
|
||||
* @param int $participantIndex The index of the participant being evaluated
|
||||
*
|
||||
* @return bool True if a ski pass selection is required for this participant
|
||||
*/
|
||||
private function isSkiPassAvailableForParticipant(Service $service, BookingDto $bookingDto, int $participantIndex): bool
|
||||
public function isSkiPassRequired(BookingDto $bookingDto, int $participantIndex): bool
|
||||
{
|
||||
$participant = $bookingDto->getParticipant($participantIndex);
|
||||
|
||||
if (null === $participant || null === $participant->dateOfBirth) {
|
||||
if (null === $participant) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// No age constraints = available to all
|
||||
if (null === $service->ageConstraintType) {
|
||||
return true;
|
||||
// Baby age exemption: participants at or under BABY_MAX_AGE never need a ski pass
|
||||
$age = $participant->getAge($bookingDto->travel->dateFrom);
|
||||
if (null !== $age && $age <= Constants::BABY_MAX_AGE) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$travelStartDate = CarbonImmutable::instance($bookingDto->travel->dateFrom);
|
||||
$birthDate = CarbonImmutable::instance($participant->dateOfBirth);
|
||||
$ageAtTravelStart = (int) $birthDate->diffInYears($travelStartDate);
|
||||
$birthYear = (int) $birthDate->format('Y');
|
||||
return [] !== $this->getOfferedSkiPasses($bookingDto);
|
||||
}
|
||||
|
||||
$constraintType = $service->ageConstraintType ?? 'absolute_age';
|
||||
/**
|
||||
* Collects the ski passes the participant could actually select.
|
||||
*
|
||||
* Applies the same filters as the form's ski pass choices: age constraints first, then
|
||||
* availability (sold out at the API level, exhausted by the other participants of this
|
||||
* booking, or on Buchungsstop).
|
||||
*
|
||||
* @param BookingDto $bookingDto The current booking data
|
||||
* @param int $participantIndex The index of the participant being evaluated
|
||||
*
|
||||
* @return array<int, Service> The selectable ski pass services
|
||||
*/
|
||||
private function getSelectableSkiPasses(BookingDto $bookingDto, int $participantIndex): array
|
||||
{
|
||||
$ageAppropriateSkiPasses = array_filter(
|
||||
$this->getOfferedSkiPasses($bookingDto),
|
||||
fn (Service $service) => false === $this->serviceAgeEvaluator->canEvaluate($service)
|
||||
|| true === $this->serviceAgeEvaluator->isServiceAvailableForParticipant($service, $bookingDto, $participantIndex)
|
||||
);
|
||||
|
||||
if ('absolute_age' === $constraintType || 'mixed' === $constraintType) {
|
||||
$minAge = $service->ageFrom;
|
||||
$maxAge = $service->ageTo;
|
||||
return $this->serviceAvailabilityCalculator->filterAvailableServices(
|
||||
$ageAppropriateSkiPasses,
|
||||
$bookingDto,
|
||||
$participantIndex
|
||||
);
|
||||
}
|
||||
|
||||
if (null !== $minAge && $ageAtTravelStart < $minAge) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null !== $maxAge && $ageAtTravelStart > $maxAge) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if ('birth_year' === $constraintType || 'mixed' === $constraintType) {
|
||||
$minBirthYear = $service->birthYearFrom;
|
||||
$maxBirthYear = $service->birthYearTo;
|
||||
|
||||
if (null !== $minBirthYear && $birthYear < $minBirthYear) {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (null !== $maxBirthYear && $birthYear > $maxBirthYear) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
return true;
|
||||
/**
|
||||
* Collects the ski passes the travel offers within its date range, ignoring the participant.
|
||||
*
|
||||
* @param BookingDto $bookingDto The current booking data
|
||||
*
|
||||
* @return array<int, Service> The ski pass services offered by the travel
|
||||
*/
|
||||
private function getOfferedSkiPasses(BookingDto $bookingDto): array
|
||||
{
|
||||
return $bookingDto->travel->getAdditionalServicesBySubTypes(Constants::TOKEN_SKI_PASS, true);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
|
||||
/**
|
||||
* Validates that a ski pass is selected whenever one is required.
|
||||
*
|
||||
* Applied on ParticipantEditDto because the decision needs the ParticipantEligibilityChecker,
|
||||
* which a DTO callback cannot reach.
|
||||
*/
|
||||
#[\Attribute]
|
||||
class SkiPassSelection extends Constraint
|
||||
{
|
||||
public string $message = 'Bitte auswählen';
|
||||
|
||||
public string $notBookableMessage = 'Für Teilnehmer:in {{ number }} ist keine Buchung möglich';
|
||||
|
||||
public function getTargets(): array|string
|
||||
{
|
||||
return static::CLASS_CONSTRAINT;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,75 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Validator\Constraints;
|
||||
|
||||
use App\Form\Model\BookingDto;
|
||||
use App\Form\Model\ParticipantEditDto;
|
||||
use App\Service\ParticipantEligibilityChecker;
|
||||
use Symfony\Component\Validator\Constraint;
|
||||
use Symfony\Component\Validator\ConstraintValidator;
|
||||
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
|
||||
|
||||
/**
|
||||
* Enforces ski pass selection for participants that need one.
|
||||
*
|
||||
* Three outcomes, mirroring what the form actually renders:
|
||||
*
|
||||
* - Ineligible participant: no ski pass is obtainable, so the ski pass field is not rendered
|
||||
* at all. Asking for a selection would be unsatisfiable - the booking is rejected as not
|
||||
* bookable instead.
|
||||
* - Ski pass required and none selected: the regular "please select" violation.
|
||||
* - Ski pass not required (baby, or a travel that offers no ski passes): no violation.
|
||||
*/
|
||||
class SkiPassSelectionValidator extends ConstraintValidator
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ParticipantEligibilityChecker $participantEligibilityService,
|
||||
) {
|
||||
}
|
||||
|
||||
public function validate(mixed $value, Constraint $constraint): void
|
||||
{
|
||||
if (false === $constraint instanceof SkiPassSelection) {
|
||||
throw new UnexpectedTypeException($constraint, SkiPassSelection::class);
|
||||
}
|
||||
|
||||
if (false === $value instanceof ParticipantEditDto) {
|
||||
throw new UnexpectedTypeException($value, ParticipantEditDto::class);
|
||||
}
|
||||
|
||||
// Skip validation in edit submissions - ski pass is readonly there.
|
||||
if (BookingDto::MODE_EDIT === $value->bookingContext->getMode()) {
|
||||
return;
|
||||
}
|
||||
|
||||
$participant = $value->participant;
|
||||
|
||||
// Without a birth date nothing can be evaluated - the date of birth constraint reports it
|
||||
if (null === $participant->dateOfBirth) {
|
||||
return;
|
||||
}
|
||||
|
||||
$participantIndex = $participant->index ?? 0;
|
||||
|
||||
if (false === $this->participantEligibilityService->isParticipantEligible($value->bookingContext, $participantIndex)) {
|
||||
$this->context->buildViolation($constraint->notBookableMessage)
|
||||
->setParameter('{{ number }}', (string) ($participantIndex + 1))
|
||||
->atPath('participant.skiPass')
|
||||
->addViolation();
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (false === $this->participantEligibilityService->isSkiPassRequired($value->bookingContext, $participantIndex)) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (null === $participant->skiPass) {
|
||||
$this->context->buildViolation($constraint->message)
|
||||
->atPath('participant.skiPass')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user