mirror of
https://gitlab.com/Chill-Projet/chill-bundles.git
synced 2025-11-20 10:57:47 +00:00
Update calendar with accepted invites
This commit is contained in:
@@ -12,6 +12,7 @@ declare(strict_types=1);
|
||||
namespace Chill\CalendarBundle\Controller;
|
||||
|
||||
use Chill\CalendarBundle\Repository\CalendarRepository;
|
||||
use Chill\CalendarBundle\Repository\InviteRepository;
|
||||
use Chill\MainBundle\CRUD\Controller\ApiController;
|
||||
use Chill\MainBundle\Entity\User;
|
||||
use Chill\MainBundle\Serializer\Model\Collection;
|
||||
@@ -23,7 +24,10 @@ use Symfony\Component\Routing\Annotation\Route;
|
||||
|
||||
class CalendarAPIController extends ApiController
|
||||
{
|
||||
public function __construct(private readonly CalendarRepository $calendarRepository) {}
|
||||
public function __construct(
|
||||
private readonly CalendarRepository $calendarRepository,
|
||||
private readonly InviteRepository $inviteRepository,
|
||||
) {}
|
||||
|
||||
#[Route(path: '/api/1.0/calendar/calendar/by-user/{id}.{_format}', name: 'chill_api_single_calendar_list_by-user', requirements: ['_format' => 'json'])]
|
||||
public function listByUser(User $user, Request $request, string $_format): JsonResponse
|
||||
@@ -52,16 +56,37 @@ class CalendarAPIController extends ApiController
|
||||
throw new BadRequestHttpException('dateTo not parsable');
|
||||
}
|
||||
|
||||
$total = $this->calendarRepository->countByUser($user, $dateFrom, $dateTo);
|
||||
$paginator = $this->getPaginatorFactory()->create($total);
|
||||
$ranges = $this->calendarRepository->findByUser(
|
||||
// Get calendar items where user is the main user
|
||||
$ownCalendars = $this->calendarRepository->findByUser(
|
||||
$user,
|
||||
$dateFrom,
|
||||
$dateTo,
|
||||
$paginator->getItemsPerPage(),
|
||||
$paginator->getCurrentPageFirstItemNumber()
|
||||
$dateTo
|
||||
);
|
||||
|
||||
// Get calendar items from accepted invites
|
||||
$acceptedInvites = $this->inviteRepository->findAcceptedInvitesByUserAndDateRange($user, $dateFrom, $dateTo);
|
||||
$inviteCalendars = array_map(fn ($invite) => $invite->getCalendar(), $acceptedInvites);
|
||||
|
||||
// Merge
|
||||
$allCalendars = array_merge($ownCalendars, $inviteCalendars);
|
||||
$uniqueCalendars = [];
|
||||
$seenIds = [];
|
||||
|
||||
foreach ($allCalendars as $calendar) {
|
||||
$id = $calendar->getId();
|
||||
if (!in_array($id, $seenIds, true)) {
|
||||
$seenIds[] = $id;
|
||||
$uniqueCalendars[] = $calendar;
|
||||
}
|
||||
}
|
||||
|
||||
$total = count($uniqueCalendars);
|
||||
$paginator = $this->getPaginatorFactory()->create($total);
|
||||
|
||||
$offset = $paginator->getCurrentPageFirstItemNumber();
|
||||
$limit = $paginator->getItemsPerPage();
|
||||
$ranges = array_slice($uniqueCalendars, $offset, $limit);
|
||||
|
||||
$collection = new Collection($ranges, $paginator);
|
||||
|
||||
return $this->json($collection, Response::HTTP_OK, [], ['groups' => ['calendar:light']]);
|
||||
|
||||
Reference in New Issue
Block a user