fix(pagination) : éviter la troncature silencieuse des collections paginées (LST-52)
API Platform pagine par défaut à 30 éléments/page et le helper front extractHydraMembers ne lit que la première page (il ignore hydra:view.next), ce qui tronque silencieusement toute liste de plus de 30 éléments. - Back : paginationEnabled false sur les GetCollection consommées en entier et à volume borné/modéré (Client, Project, User, TaskTag, TaskGroup, TaskStatus, TaskPriority, TaskEffort, Workflow). - Front : nouveau helper fetchAllHydra() qui parcourt toutes les pages ; utilisé pour /notifications (volume non borné, reste paginé côté back). - Doc : règle anti-troncature ajoutée au CLAUDE.md. Déjà protégés (vérifiés) : Task, TimeEntry, TaskDocument, TaskRecurrence, AbsenceRequest/Policy/Balance (paginationEnabled false) et /time_entries/range.
This commit is contained in:
@@ -95,6 +95,7 @@ Exemples : `feat : add login page`, `fix(auth) : prevent null token crash`
|
||||
- Traductions dans `frontend/i18n/locales/` (le module résout `langDir` depuis `i18n/`)
|
||||
- 4 espaces d'indentation
|
||||
- MalioSelect : options `{ label: string, value: string | number | null }` — accepte les valeurs **string** (enums string OK, ex `category`/`StatusCategory`), pas seulement `number` (vérifié dans la source `Select.vue` : `modelValue: string | number | null`). L'option vide `null` n'est ajoutée que si `empty-option-label` est passé (ne pas le passer pour un champ requis). Largeur via `group-class` (pas de prop `minWidth`/`min-width`). ⚠️ Le `COMPONENTS.md` de la lib est inexact sur ce composant (il indique une clé `text` et une prop `minWidth` inexistantes) : la clé d'affichage réelle est `label`. Ne jamais modifier la lib `malio-layer-ui` depuis ce projet.
|
||||
- **Pagination API Platform & `extractHydraMembers` (piège de troncature)** : API Platform pagine par défaut à **30 éléments/page**. Le helper `extractHydraMembers()` (`frontend/utils/api.ts`) ne lit **que la première page** (il ignore `hydra:view.next`) → toute liste > 30 éléments est tronquée **silencieusement** (bug LST-51/LST-52). Règle : toute collection consommée via `extractHydraMembers` doit **soit** être servie par une ressource non paginée (`paginationEnabled: false` sur le `GetCollection`, quand le volume est borné/modéré et qu'on veut tout afficher — c'est le cas des référentiels et de Client/Project/User/Task/TimeEntry), **soit** gérer explicitement la pagination via le helper `fetchAllHydra()` (suit toutes les pages, à réserver aux volumes non bornés comme `/notifications`), **soit** passer par une route dédiée bornée (ex `/time_entries/range`). Ne **jamais** lire une seule page d'une collection potentiellement > 30 éléments.
|
||||
|
||||
### Composants UI
|
||||
|
||||
|
||||
@@ -1,13 +1,16 @@
|
||||
import type { Notification } from './dto/notification'
|
||||
import type { HydraCollection } from '~/utils/api'
|
||||
import { extractHydraMembers } from '~/utils/api'
|
||||
import { fetchAllHydra } from '~/utils/api'
|
||||
|
||||
export function useNotificationService() {
|
||||
const api = useApi()
|
||||
|
||||
async function getAll(): Promise<Notification[]> {
|
||||
const data = await api.get<HydraCollection<Notification>>('/notifications')
|
||||
return extractHydraMembers(data)
|
||||
// La ressource /notifications reste paginée côté back (volume non borné) :
|
||||
// on suit toutes les pages pour ne pas tronquer la liste à 30 éléments.
|
||||
return fetchAllHydra<Notification>(page =>
|
||||
api.get<HydraCollection<Notification>>('/notifications', { page }),
|
||||
)
|
||||
}
|
||||
|
||||
async function markAsRead(id: number): Promise<void> {
|
||||
|
||||
@@ -8,3 +8,46 @@ export type HydraCollection<T> = {
|
||||
export function extractHydraMembers<T>(response: HydraCollection<T>): T[] {
|
||||
return response['hydra:member'] ?? response['member'] ?? []
|
||||
}
|
||||
|
||||
function extractHydraTotal<T>(response: HydraCollection<T>): number | undefined {
|
||||
return response['hydra:totalItems'] ?? response['totalItems']
|
||||
}
|
||||
|
||||
/**
|
||||
* Récupère TOUS les éléments d'une collection Hydra paginée en parcourant les pages.
|
||||
*
|
||||
* `extractHydraMembers` ne lit que la première page (30 éléments par défaut côté
|
||||
* API Platform) : toute liste plus longue est tronquée silencieusement. Utiliser
|
||||
* ce helper dès qu'une collection potentiellement > 30 éléments doit être
|
||||
* affichée en entier alors que sa ressource back reste paginée.
|
||||
*
|
||||
* `fetchPage` reçoit le numéro de page (1-indexé) et doit renvoyer la collection
|
||||
* Hydra correspondante (passer `page` en query param de l'appel API).
|
||||
*
|
||||
* @param maxPages garde-fou anti-boucle infinie (par défaut 1000 pages).
|
||||
*/
|
||||
export async function fetchAllHydra<T>(
|
||||
fetchPage: (page: number) => Promise<HydraCollection<T>>,
|
||||
maxPages = 1000,
|
||||
): Promise<T[]> {
|
||||
const first = await fetchPage(1)
|
||||
const all = extractHydraMembers(first)
|
||||
const total = extractHydraTotal(first)
|
||||
|
||||
if (total === undefined) {
|
||||
return all
|
||||
}
|
||||
|
||||
let page = 2
|
||||
while (all.length < total && page <= maxPages) {
|
||||
const next = await fetchPage(page)
|
||||
const members = extractHydraMembers(next)
|
||||
if (members.length === 0) {
|
||||
break
|
||||
}
|
||||
all.push(...members)
|
||||
page += 1
|
||||
}
|
||||
|
||||
return all
|
||||
}
|
||||
|
||||
@@ -18,7 +18,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
@@ -25,7 +25,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(
|
||||
security: "is_granted('ROLE_ADMIN')",
|
||||
|
||||
@@ -16,7 +16,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
@@ -19,7 +19,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
@@ -16,7 +16,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
@@ -18,7 +18,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
@@ -16,7 +16,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
@@ -33,6 +33,7 @@ use Symfony\Component\Serializer\Attribute\Groups;
|
||||
normalizationContext: ['groups' => ['user:list']],
|
||||
),
|
||||
new GetCollection(
|
||||
paginationEnabled: false,
|
||||
normalizationContext: ['groups' => ['user:list']],
|
||||
),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')", processor: UserPasswordHasherProcessor::class),
|
||||
|
||||
@@ -21,7 +21,7 @@ use Symfony\Component\Validator\Constraints as Assert;
|
||||
|
||||
#[ApiResource(
|
||||
operations: [
|
||||
new GetCollection(security: "is_granted('ROLE_USER')"),
|
||||
new GetCollection(paginationEnabled: false, security: "is_granted('ROLE_USER')"),
|
||||
new Get(security: "is_granted('ROLE_USER')"),
|
||||
new Post(security: "is_granted('ROLE_ADMIN')"),
|
||||
new Patch(security: "is_granted('ROLE_ADMIN')"),
|
||||
|
||||
Reference in New Issue
Block a user