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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user