feat: Kundensuche – Progressbar, SERP-Supplement, Dedup, Löschen, Neu-Filter
- Progressbar geht nie mehr rückwärts (Math.min-Cap entfernt) - E-Mails-suchen-Phase wird immer kurz angezeigt bevor Fertig - SERP-Supplement startet automatisch wenn Maps < Zielanzahl liefert - Suchergebnisse bleiben nach Abschluss sichtbar (kein Redirect) - Dedup in leadVault strikt nach Domain (verhindert Duplikate) - isNew-Flag pro Result (Batch-Query gegen bestehende Vault-Domains) - Nur-neue-Filter + vorhanden-Badge in Suchergebnissen - Einzeln und Bulk löschen aus Suchergebnissen + Leadspeicher - Fehlermeldung zeigt echten API-Fehler (z.B. 402 Anymailfinder) - SERP-Supplement aus /api/search entfernt (LoadingCard übernimmt) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -18,6 +18,21 @@ export async function GET(
|
||||
});
|
||||
if (!job) return NextResponse.json({ error: "Job not found" }, { status: 404 });
|
||||
|
||||
// Find which domains already existed in vault before this job
|
||||
const resultDomains = job.results
|
||||
.map(r => r.domain)
|
||||
.filter((d): d is string => !!d);
|
||||
|
||||
const preExistingDomains = new Set(
|
||||
(await prisma.lead.findMany({
|
||||
where: {
|
||||
domain: { in: resultDomains },
|
||||
sourceJobId: { not: id },
|
||||
},
|
||||
select: { domain: true },
|
||||
})).map(l => l.domain).filter((d): d is string => !!d)
|
||||
);
|
||||
|
||||
return NextResponse.json({
|
||||
id: job.id,
|
||||
type: job.type,
|
||||
@@ -49,6 +64,7 @@ export async function GET(
|
||||
address,
|
||||
phone,
|
||||
createdAt: r.createdAt,
|
||||
isNew: !r.domain || !preExistingDomains.has(r.domain),
|
||||
};
|
||||
}),
|
||||
});
|
||||
|
||||
36
app/api/leads/delete-from-results/route.ts
Normal file
36
app/api/leads/delete-from-results/route.ts
Normal file
@@ -0,0 +1,36 @@
|
||||
import { NextRequest, NextResponse } from "next/server";
|
||||
import { prisma } from "@/lib/db";
|
||||
|
||||
// Deletes vault leads that were created from the given job result IDs.
|
||||
// Looks up domain from each LeadResult and removes matching Lead records.
|
||||
export async function POST(req: NextRequest) {
|
||||
try {
|
||||
const { resultIds } = await req.json() as { resultIds: string[] };
|
||||
|
||||
if (!resultIds?.length) {
|
||||
return NextResponse.json({ error: "No result IDs provided" }, { status: 400 });
|
||||
}
|
||||
|
||||
// Get domains from the job results
|
||||
const results = await prisma.leadResult.findMany({
|
||||
where: { id: { in: resultIds } },
|
||||
select: { id: true, domain: true, email: true },
|
||||
});
|
||||
|
||||
const domains = results.map(r => r.domain).filter((d): d is string => !!d);
|
||||
|
||||
// Delete vault leads with matching domains
|
||||
let deleted = 0;
|
||||
if (domains.length > 0) {
|
||||
const { count } = await prisma.lead.deleteMany({
|
||||
where: { domain: { in: domains } },
|
||||
});
|
||||
deleted = count;
|
||||
}
|
||||
|
||||
return NextResponse.json({ deleted });
|
||||
} catch (err) {
|
||||
console.error("POST /api/leads/delete-from-results error:", err);
|
||||
return NextResponse.json({ error: "Delete failed" }, { status: 500 });
|
||||
}
|
||||
}
|
||||
@@ -32,24 +32,6 @@ export async function POST(req: NextRequest) {
|
||||
|
||||
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);
|
||||
|
||||
Reference in New Issue
Block a user