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
@@ -12,11 +12,13 @@ use App\Entity\User;
|
||||
use App\Enum\Groups\AccommodationBookingOrigin;
|
||||
use App\Enum\Groups\AccommodationBookingStatus;
|
||||
use App\Enum\Groups\AdditionalServiceType;
|
||||
use App\Exception\ImplausibleBookingPriceException;
|
||||
use App\Repository\Groups\AccommodationBookingRepository;
|
||||
use Doctrine\DBAL\Types\Types;
|
||||
use Doctrine\ORM\Mapping as ORM;
|
||||
use Symfony\Component\Uid\Uuid;
|
||||
use Symfony\Component\Validator\Constraints as Assert;
|
||||
use Symfony\Component\Validator\Context\ExecutionContextInterface;
|
||||
|
||||
#[ORM\Entity(repositoryClass: AccommodationBookingRepository::class)]
|
||||
class AccommodationBooking implements BlameableEntityInterface, TimestampableEntityInterface
|
||||
@@ -24,6 +26,24 @@ class AccommodationBooking implements BlameableEntityInterface, TimestampableEnt
|
||||
use BlameableEntity;
|
||||
use TimestampableEntity;
|
||||
|
||||
/**
|
||||
* Plausibility ceilings for the two figures every price in the breakdown is multiplied by.
|
||||
* Both sit above anything the group houses actually see — the largest booking on record is
|
||||
* 150 people for 18 nights — and exist so that a mistyped year or pax count is answered with
|
||||
* a form error rather than a price nobody meant.
|
||||
*/
|
||||
public const int MAX_PAX = 250;
|
||||
public const int MAX_NIGHTS = 30;
|
||||
|
||||
/**
|
||||
* And the ceiling on the product of the two, in minor units: a million euros against a
|
||||
* largest-ever booking of some 42,000. The inputs above cannot reach it, so what it catches
|
||||
* is a mistyped catalog price, the one factor of the total that is not bounded per booking.
|
||||
* Deliberately well inside what the INT column holds — the column's range is not a price
|
||||
* limit, and a total that needs more room is a mistake rather than a wider column.
|
||||
*/
|
||||
public const int MAX_TOTAL_PRICE = 100_000_000;
|
||||
|
||||
#[ORM\Id]
|
||||
#[ORM\GeneratedValue]
|
||||
#[ORM\Column]
|
||||
@@ -40,9 +60,11 @@ class AccommodationBooking implements BlameableEntityInterface, TimestampableEnt
|
||||
private ?\DateTimeImmutable $dateFrom = null;
|
||||
|
||||
#[ORM\Column(type: Types::DATE_IMMUTABLE)]
|
||||
#[Assert\GreaterThan(propertyPath: 'dateFrom', message: 'Das Abreisedatum muss nach dem Anreisedatum liegen.')]
|
||||
private ?\DateTimeImmutable $dateTo = null;
|
||||
|
||||
#[ORM\Column]
|
||||
#[Assert\Range(min: 0, max: self::MAX_PAX)]
|
||||
private int $paxCount = 0;
|
||||
|
||||
#[ORM\Column]
|
||||
@@ -68,6 +90,7 @@ class AccommodationBooking implements BlameableEntityInterface, TimestampableEnt
|
||||
#[ORM\Column(type: Types::JSON, nullable: true)]
|
||||
private ?array $priceBreakdown = null;
|
||||
|
||||
/** In minor units, like every other price here, and never above self::MAX_TOTAL_PRICE. */
|
||||
#[ORM\Column(nullable: true)]
|
||||
private ?int $totalPrice = null;
|
||||
|
||||
@@ -232,6 +255,25 @@ class AccommodationBooking implements BlameableEntityInterface, TimestampableEnt
|
||||
return $this->dateFrom->diff($this->dateTo)->days;
|
||||
}
|
||||
|
||||
/**
|
||||
* Reported on dateTo, the field a mistyped year is corrected in — a stay of 200 years is
|
||||
* always the departure date being wrong, never the arrival.
|
||||
*/
|
||||
#[Assert\Callback]
|
||||
public function validateStayLength(ExecutionContextInterface $context): void
|
||||
{
|
||||
if (null === $this->dateFrom || null === $this->dateTo) {
|
||||
return;
|
||||
}
|
||||
|
||||
if ($this->getNights() > self::MAX_NIGHTS) {
|
||||
$context->buildViolation('Der Aufenthalt darf höchstens {{ max }} Nächte umfassen — bitte Anreise und Abreise prüfen.')
|
||||
->setParameter('{{ max }}', (string) self::MAX_NIGHTS)
|
||||
->atPath('dateTo')
|
||||
->addViolation();
|
||||
}
|
||||
}
|
||||
|
||||
public function getPaxCount(): int
|
||||
{
|
||||
return $this->paxCount;
|
||||
@@ -336,9 +378,17 @@ class AccommodationBooking implements BlameableEntityInterface, TimestampableEnt
|
||||
return $this->priceBreakdown;
|
||||
}
|
||||
|
||||
/** @param array<string, mixed> $priceBreakdown */
|
||||
/**
|
||||
* @param array<string, mixed> $priceBreakdown
|
||||
*
|
||||
* @throws ImplausibleBookingPriceException if the total is too large to be a real price
|
||||
*/
|
||||
public function setPriceSnapshot(array $priceBreakdown, int $totalPrice, string $currency, int $version): self
|
||||
{
|
||||
if ($totalPrice > self::MAX_TOTAL_PRICE) {
|
||||
throw new ImplausibleBookingPriceException($totalPrice, $this->paxCount, $this->getNights());
|
||||
}
|
||||
|
||||
$this->priceBreakdown = $priceBreakdown;
|
||||
$this->totalPrice = $totalPrice;
|
||||
$this->pricingCurrency = $currency;
|
||||
|
||||
Reference in new issue
Block a user