- 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>
59 lines
2.1 KiB
TypeScript
59 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json() as { query: string; region: string; count: number };
|
|
const { query, region, count } = body;
|
|
|
|
if (!query || typeof query !== "string") {
|
|
return NextResponse.json({ error: "Suchbegriff fehlt" }, { status: 400 });
|
|
}
|
|
|
|
const searchQuery = region ? `${query} ${region}` : query;
|
|
const base = req.nextUrl.origin;
|
|
|
|
// ── 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({
|
|
queries: [searchQuery],
|
|
maxResultsPerQuery: Math.min(count, 60),
|
|
languageCode: "de",
|
|
categories: ["ceo"],
|
|
enrichEmails: true,
|
|
}),
|
|
});
|
|
|
|
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 { 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: "Suche konnte nicht gestartet werden" }, { status: 500 });
|
|
}
|
|
}
|