Shared checkout-guard lookup moved to app/lib/guard.server.ts. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
24 lines
1.1 KiB
TypeScript
24 lines
1.1 KiB
TypeScript
/** Checkout guard (cart & checkout validation function) lookup, shared by Settings and the Today setup guide. */
|
|
export const GUARD_HANDLE = "datewise-checkout-guard";
|
|
|
|
export type AdminGraphql = { graphql: (q: string, o?: { variables?: Record<string, unknown> }) => Promise<Response> };
|
|
|
|
export async function gql<T>(admin: AdminGraphql, q: string, variables?: Record<string, unknown>) {
|
|
const r = await admin.graphql(q, { variables });
|
|
return ((await r.json()) as { data: T }).data;
|
|
}
|
|
|
|
export async function findGuard(admin: AdminGraphql) {
|
|
const d = await gql<{
|
|
shopifyFunctions: { nodes: { id: string; title: string; apiType: string }[] };
|
|
validations: { nodes: { id: string; enabled: boolean; shopifyFunction: { id: string } }[] };
|
|
}>(admin, `#graphql
|
|
query {
|
|
shopifyFunctions(first: 50) { nodes { id title apiType } }
|
|
validations(first: 25) { nodes { id enabled shopifyFunction { id } } }
|
|
}`);
|
|
const fn = d.shopifyFunctions.nodes.find((f) => f.title === GUARD_HANDLE || /datewise/i.test(f.title));
|
|
const validation = fn ? d.validations.nodes.find((v) => v.shopifyFunction.id === fn.id) : undefined;
|
|
return { fn, validation };
|
|
}
|