Search: Maps primary + SERP supplement for count > 60

- Always use Google Maps (max 60 per call)
- If count > 60: fire SERP job in background for additional results
- Dedup handled automatically by LeadVault domain upsert

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
Timo Uttenweiler
2026-03-27 17:10:09 +01:00
parent bf3fcd4210
commit a1d2c34f36

View File

@@ -6,35 +6,53 @@ export async function POST(req: NextRequest) {
const { query, region, count } = body;
if (!query || typeof query !== "string") {
return NextResponse.json({ error: "query is required" }, { status: 400 });
return NextResponse.json({ error: "Suchbegriff fehlt" }, { status: 400 });
}
const searchQuery = region ? `${query} ${region}` : query;
const base = req.nextUrl.origin;
const baseUrl = req.nextUrl.origin;
const res = await fetch(`${baseUrl}/api/jobs/serp-enrich`, {
// ── 1. Maps job (always, max 60) ──────────────────────────────────────────
const mapsRes = await fetch(`${base}/api/jobs/maps-enrich`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: searchQuery,
maxPages: Math.ceil((count || 50) / 10),
countryCode: "de",
queries: [searchQuery],
maxResultsPerQuery: Math.min(count, 60),
languageCode: "de",
filterSocial: true,
categories: ["ceo"],
enrichEmails: true,
}),
});
if (!res.ok) {
const err = await res.json() as { error?: string };
return NextResponse.json({ error: err.error || "Failed to start job" }, { status: 500 });
if (!mapsRes.ok) {
const err = await mapsRes.json() as { error?: string };
return NextResponse.json({ error: err.error || "Suche konnte nicht gestartet werden" }, { status: 500 });
}
const data = await res.json() as { jobId: string };
return NextResponse.json({ jobId: data.jobId });
const { jobId } = await mapsRes.json() as { jobId: string };
// ── 2. SERP supplement (only when count > 60) — fire & forget ────────────
if (count > 60) {
const extraPages = Math.ceil((count - 60) / 10);
fetch(`${base}/api/jobs/serp-enrich`, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({
query: searchQuery,
maxPages: Math.min(extraPages, 10),
countryCode: "de",
languageCode: "de",
filterSocial: true,
categories: ["ceo"],
enrichEmails: true,
}),
}).catch(() => {}); // background — don't block response
}
return NextResponse.json({ jobId });
} catch (err) {
console.error("POST /api/search error:", err);
return NextResponse.json({ error: "Failed to start search" }, { status: 500 });
return NextResponse.json({ error: "Suche konnte nicht gestartet werden" }, { status: 500 });
}
}