From 344886172d7bbca7c0ec82acb66d6b9e1b14fdb6 Mon Sep 17 00:00:00 2001 From: Eric Date: Thu, 5 Nov 2026 19:27:06 +0800 Subject: [PATCH] Fix join leads so portal writes reach PocketBase for admin. Await same-origin social forward and fall back when portal only stored local JSON. Co-authored-by: Cursor --- src/app/api/portal/join-leads/route.ts | 59 ++++++++++++++++++++------ src/modules/social/lib/api.ts | 15 ++++++- 2 files changed, 59 insertions(+), 15 deletions(-) diff --git a/src/app/api/portal/join-leads/route.ts b/src/app/api/portal/join-leads/route.ts index bc0a557..89b25bb 100644 --- a/src/app/api/portal/join-leads/route.ts +++ b/src/app/api/portal/join-leads/route.ts @@ -258,8 +258,8 @@ export async function POST(req: NextRequest) { } await writeStore(store); - // Forward to social_svc → PocketBase. Only env-configured upstreams — - // never hardcode nomadro.com (local QA was double-writing production leads). + // Forward to social_svc → PocketBase (admin 读 PB,不是 Next 本机 JSON)。 + // 优先同源 /api/social/v1(走 next.config rewrite),避免只写 local 后台看不到。 const bodies = JSON.stringify({ email, stage, @@ -275,23 +275,53 @@ export async function POST(req: NextRequest) { "Content-Type": "application/json", "X-Social-Guest-Id": guestId || "portal", }; - const upstreams = new Set(); + const upstreams: string[] = []; + const seen = new Set(); + const pushUp = (base: string) => { + const b = (base || "").replace(/\/$/, ""); + if (!b || seen.has(b)) return; + seen.add(b); + upstreams.push(b); + }; + // Same-origin rewrite path (production + local next rewrites) + try { + const proto = + req.headers.get("x-forwarded-proto") || + req.nextUrl.protocol.replace(":", "") || + "https"; + const host = + req.headers.get("x-forwarded-host") || + req.headers.get("host") || + req.nextUrl.host; + if (host) pushUp(`${proto}://${host}`); + } catch { + /* ignore */ + } for (const raw of [process.env.SOCIAL_API_URL, process.env.API_URL]) { - const base = (raw || "").replace(/\/$/, ""); - if (base) upstreams.add(base); + pushUp(raw || ""); } if (process.env.NODE_ENV !== "production") { - upstreams.add("http://127.0.0.1:8080"); + pushUp("http://127.0.0.1:8080"); } - // Optional explicit mirror (ops only) — never implicit production hardcode. const mirror = (process.env.JOIN_LEAD_MIRROR_URL || "").replace(/\/$/, ""); - if (mirror) upstreams.add(mirror); + if (mirror) pushUp(mirror); + + let forwarded = false; for (const upstream of upstreams) { - void fetch(`${upstream}/api/social/v1/join/leads`, { - method: "POST", - headers, - body: bodies, - }).catch(() => undefined); + try { + const res = await fetch(`${upstream}/api/social/v1/join/leads`, { + method: "POST", + headers, + body: bodies, + signal: AbortSignal.timeout(6000), + }); + if (res.ok) { + forwarded = true; + break; + } + } catch { + /* try next upstream */ + } } return withCors( @@ -302,7 +332,8 @@ export async function POST(req: NextRequest) { guestId: lead.guestId, stage: lead.stage, at: now, - stored: "local", + stored: forwarded ? "pb" : "local", + forwarded, }), ); } diff --git a/src/modules/social/lib/api.ts b/src/modules/social/lib/api.ts index 56f40bc..d9b4e4b 100644 --- a/src/modules/social/lib/api.ts +++ b/src/modules/social/lib/api.ts @@ -597,8 +597,21 @@ export const socialApi = { id?: string; email?: string; stage?: string; + stored?: string; + forwarded?: boolean; }; - if (data?.ok !== false) return data; + // Portal may return ok with only local JSON — admin reads PB. + // Keep trying social rewrite until a PB write succeeds. + if (data?.ok === false) continue; + if ( + url.startsWith("/api/portal/join-leads") && + data?.forwarded !== true && + data?.stored === "local" + ) { + lastErr = "portal local-only; try social"; + continue; + } + return data; } catch (e) { lastErr = e instanceof Error ? e.message : String(e); }