From 132e05a71a484ff09a9592709e649e8c73d80a4e Mon Sep 17 00:00:00 2001 From: Js41637 Date: Mon, 30 Oct 2023 15:00:08 +1100 Subject: [PATCH] fix: include_past and page query params on events api --- src/routes/api/events/+server.ts | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/src/routes/api/events/+server.ts b/src/routes/api/events/+server.ts index 94bb593..92ae312 100644 --- a/src/routes/api/events/+server.ts +++ b/src/routes/api/events/+server.ts @@ -7,8 +7,10 @@ import { SUPABASE_TABLE_NAME } from '$env/static/private' export const GET: RequestHandler = async (request) => { const { supabaseClient } = await getSupabase(request); - const { params, setHeaders } = request; - const pageNum = Number.parseInt(params["page"], 10) || 0; + const { url: { searchParams }, setHeaders } = request; + + let pageNum = Number.parseInt(searchParams.get("page"), 10) || 1; + pageNum = pageNum < 1 ? 1 : pageNum; const DATES_PAGE_SIZE = 25; @@ -17,9 +19,10 @@ export const GET: RequestHandler = async (request) => { .order("priority", {ascending: false}) .order("date", {ascending: true}) - if (!params["include_past"]) { query = query.or(`date.gte.${formatISO(new Date())},end_date.gte.${formatISO(new Date())},date.is.null`) } + const includePast = searchParams.get("include_past") === "true"; + if (!includePast) { query = query.or(`date.gte.${formatISO(new Date())},end_date.gte.${formatISO(new Date())},date.is.null`) } - query = query.range(pageNum * DATES_PAGE_SIZE, ((pageNum + 1) * DATES_PAGE_SIZE) - 1); + query = query.range((pageNum - 1) * DATES_PAGE_SIZE, (pageNum * DATES_PAGE_SIZE) - 1); const { data, error: err } = await query;