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 <cursoragent@cursor.com>
This commit is contained in:
Eric 2026-11-05 19:27:06 +08:00
parent 69949b3d82
commit 344886172d
2 changed files with 59 additions and 15 deletions

View File

@ -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<string>();
const upstreams: string[] = [];
const seen = new Set<string>();
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,
}),
);
}

View File

@ -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);
}