Files
lead-scraper/app/api/leads/route.ts
Timo Uttenweiler 115cdacd08 UI improvements: Leadspeicher, Maps enrichment, exports
- Rename LeadVault → Leadspeicher throughout (sidebar, topbar, page)
- SidePanel: full lead detail view with contact, source, tags (read-only), Google Maps link for address
- Tags: kontaktiert stored as tag (toggleable), favorit tag toggle
- Remove Status column, StatusBadge dropdown, Priority feature
- Remove Aktualisieren button from Leadspeicher
- Bulk actions: remove status dropdown
- Export: LeadVault Excel-only, clean columns, freeze row + autofilter
- Export dropdown: click-based (fix overflow-hidden clipping)
- ExportButtons: remove CSV, Excel only everywhere
- Maps page: post-search Anymailfinder enrichment button
- ProgressCard: "Suche läuft..." instead of "Warte auf Anymailfinder-Server..."
- Quick SERP renamed to "Schnell neue Suche"
- Results page: Excel export, always-enabled download button
- Anymailfinder: fix bulk field names, array-of-arrays format
- Apify: fix countryCode lowercase
- API: sourceTerm search, contacted/favorite tag filters

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
2026-03-21 18:12:31 +01:00

99 lines
3.2 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 page = Math.max(1, Number(searchParams.get("page") || 1));
const perPage = Math.min(100, Math.max(10, Number(searchParams.get("perPage") || 50)));
const search = searchParams.get("search") || "";
const sortBy = searchParams.get("sortBy") || "capturedAt";
const sortDir = (searchParams.get("sortDir") || "desc") as "asc" | "desc";
const hasEmail = searchParams.get("hasEmail"); // "yes" | "no" | null
const capturedFrom = searchParams.get("capturedFrom");
const capturedTo = searchParams.get("capturedTo");
const statuses = searchParams.getAll("status");
const sourceTabs = searchParams.getAll("sourceTab");
const priorities = searchParams.getAll("priority");
const tags = searchParams.getAll("tags");
const searchTerms = searchParams.getAll("searchTerm");
const contacted = searchParams.get("contacted");
const favorite = searchParams.get("favorite");
const where: Prisma.LeadWhereInput = {};
if (search) {
where.OR = [
{ domain: { contains: search } },
{ companyName: { contains: search } },
{ contactName: { contains: search } },
{ email: { contains: search } },
{ notes: { contains: search } },
{ sourceTerm: { 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") where.email = { not: null };
else if (hasEmail === "no") where.email = null;
if (capturedFrom || capturedTo) {
where.capturedAt = {};
if (capturedFrom) where.capturedAt.gte = new Date(capturedFrom);
if (capturedTo) where.capturedAt.lte = new Date(capturedTo);
}
if (searchTerms.length > 0) {
where.sourceTerm = { in: searchTerms };
}
if (contacted === "yes") {
where.tags = { contains: "kontaktiert" };
}
if (favorite === "yes") {
where.tags = { contains: "favorit" };
}
if (tags.length > 0) {
// SQLite JSON contains — search for each tag in the JSON string
where.AND = tags.map(tag => ({
tags: { contains: tag },
}));
}
const validSortFields: Record<string, boolean> = {
capturedAt: true, status: true, priority: true, companyName: true,
domain: true, email: true, contactName: true,
};
const orderByField = validSortFields[sortBy] ? sortBy : "capturedAt";
const [total, leads] = await Promise.all([
prisma.lead.count({ where }),
prisma.lead.findMany({
where,
orderBy: { [orderByField]: sortDir },
skip: (page - 1) * perPage,
take: perPage,
}),
]);
return NextResponse.json({
leads,
total,
page,
pages: Math.ceil(total / perPage),
perPage,
});
} catch (err) {
console.error("GET /api/leads error:", err);
return NextResponse.json({ error: "Failed to fetch leads" }, { status: 500 });
}
}