Files
lead-scraper/app/api/leads/[id]/route.ts
Timo Uttenweiler 042fbeb672 feat: LeadVault - zentrale Lead-Datenbank mit CRM-Funktionen
- 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>
2026-03-20 17:33:12 +01:00

71 lines
2.4 KiB
TypeScript

import { NextRequest, NextResponse } from "next/server";
import { prisma } from "@/lib/db";
export async function PATCH(req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
const body = await req.json() as Record<string, unknown>;
const oldLead = await prisma.lead.findUnique({ where: { id } });
if (!oldLead) return NextResponse.json({ error: "Not found" }, { status: 404 });
const allowedFields = [
"status", "priority", "notes", "tags", "country", "headcount",
"industry", "contactedAt", "companyName", "contactName", "contactTitle",
"email", "phone", "linkedinUrl", "domain",
];
const data: Record<string, unknown> = {};
for (const field of allowedFields) {
if (field in body) data[field] = body[field];
}
const updated = await prisma.lead.update({ where: { id }, data });
// Track status change events
if (body.status && body.status !== oldLead.status) {
await prisma.leadEvent.create({
data: {
leadId: id,
event: `Status geändert zu "${body.status}"`,
},
});
// Auto-set contactedAt when marking as contacted
if (body.status === "contacted" && !oldLead.contactedAt) {
await prisma.lead.update({ where: { id }, data: { contactedAt: new Date() } });
}
}
return NextResponse.json(updated);
} catch (err) {
console.error("PATCH /api/leads/[id] error:", err);
return NextResponse.json({ error: "Update failed" }, { status: 500 });
}
}
export async function DELETE(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
await prisma.lead.delete({ where: { id } });
return NextResponse.json({ deleted: true });
} catch (err) {
console.error("DELETE /api/leads/[id] error:", err);
return NextResponse.json({ error: "Delete failed" }, { status: 500 });
}
}
export async function GET(_req: NextRequest, { params }: { params: Promise<{ id: string }> }) {
try {
const { id } = await params;
const lead = await prisma.lead.findUnique({
where: { id },
include: { events: { orderBy: { at: "asc" } } },
});
if (!lead) return NextResponse.json({ error: "Not found" }, { status: 404 });
return NextResponse.json(lead);
} catch (err) {
return NextResponse.json({ error: "Fetch failed" }, { status: 500 });
}
}