Files
lead-scraper/app/api/leads/stats/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

43 lines
1.4 KiB
TypeScript

import { NextResponse } from "next/server";
import { prisma } from "@/lib/db";
export async function GET() {
try {
const [total, newLeads, contacted, withEmail] = await Promise.all([
prisma.lead.count(),
prisma.lead.count({ where: { status: "new" } }),
prisma.lead.count({ where: { status: { in: ["contacted", "in_progress"] } } }),
prisma.lead.count({ where: { email: { not: null } } }),
]);
// Daily counts for last 7 days
const sevenDaysAgo = new Date();
sevenDaysAgo.setDate(sevenDaysAgo.getDate() - 6);
sevenDaysAgo.setHours(0, 0, 0, 0);
const recentLeads = await prisma.lead.findMany({
where: { capturedAt: { gte: sevenDaysAgo } },
select: { capturedAt: true },
});
// Build daily counts map
const dailyMap: Record<string, number> = {};
for (let i = 0; i < 7; i++) {
const d = new Date(sevenDaysAgo);
d.setDate(d.getDate() + i);
dailyMap[d.toISOString().split("T")[0]] = 0;
}
for (const lead of recentLeads) {
const key = lead.capturedAt.toISOString().split("T")[0];
if (key in dailyMap) dailyMap[key]++;
}
const dailyCounts = Object.entries(dailyMap).map(([date, count]) => ({ date, count }));
return NextResponse.json({ total, new: newLeads, contacted, withEmail, dailyCounts });
} catch (err) {
console.error("GET /api/leads/stats error:", err);
return NextResponse.json({ error: "Failed" }, { status: 500 });
}
}