import { NextRequest, NextResponse } from "next/server"; import { prisma } from "@/lib/db"; // Deletes vault leads that were created from the given job result IDs. // Looks up domain from each LeadResult and removes matching Lead records. export async function POST(req: NextRequest) { try { const { resultIds } = await req.json() as { resultIds: string[] }; if (!resultIds?.length) { return NextResponse.json({ error: "No result IDs provided" }, { status: 400 }); } // Get domains from the job results const results = await prisma.leadResult.findMany({ where: { id: { in: resultIds } }, select: { id: true, domain: true, email: true }, }); const domains = results.map(r => r.domain).filter((d): d is string => !!d); // Delete vault leads with matching domains let deleted = 0; if (domains.length > 0) { const { count } = await prisma.lead.deleteMany({ where: { domain: { in: domains } }, }); deleted = count; } return NextResponse.json({ deleted }); } catch (err) { console.error("POST /api/leads/delete-from-results error:", err); return NextResponse.json({ error: "Delete failed" }, { status: 500 }); } }