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:
parent
69949b3d82
commit
344886172d
@ -258,8 +258,8 @@ export async function POST(req: NextRequest) {
|
|||||||
}
|
}
|
||||||
await writeStore(store);
|
await writeStore(store);
|
||||||
|
|
||||||
// Forward to social_svc → PocketBase. Only env-configured upstreams —
|
// Forward to social_svc → PocketBase (admin 读 PB,不是 Next 本机 JSON)。
|
||||||
// never hardcode nomadro.com (local QA was double-writing production leads).
|
// 优先同源 /api/social/v1(走 next.config rewrite),避免只写 local 后台看不到。
|
||||||
const bodies = JSON.stringify({
|
const bodies = JSON.stringify({
|
||||||
email,
|
email,
|
||||||
stage,
|
stage,
|
||||||
@ -275,23 +275,53 @@ export async function POST(req: NextRequest) {
|
|||||||
"Content-Type": "application/json",
|
"Content-Type": "application/json",
|
||||||
"X-Social-Guest-Id": guestId || "portal",
|
"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]) {
|
for (const raw of [process.env.SOCIAL_API_URL, process.env.API_URL]) {
|
||||||
const base = (raw || "").replace(/\/$/, "");
|
pushUp(raw || "");
|
||||||
if (base) upstreams.add(base);
|
|
||||||
}
|
}
|
||||||
if (process.env.NODE_ENV !== "production") {
|
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(/\/$/, "");
|
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) {
|
for (const upstream of upstreams) {
|
||||||
void fetch(`${upstream}/api/social/v1/join/leads`, {
|
try {
|
||||||
method: "POST",
|
const res = await fetch(`${upstream}/api/social/v1/join/leads`, {
|
||||||
headers,
|
method: "POST",
|
||||||
body: bodies,
|
headers,
|
||||||
}).catch(() => undefined);
|
body: bodies,
|
||||||
|
signal: AbortSignal.timeout(6000),
|
||||||
|
});
|
||||||
|
if (res.ok) {
|
||||||
|
forwarded = true;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
} catch {
|
||||||
|
/* try next upstream */
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return withCors(
|
return withCors(
|
||||||
@ -302,7 +332,8 @@ export async function POST(req: NextRequest) {
|
|||||||
guestId: lead.guestId,
|
guestId: lead.guestId,
|
||||||
stage: lead.stage,
|
stage: lead.stage,
|
||||||
at: now,
|
at: now,
|
||||||
stored: "local",
|
stored: forwarded ? "pb" : "local",
|
||||||
|
forwarded,
|
||||||
}),
|
}),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -597,8 +597,21 @@ export const socialApi = {
|
|||||||
id?: string;
|
id?: string;
|
||||||
email?: string;
|
email?: string;
|
||||||
stage?: 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) {
|
} catch (e) {
|
||||||
lastErr = e instanceof Error ? e.message : String(e);
|
lastErr = e instanceof Error ? e.message : String(e);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user