req.nextUrl.origin resolves to external domain behind a reverse proxy, breaking server-to-server fetch calls. Use localhost:PORT instead. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
60 lines
1.9 KiB
TypeScript
60 lines
1.9 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { getApiKey } from "@/lib/utils/apiKey";
|
|
import { generateSupplementQuery } from "@/lib/services/openai";
|
|
|
|
export async function POST(req: NextRequest) {
|
|
try {
|
|
const body = await req.json() as {
|
|
query: string;
|
|
region: string;
|
|
targetCount: number;
|
|
foundCount: number;
|
|
};
|
|
const { query, region, targetCount, foundCount } = body;
|
|
|
|
const base = `http://localhost:${process.env.PORT || 3000}`;
|
|
const deficit = targetCount - foundCount;
|
|
|
|
// 1. Try to generate an optimized query via GPT-4.1
|
|
let optimizedQuery = region ? `${query} ${region}` : query;
|
|
let usedAI = false;
|
|
|
|
const openaiKey = await getApiKey("openai");
|
|
if (openaiKey) {
|
|
const aiQuery = await generateSupplementQuery(query, region, foundCount, targetCount, openaiKey);
|
|
if (aiQuery) {
|
|
optimizedQuery = aiQuery;
|
|
usedAI = true;
|
|
}
|
|
}
|
|
|
|
// 2. Start SERP job with the (possibly optimized) query
|
|
const maxPages = Math.min(Math.max(1, Math.ceil(deficit / 10)), 3);
|
|
|
|
const serpRes = await fetch(`${base}/api/jobs/serp-enrich`, {
|
|
method: "POST",
|
|
headers: { "Content-Type": "application/json" },
|
|
body: JSON.stringify({
|
|
query: optimizedQuery,
|
|
maxPages,
|
|
countryCode: "de",
|
|
languageCode: "de",
|
|
filterSocial: true,
|
|
categories: ["ceo"],
|
|
enrichEmails: true,
|
|
}),
|
|
});
|
|
|
|
if (!serpRes.ok) {
|
|
const err = await serpRes.json() as { error?: string };
|
|
return NextResponse.json({ error: err.error || "SERP job konnte nicht gestartet werden" }, { status: 500 });
|
|
}
|
|
|
|
const { jobId } = await serpRes.json() as { jobId: string };
|
|
return NextResponse.json({ jobId, optimizedQuery, usedAI });
|
|
} catch (err) {
|
|
console.error("POST /api/search/supplement error:", err);
|
|
return NextResponse.json({ error: "Supplement-Suche fehlgeschlagen" }, { status: 500 });
|
|
}
|
|
}
|