feat: fold purchase and promo voucher fields into one code field

This commit is contained in:
fromme committed 2026-10-06 10:41:30 +02:00
1 parent 93512b8345
commit 21d756a2e3
21 files changed
+801 -432

No files matched your search

@@ -1,25 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* Validates promotional voucher codes via BPN API.
*
* Checks code validity, travel applicability, and minimum price threshold.
* Requires participant pricing calculation and travel context.
* Applied at ParticipantEditDto level to access booking context.
*/
#[\Attribute]
class PromoVoucher extends Constraint
{
public string $message = 'Aktionscode ungültig: {{ error }}';
public function getTargets(): array|string
{
return static::CLASS_CONSTRAINT;
}
}
@@ -1,78 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use App\Form\Model\ParticipantEditDto;
use App\Service\BookingPriceCalculator;
use App\Service\VoucherValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validates promotional voucher codes against the BPN API.
*
* Verifies that promo codes are valid for the current travel, checks minimum price
* thresholds, and validates code availability. Uses the participant's calculated
* total price (including room and all selected services) for threshold comparison.
*/
class PromoVoucherValidator extends ConstraintValidator
{
public function __construct(
private readonly VoucherValidator $voucherValidationService,
private readonly BookingPriceCalculator $priceCalculatorService,
) {
}
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof PromoVoucher) {
throw new UnexpectedTypeException($constraint, PromoVoucher::class);
}
if (!$value instanceof ParticipantEditDto) {
throw new UnexpectedTypeException($value, ParticipantEditDto::class);
}
$promoCode = $value->participant->promoVoucherCode;
// Skip validation if promo code is empty
if (null === $promoCode || '' === trim($promoCode)) {
return;
}
// Get travel ID from booking context
$travelId = $value->bookingContext->travel->id;
if (null === $travelId) {
$this->context->buildViolation('Aktionscode konnte nicht validiert werden: Reise-ID fehlt.')
->atPath('participant.promoVoucherCode')
->addViolation();
return;
}
// Calculate participant's current price including all services
$participantPrice = $this->priceCalculatorService->calculateIndividualParticipantPrice(
$value->bookingContext,
$value->participant->index ?? 0
);
// Validate promo voucher via API
$result = $this->voucherValidationService->validatePromoVoucher(
$promoCode,
$travelId,
$participantPrice
);
// Add violation if voucher is invalid
if (false === $result->isValid) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ error }}', $result->errorMessage ?? 'Unbekannter Fehler')
->atPath('participant.promoVoucherCode')
->addViolation();
}
}
}
@@ -1,24 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use Symfony\Component\Validator\Constraint;
/**
* Validates purchase voucher codes via BPN API.
*
* Checks that the voucher exists and has remaining balance.
* Applied at ParticipantEditDto level to access booking context.
*/
#[\Attribute]
class PurchaseVoucher extends Constraint
{
public string $message = 'Gutschein ungültig: {{ error }}';
public function getTargets(): array|string
{
return static::CLASS_CONSTRAINT;
}
}
@@ -1,54 +0,0 @@
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use App\Form\Model\ParticipantEditDto;
use App\Service\VoucherValidator;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
/**
* Validates purchase voucher codes against the BPN API.
*
* Verifies that voucher codes exist and have remaining balance. Unlike promo
* vouchers, purchase vouchers do not require travel or price context validation.
*/
class PurchaseVoucherValidator extends ConstraintValidator
{
public function __construct(
private readonly VoucherValidator $voucherValidationService,
) {
}
public function validate(mixed $value, Constraint $constraint): void
{
if (!$constraint instanceof PurchaseVoucher) {
throw new UnexpectedTypeException($constraint, PurchaseVoucher::class);
}
if (!$value instanceof ParticipantEditDto) {
throw new UnexpectedTypeException($value, ParticipantEditDto::class);
}
$voucherCode = $value->participant->purchaseVoucherCode;
// Skip validation if voucher code is empty
if (null === $voucherCode || '' === trim($voucherCode)) {
return;
}
// Validate voucher via API
$result = $this->voucherValidationService->validatePurchaseVoucher($voucherCode);
// Add violation if voucher is invalid
if (false === $result->isValid) {
$this->context->buildViolation($constraint->message)
->setParameter('{{ error }}', $result->errorMessage ?? 'Unbekannter Fehler')
->atPath('participant.purchaseVoucherCode')
->addViolation();
}
}
}