fix: Leads ohne E-Mails wenn Anymailfinder-Guthaben leer
Anymailfinder-Fehler (z.B. 402) markiert Job nicht mehr als failed. Maps- und SERP-Jobs schließen als complete ab und liefern die gefundenen Unternehmen ohne Kontaktdaten — SERP-Supplement triggert danach normal. - maps-enrich: Anymailfinder in eigenem try-catch, Fehler → complete - serp-enrich: SERP-Rohdaten zuerst speichern, dann Enrichment versuchen Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -110,10 +110,16 @@ async function runMapsEnrich(
|
||||
// 3. Optionally enrich with Anymailfinder
|
||||
if (params.enrichEmails && places.length > 0) {
|
||||
const anymailKey = await getApiKey("anymailfinder");
|
||||
if (!anymailKey) throw new Error("Anymailfinder API-Key fehlt — bitte in den Einstellungen eintragen");
|
||||
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 } });
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const domains = places.filter(p => p.domain).map(p => p.domain!);
|
||||
|
||||
// Map domain → placeId for updating results
|
||||
// Map domain → leadResult id for updating
|
||||
const domainToResultId = new Map<string, string>();
|
||||
const existingResults = await prisma.leadResult.findMany({
|
||||
where: { jobId },
|
||||
@@ -173,6 +179,14 @@ async function runMapsEnrich(
|
||||
params.queries.join(", "),
|
||||
jobId,
|
||||
);
|
||||
} catch (enrichErr) {
|
||||
// Anymailfinder failed (e.g. 402 quota) — complete with Maps-only results
|
||||
console.warn(`[maps-enrich] Anymailfinder failed for job ${jobId}:`, enrichErr);
|
||||
await prisma.job.update({
|
||||
where: { id: jobId },
|
||||
data: { status: "complete", totalLeads: places.length },
|
||||
});
|
||||
}
|
||||
} else {
|
||||
await prisma.job.update({
|
||||
where: { id: jobId },
|
||||
|
||||
@@ -97,7 +97,35 @@ async function runSerpEnrich(
|
||||
data: { totalLeads: domains.length },
|
||||
});
|
||||
|
||||
// 7. Enrich with Anymailfinder Bulk API
|
||||
// 7. Store raw SERP results first (so we have leads even if enrichment fails)
|
||||
for (const r of filteredResults) {
|
||||
await prisma.leadResult.create({
|
||||
data: {
|
||||
jobId,
|
||||
companyName: r.title || null,
|
||||
domain: r.domain || null,
|
||||
source: JSON.stringify({ url: r.url, description: r.description, position: r.position }),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
// 8. Sink raw results to vault immediately (no contact info yet)
|
||||
await sinkLeadsToVault(
|
||||
filteredResults.map(r => ({
|
||||
domain: r.domain || null,
|
||||
companyName: r.title || null,
|
||||
serpTitle: r.title || null,
|
||||
serpSnippet: r.description || null,
|
||||
serpRank: r.position ?? null,
|
||||
serpUrl: r.url || null,
|
||||
})),
|
||||
"serp",
|
||||
params.query,
|
||||
jobId,
|
||||
);
|
||||
|
||||
// 9. Enrich with Anymailfinder (best-effort — failure still completes the job)
|
||||
try {
|
||||
const enrichResults = await bulkSearchDomains(
|
||||
domains,
|
||||
params.categories,
|
||||
@@ -107,40 +135,45 @@ async function runSerpEnrich(
|
||||
}
|
||||
);
|
||||
|
||||
// 8. Store results
|
||||
// Build domain → leadResult id map for updating
|
||||
const domainToResultId = new Map<string, string>();
|
||||
const storedResults = await prisma.leadResult.findMany({
|
||||
where: { jobId },
|
||||
select: { id: true, domain: true },
|
||||
});
|
||||
for (const r of storedResults) {
|
||||
if (r.domain) domainToResultId.set(r.domain, r.id);
|
||||
}
|
||||
|
||||
let emailsFound = 0;
|
||||
for (const result of enrichResults) {
|
||||
const serpData = serpMap.get(result.domain || "");
|
||||
const hasEmail = !!result.valid_email;
|
||||
if (hasEmail) emailsFound++;
|
||||
|
||||
await prisma.leadResult.create({
|
||||
const resultId = domainToResultId.get(result.domain || "");
|
||||
if (resultId) {
|
||||
await prisma.leadResult.update({
|
||||
where: { id: resultId },
|
||||
data: {
|
||||
jobId,
|
||||
companyName: serpData?.title || null,
|
||||
domain: result.domain || null,
|
||||
contactName: result.person_full_name || null,
|
||||
contactTitle: result.person_job_title || null,
|
||||
email: result.email || null,
|
||||
linkedinUrl: result.person_linkedin_url || null,
|
||||
source: JSON.stringify({
|
||||
url: serpData?.url,
|
||||
description: serpData?.description,
|
||||
position: serpData?.position,
|
||||
email_status: result.email_status,
|
||||
}),
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
await prisma.job.update({
|
||||
where: { id: jobId },
|
||||
data: { status: "complete", emailsFound, totalLeads: enrichResults.length },
|
||||
data: { status: "complete", emailsFound, totalLeads: filteredResults.length },
|
||||
});
|
||||
|
||||
// Sync to LeadVault
|
||||
// Update vault entries with contact info
|
||||
await sinkLeadsToVault(
|
||||
enrichResults.map(r => {
|
||||
enrichResults
|
||||
.filter(r => r.email)
|
||||
.map(r => {
|
||||
const serpData = serpMap.get(r.domain || "");
|
||||
return {
|
||||
domain: r.domain || null,
|
||||
@@ -159,6 +192,14 @@ async function runSerpEnrich(
|
||||
params.query,
|
||||
jobId,
|
||||
);
|
||||
} catch (enrichErr) {
|
||||
// Anymailfinder failed — complete with SERP-only results (no emails)
|
||||
console.warn(`[serp-enrich] Anymailfinder failed for job ${jobId}:`, enrichErr);
|
||||
await prisma.job.update({
|
||||
where: { id: jobId },
|
||||
data: { status: "complete", totalLeads: filteredResults.length },
|
||||
});
|
||||
}
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
await prisma.job.update({
|
||||
|
||||
Reference in New Issue
Block a user