feat: improved promotional and purchase vouchers

This commit is contained in:
Björn Fromme
2026-03-16 11:59:11 +01:00
parent 7df99521c4
commit fb983f23dd
15 changed files with 357 additions and 137 deletions
@@ -0,0 +1,25 @@
<?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;
}
}
@@ -0,0 +1,71 @@
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use App\Form\Model\ParticipantEditDto;
use App\Service\BookingPriceCalculatorService;
use App\Service\VoucherValidationService;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class PromoVoucherValidator extends ConstraintValidator
{
public function __construct(
private readonly VoucherValidationService $voucherValidationService,
private readonly BookingPriceCalculatorService $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();
}
}
}
@@ -0,0 +1,24 @@
<?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;
}
}
@@ -0,0 +1,48 @@
<?php
declare(strict_types=1);
namespace App\Validator\Constraints;
use App\Form\Model\ParticipantEditDto;
use App\Service\VoucherValidationService;
use Symfony\Component\Validator\Constraint;
use Symfony\Component\Validator\ConstraintValidator;
use Symfony\Component\Validator\Exception\UnexpectedTypeException;
class PurchaseVoucherValidator extends ConstraintValidator
{
public function __construct(
private readonly VoucherValidationService $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();
}
}
}