fix(api): one helper for list pages, exact cut signal, total order

listPage (src/lib/http/list-query.ts) reads one row past the limit and
cuts it back, on top of the existing list-cap signal rather than beside
it. Every capped list now gets its page there, and every read passed to
it orders by something ending in the row id, so rows that share a sort
value cannot repeat or vanish at a page boundary.

- practitioners, vaccinations and encounters were cut at their take
  with no signal; they now answer meta.truncated and accept offset, and
  their web readers walk the pages instead of stopping at the first
- symptoms/events reported hasMore on a page that was exactly full; it
  is now true only when a row past the page exists
- the document list's hand-rolled limit + 1 goes through the helper
- security-activity answers an out-of-range limit with the 422 its
  published schema already promised instead of clamping it

list-page-guard binds every apiListSuccess route to listPage, forbids
hand-cut lists and length-derived hasMore, and requires the id
tiebreaker in every listPage read.
This commit is contained in:
Marc-André Bombeck committed 2026-10-20 13:29:14 +02:00
1 parent 2265763160
commit 8db7c71b0e
35 files changed
+1024 -394

No files matched your search

+29 -9
View File
@@ -19,7 +19,13 @@ import {
useQueryClient,
} from "@tanstack/react-query";
import { apiDelete, apiGet, apiPatch, apiPost } from "@/lib/api/api-fetch";
import {
apiDelete,
apiGet,
apiGetList,
apiPatch,
apiPost,
} from "@/lib/api/api-fetch";
import { invalidateReminderReads } from "@/hooks/use-measurement-reminders";
import {
encounterDependentKeys,
@@ -53,6 +59,7 @@ export interface EncounterWriteBody {
}
const BASE = "/api/encounters";
const ENCOUNTER_PAGE_SIZE = 200;
export function useEncounters(
enabled = true,
@@ -73,14 +80,27 @@ export function useEncounters(
window?.status,
window?.episodeId,
),
queryFn: () => {
const sp = new URLSearchParams();
if (from) sp.set("from", from);
if (to) sp.set("to", to);
if (window?.status) sp.set("status", window.status);
if (window?.episodeId) sp.set("episodeId", window.episodeId);
const qs = sp.toString();
return apiGet<EncounterList>(qs ? `${BASE}?${qs}` : BASE);
// `limit` and `offset` apply to both arms and the route says whether
// either has rows past the page, so this reads to the end: a visit list
// that stopped at the first page showed an old visit as missing.
queryFn: async () => {
const list: EncounterList = { upcoming: [], past: [] };
for (let offset = 0; ; offset += ENCOUNTER_PAGE_SIZE) {
const sp = new URLSearchParams({
limit: String(ENCOUNTER_PAGE_SIZE),
offset: String(offset),
});
if (from) sp.set("from", from);
if (to) sp.set("to", to);
if (window?.status) sp.set("status", window.status);
if (window?.episodeId) sp.set("episodeId", window.episodeId);
const { data: page, cap } = await apiGetList<EncounterList>(
`/api/encounters?${sp.toString()}`,
);
list.upcoming.push(...page.upcoming);
list.past.push(...page.past);
if (!cap?.truncated) return list;
}
},
enabled,
});
+19 -5
View File
@@ -10,7 +10,7 @@
*/
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import { apiDelete, apiGet, apiPatch, apiPost } from "@/lib/api/api-fetch";
import { apiDelete, apiGetList, apiPatch, apiPost } from "@/lib/api/api-fetch";
import {
encounterDependentKeys,
invalidateKeys,
@@ -31,15 +31,29 @@ export interface PractitionerWriteBody {
}
const BASE = "/api/practitioners";
const PRACTITIONER_PAGE_SIZE = 200;
export function usePractitioners(q?: string, enabled = true) {
const term = q?.trim() || undefined;
return useQuery({
queryKey: queryKeys.practitionerList(term),
queryFn: () =>
apiGet<Practitioner[]>(
term ? `${BASE}?q=${encodeURIComponent(term)}` : BASE,
),
// The route pages and says whether another page exists; the pickers and
// the address book read this as the whole list, so it walks to the end.
queryFn: async () => {
const all: Practitioner[] = [];
for (let offset = 0; ; offset += PRACTITIONER_PAGE_SIZE) {
const sp = new URLSearchParams({
limit: String(PRACTITIONER_PAGE_SIZE),
offset: String(offset),
});
if (term) sp.set("q", term);
const { data: page, cap } = await apiGetList<Practitioner[]>(
`/api/practitioners?${sp.toString()}`,
);
all.push(...page);
if (!cap?.truncated) return all;
}
},
enabled,
});
}