diff --git a/public/typo3conf/ext/ep_products/Classes/BpnConnect/ApiClient.php b/public/typo3conf/ext/ep_products/Classes/BpnConnect/ApiClient.php index 4b674f59..5baf99b3 100644 --- a/public/typo3conf/ext/ep_products/Classes/BpnConnect/ApiClient.php +++ b/public/typo3conf/ext/ep_products/Classes/BpnConnect/ApiClient.php @@ -13,14 +13,27 @@ use TYPO3\CMS\Core\Utility\GeneralUtility; class ApiClient { - public function getCalendar($hotelCode, \DateTimeInterface $dateFrom, \DateTimeInterface $dateTo): array - { + public const CALENDAR_MODE_DAYS = 'days'; + public const CALENDAR_MODE_RANGES = 'ranges'; + + public function getCalendar( + $hotelCode, + \DateTimeInterface $dateFrom, + \DateTimeInterface $dateTo = null, + string $mode = self::CALENDAR_MODE_DAYS + ): array { + $query = [ + 'hotelCode' => $hotelCode, + 'dateFrom' => $dateFrom->format('Y-m-d'), + 'mode' => $mode, + ]; + + if (null !== $dateTo) { + $query['dateTo'] = $dateTo->format('Y-m-d'); + } + $response = $this->request('GET', 'api/v1/contingents/calendar', [ - 'query' => [ - 'hotelCode' => $hotelCode, - 'dateFrom' => $dateFrom->format('Y-m-d'), - 'dateTo' => $dateTo->format('Y-m-d'), - ], + 'query' => $query, ]); $this->assertStatusCode($response, [Response::HTTP_OK], 'GET', 'api/v1/contingents/calendar'); diff --git a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php index af9401d1..fb60965b 100644 --- a/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php +++ b/public/typo3conf/ext/ep_products/Classes/Controller/AjaxCalendarController.php @@ -80,7 +80,12 @@ class AjaxCalendarController extends ActionController $calendar['error'] = true; } else { try { - $cacheIdentifier = $this->getCacheIdentifier($hotelCode, $apiDateFrom, $apiDateTo); + $cacheIdentifier = $this->getCacheIdentifier( + $hotelCode, + $apiDateFrom, + $apiDateTo, + ApiClient::CALENDAR_MODE_DAYS + ); $cachedCalendar = $this->cache->get($cacheIdentifier); if (false !== $cachedCalendar) { @@ -89,7 +94,8 @@ class AjaxCalendarController extends ActionController $calendar = $this->apiClient->getCalendar( $hotelCode, new \DateTimeImmutable($apiDateFrom), - new \DateTimeImmutable($apiDateTo) + new \DateTimeImmutable($apiDateTo), + ApiClient::CALENDAR_MODE_DAYS ); $this->cache->set($cacheIdentifier, $calendar, [], self::CACHE_TTL); } @@ -158,12 +164,13 @@ class AjaxCalendarController extends ActionController ]; } - private function getCacheIdentifier(string $hotelCode, string $dateFrom, string $dateTo): string + private function getCacheIdentifier(string $hotelCode, string $dateFrom, string $dateTo, string $mode): string { return 'bpn_connect_calendar_' . sha1(implode('|', [ $hotelCode, $dateFrom, $dateTo, + $mode, ])); } diff --git a/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php b/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php index 869c3af6..324f1958 100644 --- a/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php +++ b/public/typo3conf/ext/ep_products/Classes/Service/IcalService.php @@ -34,7 +34,6 @@ use TYPO3\CMS\Core\SingletonInterface; class IcalService implements SingletonInterface { - private const FEED_HORIZON = 'P1Y'; private const SUMMARY_UNAVAILABLE = 'Unavailable'; /** @@ -66,10 +65,14 @@ class IcalService implements SingletonInterface if ($hotelCode !== '') { $dateFrom = new \DateTimeImmutable('today'); - $dateTo = $dateFrom->add(new \DateInterval(self::FEED_HORIZON))->sub(new \DateInterval('P1D')); try { - $calendar = $this->apiClient->getCalendar($hotelCode, $dateFrom, $dateTo); + $calendar = $this->apiClient->getCalendar( + $hotelCode, + $dateFrom, + null, + ApiClient::CALENDAR_MODE_RANGES + ); $calendarRows = isset($calendar['data']) && is_array($calendar['data']) ? $calendar['data'] : []; } catch (\Throwable $e) { $calendarRows = []; @@ -92,41 +95,28 @@ class IcalService implements SingletonInterface protected function generateUnavailableItems(array $calendarRows): void { usort($calendarRows, function (array $left, array $right): int { - return strcmp((string) ($left['date'] ?? ''), (string) ($right['date'] ?? '')); + return strcmp((string) ($left['date_from'] ?? ''), (string) ($right['date_from'] ?? '')); }); - $blockStart = null; - $lastDate = null; - foreach ($calendarRows as $row) { - if (!isset($row['date'])) { + if (!isset($row['date_from'], $row['date_to'])) { continue; } - $date = new \DateTimeImmutable($row['date']); - $lastDate = $date; - - if ($this->isUnavailableStatus($row['status'] ?? 'OK')) { - if (null === $blockStart) { - $blockStart = $date; - } - + if (!$this->isUnavailableStatus($row['status'] ?? 'OK')) { continue; } - if (null !== $blockStart) { - $this->addBlockedItem([ - 'from' => $blockStart, - 'to' => $date, - ]); - $blockStart = null; + $dateFrom = new \DateTimeImmutable($row['date_from']); + $dateTo = new \DateTimeImmutable($row['date_to']); + + if ($dateTo < $dateFrom) { + continue; } - } - if (null !== $blockStart && null !== $lastDate) { $this->addBlockedItem([ - 'from' => $blockStart, - 'to' => $lastDate->add(new \DateInterval('P1D')), + 'from' => $dateFrom, + 'to' => $dateTo->add(new \DateInterval('P1D')), ]); } }