From 25234b70ee43385e5e973a09dc4951bc541c74b3 Mon Sep 17 00:00:00 2001 From: TimoUttenweiler Date: Wed, 1 Apr 2026 10:54:30 +0200 Subject: [PATCH] fix: Leads ohne Domain werden gefiltert und nicht gespeichert MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Maps-Ergebnisse ohne Domain werden vor Speicherung herausgefiltert. sinkLeadsToVault überspringt Leads ohne Domain komplett. Co-Authored-By: Claude Sonnet 4.6 --- app/api/jobs/maps-enrich/route.ts | 18 +++++++++++------- lib/services/leadVault.ts | 2 ++ 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/app/api/jobs/maps-enrich/route.ts b/app/api/jobs/maps-enrich/route.ts index 5b3718c..2551b62 100644 --- a/app/api/jobs/maps-enrich/route.ts +++ b/app/api/jobs/maps-enrich/route.ts @@ -75,7 +75,11 @@ async function runMapsEnrich( }); // 2. Store raw Google Maps results + immediately sink to LeadVault - for (const place of places) { + // Skip places without a domain — not actionable as leads + const placesWithDomain = places.filter(p => !!p.domain); + await prisma.job.update({ where: { id: jobId }, data: { totalLeads: placesWithDomain.length } }); + + for (const place of placesWithDomain) { await prisma.leadResult.create({ data: { jobId, @@ -94,7 +98,7 @@ async function runMapsEnrich( // Sink Google Maps results to vault immediately (before enrichment) // so they're available even if Anymailfinder fails await sinkLeadsToVault( - places.map(p => ({ + placesWithDomain.map(p => ({ domain: p.domain, companyName: p.name || null, phone: p.phone, @@ -108,16 +112,16 @@ async function runMapsEnrich( ); // 3. Optionally enrich with Anymailfinder - if (params.enrichEmails && places.length > 0) { + if (params.enrichEmails && placesWithDomain.length > 0) { const anymailKey = await getApiKey("anymailfinder"); if (!anymailKey) { // No key configured — complete with Maps-only results (no emails) - await prisma.job.update({ where: { id: jobId }, data: { status: "complete", totalLeads: places.length } }); + await prisma.job.update({ where: { id: jobId }, data: { status: "complete", totalLeads: placesWithDomain.length } }); return; } try { - const domains = places.filter(p => p.domain).map(p => p.domain!); + const domains = placesWithDomain.map(p => p.domain!); // Map domain → leadResult id for updating const domainToResultId = new Map(); @@ -161,7 +165,7 @@ async function runMapsEnrich( await prisma.job.update({ where: { id: jobId }, - data: { status: "complete", emailsFound, totalLeads: places.length }, + data: { status: "complete", emailsFound, totalLeads: placesWithDomain.length }, }); // Update vault entries with enrichment results @@ -184,7 +188,7 @@ async function runMapsEnrich( console.warn(`[maps-enrich] Anymailfinder failed for job ${jobId}:`, enrichErr); await prisma.job.update({ where: { id: jobId }, - data: { status: "complete", totalLeads: places.length }, + data: { status: "complete", totalLeads: placesWithDomain.length }, }); } } else { diff --git a/lib/services/leadVault.ts b/lib/services/leadVault.ts index 0cc6698..f2f9595 100644 --- a/lib/services/leadVault.ts +++ b/lib/services/leadVault.ts @@ -98,6 +98,8 @@ export async function sinkLeadsToVault( const domain = lead.domain || null; const email = lead.email || null; + if (!domain) { skipped++; continue; } // no domain → skip entirely + if (domain) { // Strict domain dedup: one lead per domain const existing = await prisma.lead.findFirst({ where: { domain } });