- Prisma-Schema: Lead + LeadEvent Modelle (Migration 20260320) - lib/services/leadVault.ts: sinkLeadsToVault mit Deduplizierung - Auto-Sync: alle 4 Pipelines schreiben Leads in LeadVault - GET /api/leads: Filter, Sortierung, Pagination (Server-side) - PATCH/DELETE /api/leads/[id]: Status, Priorität, Tags, Notizen - POST /api/leads/bulk: Bulk-Aktionen für mehrere Leads - GET /api/leads/stats: Statistiken + 7-Tage-Sparkline - POST /api/leads/quick-serp: SERP-Capture ohne Enrichment - GET /api/leads/export: CSV-Export mit allen Feldern - app/leadvault/page.tsx: vollständige UI mit Stats, Quick SERP, Filter-Leiste, sortierbare Tabelle, Bulk-Aktionen, Side Panel - Sidebar: LeadVault-Eintrag mit Live-Badge (neue Leads) Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
94 lines
3.3 KiB
TypeScript
94 lines
3.3 KiB
TypeScript
import { NextRequest, NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
import { Prisma } from "@prisma/client";
|
|
|
|
export async function GET(req: NextRequest) {
|
|
try {
|
|
const { searchParams } = new URL(req.url);
|
|
const search = searchParams.get("search") || "";
|
|
const statuses = searchParams.getAll("status");
|
|
const sourceTabs = searchParams.getAll("sourceTab");
|
|
const priorities = searchParams.getAll("priority");
|
|
const hasEmail = searchParams.get("hasEmail");
|
|
const emailOnly = searchParams.get("emailOnly") === "true";
|
|
|
|
const where: Prisma.LeadWhereInput = {};
|
|
if (search) {
|
|
where.OR = [
|
|
{ domain: { contains: search } },
|
|
{ companyName: { contains: search } },
|
|
{ contactName: { contains: search } },
|
|
{ email: { contains: search } },
|
|
];
|
|
}
|
|
if (statuses.length > 0) where.status = { in: statuses };
|
|
if (sourceTabs.length > 0) where.sourceTab = { in: sourceTabs };
|
|
if (priorities.length > 0) where.priority = { in: priorities };
|
|
if (hasEmail === "yes" || emailOnly) where.email = { not: null };
|
|
else if (hasEmail === "no") where.email = null;
|
|
|
|
const leads = await prisma.lead.findMany({
|
|
where,
|
|
orderBy: { capturedAt: "desc" },
|
|
take: 10000,
|
|
});
|
|
|
|
const columns = [
|
|
"status", "priority", "company_name", "domain", "contact_name",
|
|
"contact_title", "email", "email_confidence", "linkedin_url", "phone",
|
|
"source_tab", "source_term", "tags", "country", "headcount", "industry",
|
|
"notes", "captured_at", "contacted_at", "serp_rank", "serp_url",
|
|
"serp_title", "serp_snippet", "lead_id",
|
|
];
|
|
|
|
const rows = leads.map(l => ({
|
|
status: l.status,
|
|
priority: l.priority,
|
|
company_name: l.companyName || "",
|
|
domain: l.domain || "",
|
|
contact_name: l.contactName || "",
|
|
contact_title: l.contactTitle || "",
|
|
email: l.email || "",
|
|
email_confidence: l.emailConfidence != null ? Math.round(l.emailConfidence * 100) + "%" : "",
|
|
linkedin_url: l.linkedinUrl || "",
|
|
phone: l.phone || "",
|
|
source_tab: l.sourceTab,
|
|
source_term: l.sourceTerm || "",
|
|
tags: l.tags || "",
|
|
country: l.country || "",
|
|
headcount: l.headcount || "",
|
|
industry: l.industry || "",
|
|
notes: l.notes || "",
|
|
captured_at: l.capturedAt.toISOString(),
|
|
contacted_at: l.contactedAt?.toISOString() || "",
|
|
serp_rank: l.serpRank?.toString() || "",
|
|
serp_url: l.serpUrl || "",
|
|
serp_title: l.serpTitle || "",
|
|
serp_snippet: l.serpSnippet || "",
|
|
lead_id: l.id,
|
|
}));
|
|
|
|
const csv = [
|
|
columns.join(","),
|
|
...rows.map(r =>
|
|
columns.map(c => {
|
|
const v = String(r[c as keyof typeof r] || "");
|
|
return v.includes(",") || v.includes('"') || v.includes("\n")
|
|
? `"${v.replace(/"/g, '""')}"`
|
|
: v;
|
|
}).join(",")
|
|
),
|
|
].join("\n");
|
|
|
|
return new NextResponse(csv, {
|
|
headers: {
|
|
"Content-Type": "text/csv; charset=utf-8",
|
|
"Content-Disposition": `attachment; filename="leadflow-vault-${new Date().toISOString().split("T")[0]}.csv"`,
|
|
},
|
|
});
|
|
} catch (err) {
|
|
console.error("GET /api/leads/export error:", err);
|
|
return NextResponse.json({ error: "Export failed" }, { status: 500 });
|
|
}
|
|
}
|