Booking - Small improvements and fixes

This commit is contained in:
2026-01-13 15:01:30 +01:00
parent 52a4997497
commit 3bb042462b
3 changed files with 38 additions and 36 deletions
+36 -33
View File
@@ -15,6 +15,7 @@ use App\Request\BookingDetailsRequest;
use App\Response\BookingCreateResponse;
use App\Service\ConfigVariableService;
use App\Service\PricesService;
use Doctrine\DBAL\TransactionIsolationLevel;
use Doctrine\ORM\EntityManagerInterface;
use Nelmio\ApiDocBundle\Attribute\Model;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
@@ -90,18 +91,39 @@ class BookingController extends AbstractController
#[MapRequestPayload()] BookingCreateRequest $createRequest
): JsonResponse {
$booking = new Booking();
try {
if ($createRequest->arrivalDate >= $createRequest->departureDate) {
if ($createRequest->arrivalDate >= $createRequest->departureDate) {
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
'Arrival cant be before departure',
);
}
if (\count($createRequest->rooms) == 0) {
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
'Booking must contain rooms',
);
}
$maxAllowedGuestNumber = $config->getVariableFromConst(ConfigVariableConsts::CONFIG_API_NAXGUESTS)?->getValue() ?? 0;
if ($maxAllowedGuestNumber > 0) {
$totalGuestNumber = array_reduce($createRequest->rooms, function ($carry, $item) {
return $carry += $item->guestNumber;
}, 0);
if ($totalGuestNumber > $maxAllowedGuestNumber) {
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_INVALID_DATE),
'Arrival cant be before departure',
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
'Max allowed guest number exceeded. Contact the company to complete the booking',
);
}
}
$entityManager->beginTransaction();
$currentTime = new \DateTimeImmutable("now");
$booking = new Booking();
try {
$currentTime = new \DateTime("now");
$entityManager->beginTransaction(TransactionIsolationLevel::REPEATABLE_READ);
// Create booking from request data.
$booking->setBookingTime($currentTime);
@@ -120,27 +142,6 @@ class BookingController extends AbstractController
$entityManager->persist($booking);
if (\count($createRequest->rooms) == 0) {
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
'Booking must contain rooms',
);
}
$maxAllowedGuestNumber = $config->getVariableFromConst(ConfigVariableConsts::CONFIG_API_NAXGUESTS)?->getValue() ?? 0;
if ($maxAllowedGuestNumber > 0) {
$totalGuestNumber = array_reduce($createRequest->rooms, function ($carry, $item) {
return $carry += $item->guestNumber;
}, 0);
if ($totalGuestNumber > $maxAllowedGuestNumber) {
throw new ApiErrorException(
new ApiErrorMessage(422, ApiErrorMessage::TYPE_VALIDATION_ERROR),
'Max allowed guest number exceeded. Contact the company to complete the booking',
);
}
}
// Create each room.
foreach ($createRequest->rooms as $roomItem) {
$room = new BookingRoom();
@@ -174,9 +175,9 @@ class BookingController extends AbstractController
//Get Price for period.
$priceList = $priceSrv->getFinalPricesForSearch($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate);
$priceFound = false;
foreach ($priceList as $key => $value) {
if ($key == $selectedGuestAmount) {
$room->setRoomPrice($value);
foreach ($priceList as $priceItem) {
if ($priceItem->guestNumber == $selectedGuestAmount) {
$room->setRoomPrice($priceItem->price);
$priceFound = true;
break;
}
@@ -195,10 +196,12 @@ class BookingController extends AbstractController
$repoAvailability->updateAvailabilityForRoomCategoryOnPeriod($roomCategory, $createRequest->arrivalDate, $createRequest->departureDate, $selectedAmount, true); //Update using "diff" mode.
}
$entityManager->commit();
$entityManager->flush();
$entityManager->commit();
} catch (\Throwable $th) {
$entityManager->rollback();
if ($entityManager->getConnection()->isTransactionActive()) {
$entityManager->rollback();
}
throw $th;
}
@@ -106,6 +106,7 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
$strEndDate = $endDate->format('Y-m-d');
$roomCatId = $roomCategory->getId();
/** @var RoomAvailability[] */
$availabilityItemList = $this->createQueryBuilder('ra')
->andWhere('ra.day >= :startDate')
->andWhere('ra.day < :endDate')
@@ -116,8 +117,6 @@ class RoomAvailabilityRepository extends ServiceEntityRepository
->getQuery()
->getResult();
$em = $this->getEntityManager();
foreach ($availabilityItemList as $item) {
if ($isDiff) {
$item->modNumAvailable($amount);
+1 -1
View File
@@ -43,7 +43,7 @@ class PricesService
*
* e. g. --> [ 2 => 120€, 3 => 180€ ]
*
* @return array Contiene los precios en función del número de personas.
* @return SearchPricesResponsePriceItem[] Contiene los precios en función del número de personas.
*/
public function getFinalPricesForSearch(RoomCategory $category, \DateTimeInterface $startDate, \DateTimeInterface $endDate): array
{