From e8fbefaa49faab26a0a2d75216d735701185d588 Mon Sep 17 00:00:00 2001 From: DMleadgen Date: Fri, 27 Mar 2026 13:08:37 -0600 Subject: [PATCH] Query manuals from shared Convex directly --- lib/convex-service.ts | 43 +++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 41 insertions(+), 2 deletions(-) diff --git a/lib/convex-service.ts b/lib/convex-service.ts index 62253fba..4c22af23 100644 --- a/lib/convex-service.ts +++ b/lib/convex-service.ts @@ -1,4 +1,6 @@ +import { ConvexHttpClient } from "convex/browser" import { fetchQuery } from "convex/nextjs" +import { makeFunctionReference } from "convex/server" import { api } from "@/convex/_generated/api" import { hasConvexUrl } from "@/lib/convex-config" import type { Product } from "@/lib/products/types" @@ -30,6 +32,33 @@ type ConvexManualDoc = { manualUrl?: string } + +const LIST_MANUALS = makeFunctionReference<"query">("manuals:list") +const MANUALS_DASHBOARD = makeFunctionReference<"query">("manuals:dashboard") + +let cachedServerConvexClient: ConvexHttpClient | null | undefined + +function getServerConvexClient() { + if (cachedServerConvexClient !== undefined) { + return cachedServerConvexClient + } + + const convexUrl = process.env.CONVEX_URL || process.env.NEXT_PUBLIC_CONVEX_URL + if (!convexUrl) { + cachedServerConvexClient = null + return cachedServerConvexClient + } + + const client = new ConvexHttpClient(convexUrl) + const adminKey = process.env.CONVEX_SELF_HOSTED_ADMIN_KEY + if (adminKey) { + client.setAdminAuth(adminKey) + } + + cachedServerConvexClient = client + return cachedServerConvexClient +} + type ConvexOrderItem = { productId?: string | null productName: string @@ -138,7 +167,12 @@ export async function listConvexManuals(): Promise { return [] } - const manuals = await safeFetchQuery("manuals.list", fetchQuery(api.manuals.list, {}), [] as ConvexManualDoc[]) + const client = getServerConvexClient() + if (!client) { + return [] + } + + const manuals = await safeFetchQuery("manuals.list", client.query(LIST_MANUALS, {}), [] as ConvexManualDoc[]) return (manuals as ConvexManualDoc[]).map(mapConvexManual) } @@ -147,7 +181,12 @@ export async function getConvexManualDashboard() { return null } - return await safeFetchQuery("manuals.dashboard", fetchQuery(api.manuals.dashboard, {}), null) + const client = getServerConvexClient() + if (!client) { + return null + } + + return await safeFetchQuery("manuals.dashboard", client.query(MANUALS_DASHBOARD, {}), null) } export async function getConvexOrderMetrics() {