wip: api endpoint for calendar data to be consumed by cms
This commit is contained in:
1 parent
0b4172bb6f
commit
2871b3c2bd
3 files changed
+132
-24
No files matched your search
@@ -121,6 +121,12 @@ Authorization: Bearer {{$auth.token("oauth2_newsletter")}}
|
|||||||
"listIds": [10321569,10321382,10321383]
|
"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
|
### API pickups planning webhook
|
||||||
# @no-cookie-jar
|
# @no-cookie-jar
|
||||||
POST {{base_url}}/api/pickups-planning
|
POST {{base_url}}/api/pickups-planning
|
||||||
|
|||||||
@@ -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
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
namespace App\Repository\Groups;
|
namespace App\Repository\Groups;
|
||||||
|
|
||||||
use App\Entity\Groups\AccommodationPrice;
|
use App\Entity\Groups\AccommodationPrice;
|
||||||
@@ -16,28 +18,27 @@ class AccommodationPriceRepository extends ServiceEntityRepository
|
|||||||
parent::__construct($registry, AccommodationPrice::class);
|
parent::__construct($registry, AccommodationPrice::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
// /**
|
/**
|
||||||
// * @return AccommodationPrice[] Returns an array of AccommodationPrice objects
|
* Returns all AccommodationPrice records whose period overlaps with [dateFrom, dateTo]
|
||||||
// */
|
* for the accommodation identified by the given hotel code.
|
||||||
// public function findByExampleField($value): array
|
*
|
||||||
// {
|
* @return AccommodationPrice[]
|
||||||
// return $this->createQueryBuilder('a')
|
*/
|
||||||
// ->andWhere('a.exampleField = :val')
|
public function findByHotelCodeAndDateRange(
|
||||||
// ->setParameter('val', $value)
|
string $hotelCode,
|
||||||
// ->orderBy('a.id', 'ASC')
|
\DateTimeImmutable $dateFrom,
|
||||||
// ->setMaxResults(10)
|
\DateTimeImmutable $dateTo,
|
||||||
// ->getQuery()
|
): array {
|
||||||
// ->getResult()
|
return $this->createQueryBuilder('ap')
|
||||||
// ;
|
->join('ap.accommodation', 'a')
|
||||||
// }
|
->where('a.code = :hotelCode')
|
||||||
|
->andWhere('ap.dateFrom <= :dateTo')
|
||||||
// public function findOneBySomeField($value): ?AccommodationPrice
|
->andWhere('ap.dateTo >= :dateFrom')
|
||||||
// {
|
->setParameter('hotelCode', $hotelCode)
|
||||||
// return $this->createQueryBuilder('a')
|
->setParameter('dateFrom', $dateFrom)
|
||||||
// ->andWhere('a.exampleField = :val')
|
->setParameter('dateTo', $dateTo)
|
||||||
// ->setParameter('val', $value)
|
->orderBy('ap.dateFrom', 'ASC')
|
||||||
// ->getQuery()
|
->getQuery()
|
||||||
// ->getOneOrNullResult()
|
->getResult();
|
||||||
// ;
|
}
|
||||||
// }
|
|
||||||
}
|
}
|
||||||
Reference in new issue
Block a user