- Schema: companyType, topics, salesScore, salesReason, offerPackage, approved, approvedAt, SearchHistory table - /api/search-history: GET (by mode) + POST (save query) - /api/ai-search: stadtwerke/industrie/custom prompts with history dedup - /api/enrich-leads: website scraping + GPT-4o-mini enrichment (fire-and-forget after each job) - /api/generate-email: personalized outreach via GPT-4o - Suche page: 3 mode tabs (Stadtwerke/Industrie/Freie Suche), Alle-Bundesländer queue button, AiSearchModal gets searchMode + history - Leadspeicher: Bewertung dots column, Paket badge column, Freigeben toggle button, email generator in SidePanel, approved-only export option - Leads API: approvedOnly + companyType filters, new fields in PATCH Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
73 lines
2.5 KiB
TypeScript
73 lines
2.5 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",
|
|
"companyType", "topics", "salesScore", "salesReason", "offerPackage",
|
|
"approved", "approvedAt",
|
|
];
|
|
|
|
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 });
|
|
}
|
|
}
|