fix(notifications) : pagination réelle côté provider pour ne plus tronquer à 30 (LST-52)
NotificationProvider retournait findBy(..., 30) : limite codée en dur, paramètre page ignoré et tableau brut (pas un Paginator). hydra:totalItems valait donc 30 → fetchAllHydra s'arrêtait à la 1re page et les notifications restaient tronquées à 30 malgré le correctif front. - NotificationProvider : vraie pagination Doctrine (Pagination + DoctrinePaginator + TraversablePaginator), totalItems réel et hydra:view.next exposés - NotificationRepository : createUserNotificationsQueryBuilder (filtre user + tri) - fetchAllHydra : ne retronque plus silencieusement quand hydra:totalItems est absent, pagine jusqu'à une page non pleine
This commit is contained in:
+16
-2
@@ -33,13 +33,27 @@ export async function fetchAllHydra<T>(
|
|||||||
const first = await fetchPage(1)
|
const first = await fetchPage(1)
|
||||||
const all = extractHydraMembers(first)
|
const all = extractHydraMembers(first)
|
||||||
const total = extractHydraTotal(first)
|
const total = extractHydraTotal(first)
|
||||||
|
const pageSize = all.length
|
||||||
|
|
||||||
if (total === undefined) {
|
// 1ʳᵉ page vide → collection vide, rien de plus à récupérer.
|
||||||
|
if (pageSize === 0) {
|
||||||
return all
|
return all
|
||||||
}
|
}
|
||||||
|
|
||||||
let page = 2
|
let page = 2
|
||||||
while (all.length < total && page <= maxPages) {
|
while (page <= maxPages) {
|
||||||
|
if (total !== undefined) {
|
||||||
|
// Total connu : on s'arrête dès qu'on a tout récupéré.
|
||||||
|
if (all.length >= total) {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
} else if (all.length % pageSize !== 0) {
|
||||||
|
// Total inconnu (provider custom sans `hydra:totalItems`) : la dernière
|
||||||
|
// page récupérée n'était pas pleine → fin de collection. On ne s'arrête
|
||||||
|
// pas en silence sur la 1ʳᵉ page, contrairement à `extractHydraMembers`.
|
||||||
|
break
|
||||||
|
}
|
||||||
|
|
||||||
const next = await fetchPage(page)
|
const next = await fetchPage(page)
|
||||||
const members = extractHydraMembers(next)
|
const members = extractHydraMembers(next)
|
||||||
if (members.length === 0) {
|
if (members.length === 0) {
|
||||||
|
|||||||
@@ -7,7 +7,9 @@ namespace App\Repository;
|
|||||||
use App\Entity\Notification;
|
use App\Entity\Notification;
|
||||||
use App\Entity\User;
|
use App\Entity\User;
|
||||||
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
|
||||||
|
use Doctrine\ORM\QueryBuilder;
|
||||||
use Doctrine\Persistence\ManagerRegistry;
|
use Doctrine\Persistence\ManagerRegistry;
|
||||||
|
use Symfony\Component\Security\Core\User\UserInterface;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @extends ServiceEntityRepository<Notification>
|
* @extends ServiceEntityRepository<Notification>
|
||||||
@@ -19,6 +21,15 @@ class NotificationRepository extends ServiceEntityRepository
|
|||||||
parent::__construct($registry, Notification::class);
|
parent::__construct($registry, Notification::class);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function createUserNotificationsQueryBuilder(UserInterface $user): QueryBuilder
|
||||||
|
{
|
||||||
|
return $this->createQueryBuilder('n')
|
||||||
|
->where('n.user = :user')
|
||||||
|
->setParameter('user', $user)
|
||||||
|
->orderBy('n.createdAt', 'DESC')
|
||||||
|
;
|
||||||
|
}
|
||||||
|
|
||||||
public function countUnreadByUser(User $user): int
|
public function countUnreadByUser(User $user): int
|
||||||
{
|
{
|
||||||
return (int) $this->createQueryBuilder('n')
|
return (int) $this->createQueryBuilder('n')
|
||||||
|
|||||||
@@ -5,11 +5,17 @@ declare(strict_types=1);
|
|||||||
namespace App\State;
|
namespace App\State;
|
||||||
|
|
||||||
use ApiPlatform\Metadata\Operation;
|
use ApiPlatform\Metadata\Operation;
|
||||||
|
use ApiPlatform\State\Pagination\Pagination;
|
||||||
|
use ApiPlatform\State\Pagination\TraversablePaginator;
|
||||||
use ApiPlatform\State\ProviderInterface;
|
use ApiPlatform\State\ProviderInterface;
|
||||||
use App\Entity\Notification;
|
use App\Entity\Notification;
|
||||||
use App\Repository\NotificationRepository;
|
use App\Repository\NotificationRepository;
|
||||||
|
use ArrayIterator;
|
||||||
|
use Doctrine\ORM\Tools\Pagination\Paginator as DoctrinePaginator;
|
||||||
use Symfony\Bundle\SecurityBundle\Security;
|
use Symfony\Bundle\SecurityBundle\Security;
|
||||||
|
|
||||||
|
use function count;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @implements ProviderInterface<Notification>
|
* @implements ProviderInterface<Notification>
|
||||||
*/
|
*/
|
||||||
@@ -18,16 +24,28 @@ final readonly class NotificationProvider implements ProviderInterface
|
|||||||
public function __construct(
|
public function __construct(
|
||||||
private Security $security,
|
private Security $security,
|
||||||
private NotificationRepository $notificationRepository,
|
private NotificationRepository $notificationRepository,
|
||||||
|
private Pagination $pagination,
|
||||||
) {}
|
) {}
|
||||||
|
|
||||||
public function provide(Operation $operation, array $uriVariables = [], array $context = []): array|object
|
public function provide(Operation $operation, array $uriVariables = [], array $context = []): object
|
||||||
{
|
{
|
||||||
$user = $this->security->getUser();
|
$user = $this->security->getUser();
|
||||||
|
|
||||||
return $this->notificationRepository->findBy(
|
[$page, $offset, $limit] = $this->pagination->getPagination($operation, $context);
|
||||||
['user' => $user],
|
|
||||||
['createdAt' => 'DESC'],
|
$queryBuilder = $this->notificationRepository
|
||||||
30,
|
->createUserNotificationsQueryBuilder($user)
|
||||||
|
->setFirstResult($offset)
|
||||||
|
->setMaxResults($limit)
|
||||||
|
;
|
||||||
|
|
||||||
|
$doctrinePaginator = new DoctrinePaginator($queryBuilder);
|
||||||
|
|
||||||
|
return new TraversablePaginator(
|
||||||
|
new ArrayIterator(iterator_to_array($doctrinePaginator, false)),
|
||||||
|
$page,
|
||||||
|
$limit,
|
||||||
|
(float) count($doctrinePaginator),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user