diff --git a/src/api/__tests__/client/page.test.ts b/src/api/__tests__/client/page.test.ts index 4205418..041e86d 100644 --- a/src/api/__tests__/client/page.test.ts +++ b/src/api/__tests__/client/page.test.ts @@ -13,6 +13,19 @@ describe("page helpers", () => { expect(pageQuery({ offset: 20 })).toBe("?offset=20"); expect(pageQuery({ limit: 10, offset: 20 })).toBe("?limit=10&offset=20"); }); + + it("formats the schedule window and omits undefined keys", () => { + expect(pageQuery({ ends_after: 1_700_000_000_000 })).toBe("?ends_after=1700000000000"); + expect(pageQuery({ starts_after: 1_700_000_000_000 })).toBe("?starts_after=1700000000000"); + expect(pageQuery({ limit: 50, ends_after: 1_700_000_000_000 })).toBe( + "?limit=50&ends_after=1700000000000" + ); + // Present-but-empty (`?ends_after=`) is a hard 400 server-side, so an + // undefined filter must drop the key, not serialize as a blank value. + expect(pageQuery({ limit: 50, ends_after: undefined, starts_after: undefined })).toBe( + "?limit=50" + ); + }); }); describe("appendPageParams", () => { diff --git a/src/api/__tests__/events/events.test.ts b/src/api/__tests__/events/events.test.ts index 731f54b..24e5d2c 100644 --- a/src/api/__tests__/events/events.test.ts +++ b/src/api/__tests__/events/events.test.ts @@ -29,6 +29,25 @@ describe("events API", () => { expect(init?.method).toBe("GET"); }); + it("getEvents sends the schedule window and drops undefined params", async () => { + mockFetchSuccess({ items: [{ id: "e1" }], total: 1 }); + + await getEvents({ ends_after: 1_700_000_000_000, limit: 50 }); + expect(lastFetchCall()[0]).toBe("/api/events?limit=50&ends_after=1700000000000"); + + mockFetchSuccess({ items: [], total: 0 }); + await getEvents({ starts_after: 1_700_000_000_000 }); + expect(lastFetchCall()[0]).toBe("/api/events?starts_after=1700000000000"); + + // A blank value (`?ends_after=`) is a hard 400 server-side, so an + // undefined filter must leave the key out of the query string entirely. + mockFetchSuccess({ items: [], total: 0 }); + await getEvents({ limit: 10, ends_after: undefined, starts_after: undefined }); + expect(lastFetchCall()[0]).toBe("/api/events?limit=10"); + expect(lastFetchCall()[0]).not.toContain("ends_after"); + expect(lastFetchCall()[0]).not.toContain("starts_after"); + }); + it("getEventById calls /events/:id", async () => { const mockEvent = { id: "e1", title: "Event" }; mockFetchSuccess(mockEvent); diff --git a/src/api/__tests__/exams/exams.test.ts b/src/api/__tests__/exams/exams.test.ts index 89fca13..091e7ad 100644 --- a/src/api/__tests__/exams/exams.test.ts +++ b/src/api/__tests__/exams/exams.test.ts @@ -27,6 +27,25 @@ describe("exams API - core", () => { expect(init?.method).toBe("GET"); }); + it("getExams sends the schedule window and drops undefined params", async () => { + mockFetchSuccess({ items: [{ id: "ex1" }], total: 1 }); + + await getExams({ ends_after: 1_700_000_000_000, limit: 50 }); + expect(lastFetchCall()[0]).toBe("/api/exams?limit=50&ends_after=1700000000000"); + + mockFetchSuccess({ items: [], total: 0 }); + await getExams({ starts_after: 1_700_000_000_000 }); + expect(lastFetchCall()[0]).toBe("/api/exams?starts_after=1700000000000"); + + // A blank value (`?ends_after=`) is a hard 400 server-side, so an + // undefined filter must leave the key out of the query string entirely. + mockFetchSuccess({ items: [], total: 0 }); + await getExams({ limit: 10, ends_after: undefined, starts_after: undefined }); + expect(lastFetchCall()[0]).toBe("/api/exams?limit=10"); + expect(lastFetchCall()[0]).not.toContain("ends_after"); + expect(lastFetchCall()[0]).not.toContain("starts_after"); + }); + it("getExamChoiceImageBlob calls /exams/:id/questions/:qid/choices/:choiceId/image", async () => { mockFetchBlob(new Blob(["img"], { type: "image/png" })); diff --git a/src/api/client/page.ts b/src/api/client/page.ts index 0d93d9e..dcef1cc 100644 --- a/src/api/client/page.ts +++ b/src/api/client/page.ts @@ -10,11 +10,28 @@ export type Page = { offset: number; }; -export function pageQuery(params?: PageParams): string { +/** + * Schedule-window filters, UTC unix ms. Only `/events` and `/exams` honour + * them; both are optional and AND-ed when both sent, and a row with no + * schedule at all is excluded by either. Sending either flips the server + * order to ASCENDING by schedule (soonest first) instead of newest-created. + */ +export type ScheduleWindowParams = { + /** Keeps rows that have not begun: `starts_at > T`. */ + starts_after?: number; + /** Keeps rows whose window has not finished: `ends_at > T`, else `starts_at > T`. */ + ends_after?: number; +}; + +export function pageQuery(params?: PageParams & ScheduleWindowParams): string { if (!params) return ""; const query = new URLSearchParams(); + // `!= null` is load-bearing: the API 400s on a present-but-empty value + // (`?limit=`), so an undefined param must drop the key, never emit `key=`. if (params.limit != null) query.set("limit", String(params.limit)); if (params.offset != null) query.set("offset", String(params.offset)); + if (params.starts_after != null) query.set("starts_after", String(params.starts_after)); + if (params.ends_after != null) query.set("ends_after", String(params.ends_after)); const value = query.toString(); return value ? `?${value}` : ""; } diff --git a/src/api/events/getEvents.ts b/src/api/events/getEvents.ts index 96f55ff..b58fc40 100644 --- a/src/api/events/getEvents.ts +++ b/src/api/events/getEvents.ts @@ -1,8 +1,17 @@ import { client } from "../client"; -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import { + normalizePage, + pageQuery, + type Page, + type PageParams, + type ScheduleWindowParams, +} from "../client"; import type { Event } from "../client"; -export async function getEvents(params?: PageParams, signal?: AbortSignal): Promise> { +export async function getEvents( + params?: PageParams & ScheduleWindowParams, + signal?: AbortSignal +): Promise> { const data = await client(`/events${pageQuery(params)}`, { signal }); return normalizePage(data); } diff --git a/src/api/exams/getExams.ts b/src/api/exams/getExams.ts index 73803e3..e991f65 100644 --- a/src/api/exams/getExams.ts +++ b/src/api/exams/getExams.ts @@ -1,8 +1,17 @@ import { client } from "../client"; -import { normalizePage, pageQuery, type Page, type PageParams } from "../client"; +import { + normalizePage, + pageQuery, + type Page, + type PageParams, + type ScheduleWindowParams, +} from "../client"; import type { Exam } from "../client"; -export async function getExams(params?: PageParams, signal?: AbortSignal): Promise> { +export async function getExams( + params?: PageParams & ScheduleWindowParams, + signal?: AbortSignal +): Promise> { const data = await client(`/exams${pageQuery(params)}`, { signal }); return normalizePage(data); } diff --git a/src/stores/shell-feed-context.tsx b/src/stores/shell-feed-context.tsx index 7235006..31e6382 100644 --- a/src/stores/shell-feed-context.tsx +++ b/src/stores/shell-feed-context.tsx @@ -88,18 +88,24 @@ function createUserFeed(loggedIn: boolean): UserFeed { { initialValue: emptyPage() } ); - // No limit on events/exams: the backend has no schedule filter or ordering - // param (`GET /events`, `GET /exams` take limit/offset only and serve - // ORDER BY id DESC = creation order), so any cap drops an event scheduled - // for tomorrow that was created before the newest N rows. The server loads - // the full list either way and only slices it afterwards. - // ponytail: payload grows with school history — upgrade path is a backend - // `starts_after`/`ends_after` query param, then cap again. + // `ends_after=now` keeps only rows whose window has not finished AND flips + // the server order to soonest-first, so a cap finally truncates the archive + // end instead of the upcoming end. Without it the whole school history rode + // along (~3.4 MB/poll at 5k+5k rows) only to be filtered out client-side. + // + // 50: the feed drives "upcoming" badges and the calendar dot — it needs the + // next handful, not the archive. 50 soonest-first rows cover a busy month + // per source. Both consumers already show only not-yet-ended items, so the + // dropped rows were never rendered. + // ponytail: a school with >50 simultaneously-open events loses the tail — + // upgrade path is narrowing the window (add an upper bound), not a bigger cap. + const upcoming = () => ({ ends_after: Date.now(), limit: 50 }); + const [eventsRes, { refetch: refetchEvents }] = createResource( source, async () => { try { - return await getEvents(); + return await getEvents(upcoming()); } catch { return emptyPage(); } @@ -111,7 +117,7 @@ function createUserFeed(loggedIn: boolean): UserFeed { source, async () => { try { - return await getExams(); + return await getExams(upcoming()); } catch { return emptyPage(); }