proxy: do not show calendar events when they are present in database

This commit is contained in:
2022-05-23 23:34:33 +02:00
parent 9dba558bef
commit 17778ab346
2 changed files with 75 additions and 1 deletions

View File

@@ -17,6 +17,7 @@ use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\MapCalendarToUser;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserHttpClient;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\OnBehalfOfUserTokenStorage;
use Chill\CalendarBundle\RemoteCalendar\Connector\MSGraph\RemoteEventConverter;
use Chill\CalendarBundle\Repository\CalendarRangeRepository;
use Chill\MainBundle\Entity\User;
use DateTimeImmutable;
use Psr\Log\LoggerInterface;
@@ -27,6 +28,8 @@ use Symfony\Contracts\HttpClient\Exception\ClientExceptionInterface;
class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
{
private CalendarRangeRepository $calendarRangeRepository;
private LoggerInterface $logger;
private MachineHttpClient $machineHttpClient;
@@ -42,6 +45,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
private OnBehalfOfUserHttpClient $userHttpClient;
public function __construct(
CalendarRangeRepository $calendarRangeRepository,
MachineHttpClient $machineHttpClient,
MapCalendarToUser $mapCalendarToUser,
LoggerInterface $logger,
@@ -50,6 +54,7 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
RemoteEventConverter $remoteEventConverter,
UrlGeneratorInterface $urlGenerator
) {
$this->calendarRangeRepository = $calendarRangeRepository;
$this->machineHttpClient = $machineHttpClient;
$this->mapCalendarToUser = $mapCalendarToUser;
$this->logger = $logger;
@@ -91,7 +96,21 @@ class MSGraphRemoteCalendarConnector implements RemoteCalendarConnectorInterface
]
)->toArray();
return array_map(function ($item) { return $this->remoteEventConverter->convertToRemote($item); }, $bareEvents['value']);
$ids = array_map(function ($item) { return $item['id']; }, $bareEvents['value']);
$existingIdsInRange = $this->calendarRangeRepository->findRemoteIdsPresent($ids);
return array_map(
function ($item) {
return $this->remoteEventConverter->convertToRemote($item);
},
// filter all event to keep only the one not in range
array_filter(
$bareEvents['value'],
function ($item) use ($existingIdsInRange) {
return (!$existingIdsInRange[$item['id']]) ?? true;
}
)
);
} catch (ClientExceptionInterface $e) {
if (403 === $e->getResponse()->getStatusCode()) {
return $this->getScheduleTimesForUser($user, $startDate, $endDate);