fix: refuse implausible accommodation booking prices
This commit is contained in:
1 parent
773cd4f847
commit
56adf72d12
16 files changed
+628
-15
No files matched your search
@@ -6,11 +6,13 @@ namespace App\Controller\Admin\AccommodationBooking;
|
||||
|
||||
use App\Entity\Groups\Accommodation;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Exception\ImplausibleBookingPriceException;
|
||||
use App\Form\Admin\Groups\AccommodationBookingChangeAccommodationType;
|
||||
use App\Htmx\HxTrait;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -49,7 +51,24 @@ class ChangeAccommodationController extends AbstractController
|
||||
$selected = $form->get('accommodation')->getData();
|
||||
|
||||
if ($selected instanceof Accommodation && $selected !== $booking->getAccommodation()) {
|
||||
$this->bookingService->changeAccommodation($booking, $selected);
|
||||
try {
|
||||
$this->bookingService->changeAccommodation($booking, $selected);
|
||||
} catch (ImplausibleBookingPriceException $e) {
|
||||
// The house that was picked prices this stay implausibly high, so the change
|
||||
// did not happen. Reported in the modal the office is standing in front of —
|
||||
// the same treatment the create and edit forms give it.
|
||||
$this->logger->error('Refused to change accommodation over an implausible booking total', [
|
||||
'exception' => $e,
|
||||
'id' => $booking->getId(),
|
||||
'accommodation' => $selected->getCalendarCode(),
|
||||
]);
|
||||
$form->addError(new FormError(ImplausibleBookingPriceException::FORM_ERROR_MESSAGE));
|
||||
|
||||
return $this->render('admin/accommodation_booking/modal_change_accommodation.html.twig', [
|
||||
'booking' => $booking,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
$this->addFlash('success', 'Das Gruppenhaus wurde geändert. Bitte wähle die Leistungen neu aus.');
|
||||
|
||||
|
||||
@@ -6,12 +6,15 @@ namespace App\Controller\Admin\AccommodationBooking;
|
||||
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Entity\User;
|
||||
use App\Exception\ImplausibleBookingPriceException;
|
||||
use App\Form\Admin\Groups\AccommodationBookingCreateType;
|
||||
use App\Htmx\HxRedirectResponse;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -45,8 +48,7 @@ class CreateController extends AbstractController
|
||||
]);
|
||||
$form->handleRequest($request);
|
||||
|
||||
if ($form->isSubmitted() && $form->isValid()) {
|
||||
$this->bookingService->refreshPriceSnapshot($booking);
|
||||
if ($form->isSubmitted() && $form->isValid() && $this->refreshPriceSnapshot($form, $booking)) {
|
||||
$this->entityManager->persist($booking);
|
||||
$this->entityManager->flush();
|
||||
|
||||
@@ -65,4 +67,31 @@ class CreateController extends AbstractController
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The price is computed after validation, so an implausible total cannot be a constraint on
|
||||
* the form — it is reported as one anyway, because the office's next move is to go and fix
|
||||
* the house's prices, not to read an error page.
|
||||
*
|
||||
* @param FormInterface<AccommodationBooking> $form
|
||||
*/
|
||||
private function refreshPriceSnapshot(FormInterface $form, AccommodationBooking $booking): bool
|
||||
{
|
||||
try {
|
||||
$this->bookingService->refreshPriceSnapshot($booking);
|
||||
|
||||
return true;
|
||||
} catch (ImplausibleBookingPriceException $e) {
|
||||
$this->logger->error('Refused to store an implausible accommodation booking total', [
|
||||
'exception' => $e,
|
||||
'id' => $booking->getId(),
|
||||
'accommodation' => $booking->getAccommodation()?->getCalendarCode(),
|
||||
'paxCount' => $booking->getPaxCount(),
|
||||
'nights' => $booking->getNights(),
|
||||
]);
|
||||
$form->addError(new FormError(ImplausibleBookingPriceException::FORM_ERROR_MESSAGE));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,7 @@ namespace App\Controller\Admin\AccommodationBooking;
|
||||
use App\Entity\Groups\AccommodationBooking;
|
||||
use App\Entity\Groups\AdditionalService;
|
||||
use App\Entity\Groups\BoardService;
|
||||
use App\Exception\ImplausibleBookingPriceException;
|
||||
use App\Form\Admin\Groups\AccommodationBookingType;
|
||||
use App\Repository\Groups\AdditionalServiceRepository;
|
||||
use App\Repository\Groups\BoardServiceRepository;
|
||||
@@ -15,6 +16,8 @@ use App\Service\AccommodationBookingService;
|
||||
use Doctrine\ORM\EntityManagerInterface;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
|
||||
use Symfony\Component\Form\FormError;
|
||||
use Symfony\Component\Form\FormInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -121,7 +124,15 @@ class EditController extends AbstractController
|
||||
}
|
||||
}
|
||||
|
||||
$this->bookingService->refreshPriceSnapshot($booking);
|
||||
if (!$this->refreshPriceSnapshot($form, $booking)) {
|
||||
// The submitted changes stay on the entity but are never flushed, so the form
|
||||
// comes back filled in with the error on it. Logging is safe here: the database
|
||||
// handler writes through DBAL, not the EntityManager.
|
||||
return $this->render('admin/accommodation_booking/edit.html.twig', [
|
||||
'booking' => $booking,
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
// Editing never moves the booking on and never mails the customer: every status
|
||||
// change is its own named action (Angebot senden, bestätigen, absagen), so the
|
||||
@@ -154,4 +165,31 @@ class EditController extends AbstractController
|
||||
'form' => $form,
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* The price is computed after validation, so an implausible total cannot be a constraint on
|
||||
* the form — it is reported as one anyway, because the office's next move is to go and fix
|
||||
* the house's prices, not to read an error page.
|
||||
*
|
||||
* @param FormInterface<AccommodationBooking> $form
|
||||
*/
|
||||
private function refreshPriceSnapshot(FormInterface $form, AccommodationBooking $booking): bool
|
||||
{
|
||||
try {
|
||||
$this->bookingService->refreshPriceSnapshot($booking);
|
||||
|
||||
return true;
|
||||
} catch (ImplausibleBookingPriceException $e) {
|
||||
$this->logger->error('Refused to store an implausible accommodation booking total', [
|
||||
'exception' => $e,
|
||||
'id' => $booking->getId(),
|
||||
'accommodation' => $booking->getAccommodation()?->getCalendarCode(),
|
||||
'paxCount' => $booking->getPaxCount(),
|
||||
'nights' => $booking->getNights(),
|
||||
]);
|
||||
$form->addError(new FormError(ImplausibleBookingPriceException::FORM_ERROR_MESSAGE));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -8,6 +8,7 @@ use App\Entity\Groups\Accommodation;
|
||||
use App\Entity\Groups\AdditionalService;
|
||||
use App\Entity\Groups\BoardService;
|
||||
use App\Exception\AccommodationSessionNotFoundException;
|
||||
use App\Exception\ImplausibleBookingPriceException;
|
||||
use App\Form\AccommodationBookingConfirmationType;
|
||||
use App\Form\AccommodationInquiryConfirmationType;
|
||||
use App\Form\Model\AccommodationBookingDto;
|
||||
@@ -16,6 +17,7 @@ use App\Model\AccommodationBookingContext;
|
||||
use App\Service\AccommodationBookingService;
|
||||
use App\Service\AccommodationSessionManager;
|
||||
use App\Service\AccommodationTermsUrlProvider;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use Symfony\Component\HttpFoundation\Response;
|
||||
use Symfony\Component\Routing\Attribute\Route;
|
||||
@@ -28,6 +30,7 @@ class Step4Controller extends AbstractController
|
||||
private readonly AccommodationBookingService $bookingService,
|
||||
private readonly AccommodationSessionManager $sessionManager,
|
||||
private readonly AccommodationTermsUrlProvider $termsUrlProvider,
|
||||
private readonly LoggerInterface $logger,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -51,7 +54,9 @@ class Step4Controller extends AbstractController
|
||||
}
|
||||
|
||||
$dto->forceInquiry = true;
|
||||
$this->persistBooking($request, $dto, $accommodation, $services);
|
||||
if (null !== $failure = $this->persistBooking($request, $dto, $accommodation, $services)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->redirectToRoute('app_groups_booking_success');
|
||||
}
|
||||
@@ -102,7 +107,9 @@ class Step4Controller extends AbstractController
|
||||
|
||||
if ($confirmationForm->isSubmitted() && $confirmationForm->isValid()) {
|
||||
$services = $this->bookingService->loadAvailableServices($dto, $accommodation);
|
||||
$this->persistBooking($request, $dto, $accommodation, $services);
|
||||
if (null !== $failure = $this->persistBooking($request, $dto, $accommodation, $services)) {
|
||||
return $failure;
|
||||
}
|
||||
|
||||
return $this->htmxRedirect($request, $this->generateUrl('app_groups_booking_success'));
|
||||
}
|
||||
@@ -148,12 +155,35 @@ class Step4Controller extends AbstractController
|
||||
* groupedAdditionalServices: array<string, AdditionalService[]>,
|
||||
* ungroupedAdditionalServices: AdditionalService[]
|
||||
* } $services
|
||||
*
|
||||
* @return Response|null a response to return instead of the success redirect, or null when
|
||||
* the booking was stored
|
||||
*/
|
||||
private function persistBooking(Request $request, AccommodationBookingDto $dto, Accommodation $accommodation, array $services): void
|
||||
private function persistBooking(Request $request, AccommodationBookingDto $dto, Accommodation $accommodation, array $services): ?Response
|
||||
{
|
||||
$prices = $this->bookingService->loadPrices($dto, $accommodation);
|
||||
$booking = $this->bookingService->finalizeBooking($dto, $accommodation, $prices, $services);
|
||||
|
||||
try {
|
||||
$booking = $this->bookingService->finalizeBooking($dto, $accommodation, $prices, $services);
|
||||
} catch (ImplausibleBookingPriceException $e) {
|
||||
// The bounded inputs are already rejected in step 2, so what is left is a mistyped
|
||||
// catalog price for this house — nothing the customer can act on and nothing to show
|
||||
// them an error page over. The session survives so the desk can pick the inquiry up
|
||||
// by phone with the data still in hand.
|
||||
$this->logger->error('Refused to store an implausible accommodation booking total', [
|
||||
'exception' => $e,
|
||||
'accommodation' => $accommodation->getCalendarCode(),
|
||||
'paxCount' => $dto->paxCount,
|
||||
'nights' => $dto->getNights(),
|
||||
]);
|
||||
$this->addFlash('error', 'Die Anfrage konnte nicht abgeschlossen werden. Bitte wende dich direkt an uns — wir kümmern uns darum.');
|
||||
|
||||
return $this->htmxRedirect($request, $this->generateUrl('app_groups_booking_error'));
|
||||
}
|
||||
|
||||
$this->addFlash('groups_booking_result', $booking->getOrigin()->value);
|
||||
$this->sessionManager->clear($request);
|
||||
|
||||
return null;
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user