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"; // POS UI Extension endpoint — session-token authenticated (POS extensions // can't use the storefront's app-proxy signature scheme), CORS-enabled // since POS calls this cross-origin. Calls the exact same // resolveAvailabilityRequest() as the storefront's // apps.scheduling.availability.tsx — 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.pos(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)); };