import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/db"; export async function POST(req: NextRequest) { try { const body = await req.json() as { ids: string[]; action: "status" | "priority" | "tag" | "delete"; value: string; }; const { ids, action, value } = body; if (!ids?.length) return NextResponse.json({ error: "No IDs provided" }, { status: 400 }); if (action === "delete") { const { count } = await prisma.lead.deleteMany({ where: { id: { in: ids } } }); return NextResponse.json({ updated: count }); } if (action === "status") { const { count } = await prisma.lead.updateMany({ where: { id: { in: ids } }, data: { status: value }, }); // Create events for status change for (const id of ids) { await prisma.leadEvent.create({ data: { leadId: id, event: `Status geƤndert zu "${value}" (Bulk)` }, }).catch(() => {}); // ignore if lead was deleted } return NextResponse.json({ updated: count }); } if (action === "priority") { const { count } = await prisma.lead.updateMany({ where: { id: { in: ids } }, data: { priority: value }, }); return NextResponse.json({ updated: count }); } if (action === "tag") { // Add tag to each lead's tags JSON array const leads = await prisma.lead.findMany({ where: { id: { in: ids } }, select: { id: true, tags: true } }); let count = 0; for (const lead of leads) { const existing: string[] = JSON.parse(lead.tags || "[]"); if (!existing.includes(value)) { await prisma.lead.update({ where: { id: lead.id }, data: { tags: JSON.stringify([...existing, value]) }, }); count++; } } return NextResponse.json({ updated: count }); } return NextResponse.json({ error: "Unknown action" }, { status: 400 }); } catch (err) { console.error("POST /api/leads/bulk error:", err); return NextResponse.json({ error: "Bulk action failed" }, { status: 500 }); } }