wip: api endpoint for calendar data to be consumed by cms

This commit is contained in:
Björn Fromme committed 2026-07-16 17:13:15 +02:00
1 parent b3210a7a27
commit 487597c828
3 files changed
+132 -24

No files matched your search

+6
View File
@@ -121,6 +121,12 @@ Authorization: Bearer {{$auth.token("oauth2_newsletter")}}
"listIds": [10321569,10321382,10321383]
}
### API contingent calendar
# @no-cookie-jar
GET {{base_url}}/api/contingents/calendar?hotelCode=DGS&dateFrom=2026-06-01&dateTo=2026-06-30
Accept: application/json
Authorization: Bearer {{$auth.token("oauth2_api")}}
### API pickups planning webhook
# @no-cookie-jar
POST {{base_url}}/api/pickups-planning
+101
View File
@@ -0,0 +1,101 @@
<?php
declare(strict_types=1);
namespace App\Controller\Api;
use App\BpnConnect\ContingentsClient;
use App\BpnConnect\Exception\BpnConnectException;
use App\Entity\Groups\AccommodationPrice;
use App\Repository\Groups\AccommodationPriceRepository;
use Psr\Cache\InvalidArgumentException;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\JsonResponse;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Attribute\Route;
use Symfony\Component\Security\Http\Attribute\IsGranted;
use Symfony\Contracts\Cache\CacheInterface;
use Symfony\Contracts\Cache\ItemInterface;
#[Route('/api')]
#[IsGranted('ROLE_OAUTH2_API')]
class ContingentController extends AbstractController
{
public function __construct(
private readonly ContingentsClient $contingentsClient,
private readonly AccommodationPriceRepository $priceRepository,
private readonly CacheInterface $cache,
) {
}
#[Route(path: '/contingents/calendar', name: 'api_contingents_calendar', methods: ['GET'])]
public function calendar(Request $request): JsonResponse
{
$hotelCode = $request->query->get('hotelCode');
$dateFromRaw = $request->query->get('dateFrom');
$dateToRaw = $request->query->get('dateTo');
if (!$hotelCode || !$dateFromRaw || !$dateToRaw) {
return $this->json(['error' => 'hotelCode, dateFrom and dateTo are required.'], Response::HTTP_BAD_REQUEST);
}
$dateFrom = \DateTimeImmutable::createFromFormat('Y-m-d', $dateFromRaw)?->setTime(0, 0);
$dateTo = \DateTimeImmutable::createFromFormat('Y-m-d', $dateToRaw)?->setTime(0, 0);
if (!$dateFrom || !$dateTo) {
return $this->json(['error' => 'dateFrom and dateTo must be in Y-m-d format.'], Response::HTTP_BAD_REQUEST);
}
try {
$cacheKey = sprintf('contingents_calendar_%s_%s_%s', $hotelCode, $dateFromRaw, $dateToRaw);
$calendar = $this->cache->get($cacheKey, function (ItemInterface $item) use ($hotelCode, $dateFromRaw, $dateToRaw) {
$item->expiresAfter(3600);
return $this->contingentsClient->getContingentCalendar($hotelCode, $dateFromRaw, $dateToRaw);
});
} catch (BpnConnectException|InvalidArgumentException $e) {
return $this->json(['error' => 'Failed to fetch contingent data.'], Response::HTTP_BAD_GATEWAY);
}
$prices = $this->priceRepository->findByHotelCodeAndDateRange($hotelCode, $dateFrom, $dateTo);
$data = array_map(
fn ($entry) => $this->enrichEntry($entry->date, $entry->status->value, $prices),
$calendar->data,
);
return $this->json($data);
}
/**
* @param AccommodationPrice[] $prices
* @return array{date: string, status: string, pricePerNight: float|null, pricePerNightOverride: float|null, includedPax: int|null}
*/
private function enrichEntry(string $date, string $status, array $prices): array
{
$day = (new \DateTimeImmutable($date))->setTime(0, 0);
$price = null;
foreach ($prices as $candidate) {
if ($candidate->getDateFrom() > $day || $candidate->getDateTo() < $day) {
continue;
}
if (null === $price || $candidate->getDateFrom() > $price->getDateFrom()) {
$price = $candidate;
}
}
return [
'date' => $date,
'status' => $status,
'pricePerNight' => null !== $price ? round($price->getPricePerNight() / 100, 2) : null,
'pricePerNightOverride' => null !== $price?->getPricePerNightOverride() ? round($price->getPricePerNightOverride() / 100, 2) : null,
'priceAdditionalPerson' => null !== $price ? round($price->getPriceAdditionalPerson() / 100, 2) : null,
'priceAdditionalPersonOverride' => null !== $price?->getPriceAdditionalPersonOverride() ? round($price->getPriceAdditionalPersonOverride() / 100, 2) : null,
'includedPax' => $price?->getIncludedPax(),
'minNights' => $price?->getMinNights(),
];
}
}
@@ -1,5 +1,7 @@
<?php
declare(strict_types=1);
namespace App\Repository\Groups;
use App\Entity\Groups\AccommodationPrice;
@@ -16,28 +18,27 @@ class AccommodationPriceRepository extends ServiceEntityRepository
parent::__construct($registry, AccommodationPrice::class);
}
// /**
// * @return AccommodationPrice[] Returns an array of AccommodationPrice objects
// */
// public function findByExampleField($value): array
// {
// return $this->createQueryBuilder('a')
// ->andWhere('a.exampleField = :val')
// ->setParameter('val', $value)
// ->orderBy('a.id', 'ASC')
// ->setMaxResults(10)
// ->getQuery()
// ->getResult()
// ;
// }
// public function findOneBySomeField($value): ?AccommodationPrice
// {
// return $this->createQueryBuilder('a')
// ->andWhere('a.exampleField = :val')
// ->setParameter('val', $value)
// ->getQuery()
// ->getOneOrNullResult()
// ;
// }
/**
* Returns all AccommodationPrice records whose period overlaps with [dateFrom, dateTo]
* for the accommodation identified by the given hotel code.
*
* @return AccommodationPrice[]
*/
public function findByHotelCodeAndDateRange(
string $hotelCode,
\DateTimeImmutable $dateFrom,
\DateTimeImmutable $dateTo,
): array {
return $this->createQueryBuilder('ap')
->join('ap.accommodation', 'a')
->where('a.code = :hotelCode')
->andWhere('ap.dateFrom <= :dateTo')
->andWhere('ap.dateTo >= :dateFrom')
->setParameter('hotelCode', $hotelCode)
->setParameter('dateFrom', $dateFrom)
->setParameter('dateTo', $dateTo)
->orderBy('ap.dateFrom', 'ASC')
->getQuery()
->getResult();
}
}