import type { LoaderFunctionArgs } from "@remix-run/node"; import type { Method } from "@prisma/client"; import { authenticate } from "../shopify.server"; import { resolveAvailabilityRequest } from "../services/availability-request.server"; // Checkout UI Extension endpoint (extensions/checkout-datetime, Plus-only // native picker) — session-token authenticated, CORS-enabled. Calls the // exact same resolveAvailabilityRequest() as the storefront and POS // routes — CLAUDE.md: "the same capacity pool feeds every surface... // behavior must never diverge between channels." const VALID_METHODS = new Set(["SHIPPING", "LOCAL_DELIVERY", "PICKUP"]); export const loader = async ({ request }: LoaderFunctionArgs) => { const { sessionToken, cors } = await authenticate.public.checkout(request); const shopDomain = sessionToken.dest.replace(/^https?:\/\//, ""); const url = new URL(request.url); const methodParam = url.searchParams.get("method"); if (!methodParam || !VALID_METHODS.has(methodParam as Method)) { return cors(Response.json({ error: "Invalid or missing method" }, { status: 400 })); } const result = await resolveAvailabilityRequest(shopDomain, { method: methodParam as Method, locationId: url.searchParams.get("locationId") || undefined, postalCode: url.searchParams.get("postalCode") || undefined, address: url.searchParams.get("address") || undefined, days: Number(url.searchParams.get("days")) || undefined, }); return cors(Response.json(result)); };