import type { LoaderFunctionArgs } from "react-router"; import { loadRegistrationPlanSelection } from "~/services/billing/registration-plan-selection.server"; const invalidQuantityResponse = () => Response.json({ errorKey: "account.register.recommendationErrorBody" }, { status: 400 }); function readSeatQuantity(searchParams: URLSearchParams, key: string, minimum: number): number { const raw = searchParams.get(key)?.trim() ?? ""; if (!/^\d{1,5}$/u.test(raw)) throw invalidQuantityResponse(); const value = Number(raw); if (!Number.isSafeInteger(value) || value < minimum || value > 10_000) { throw invalidQuantityResponse(); } return value; } export const loader = async ({ request }: LoaderFunctionArgs) => { const url = new URL(request.url); const officeSeatQuantity = readSeatQuantity(url.searchParams, "office", 1); const fieldSeatQuantity = readSeatQuantity(url.searchParams, "field", 0); try { const selection = await loadRegistrationPlanSelection(null, { officeSeatQuantity, fieldSeatQuantity }); return Response.json(selection, { headers: { "Cache-Control": "private, no-store", "X-Content-Type-Options": "nosniff", }, }); } catch (error) { if (error instanceof RangeError) throw invalidQuantityResponse(); throw error; } };