43 lines
1.4 KiB
TypeScript
43 lines
1.4 KiB
TypeScript
import { NextResponse } from "next/server";
|
|
import { prisma } from "@/lib/db";
|
|
|
|
export async function GET() {
|
|
try {
|
|
const [total, contacted, withEmail] = await Promise.all([
|
|
prisma.lead.count(),
|
|
prisma.lead.count({ where: { tags: { contains: "kontaktiert" } } }),
|
|
prisma.lead.count({ where: { email: { not: null } } }),
|
|
]);
|
|
const newLeads = total - contacted;
|
|
|
|
// 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 });
|
|
}
|
|
}
|