/** * Elimina todos los registros de LeadCaptura donde board = 'LEADS' o * board = 'OPORTUNIDADES'. Muestra conteo antes y después. * * Uso: npx tsx scripts/limpiar-leads.js */ import path from 'node:path' import { fileURLToPath } from 'node:url' import dotenv from 'dotenv' import { PrismaClient } from '@prisma/client' const raizApi = path.resolve(path.dirname(fileURLToPath(import.meta.url)), '..') dotenv.config({ path: [path.resolve(raizApi, '.env'), path.resolve(raizApi, '..', '.env')] }) const prisma = new PrismaClient() async function main() { // ── Conteo antes ── const total = await prisma.leadCaptura.count() const leads = await prisma.leadCaptura.count({ where: { board: 'LEADS' } }) const oportunidades = await prisma.leadCaptura.count({ where: { board: 'OPORTUNIDADES' } }) const otros = total - leads - oportunidades console.log('── Antes de limpiar ──') console.log(` Total LeadCaptura: ${total}`) console.log(` board = LEADS: ${leads}`) console.log(` board = OPORTUNIDADES: ${oportunidades}`) console.log(` Otros (APARTADO, etc.): ${otros}`) if (leads + oportunidades === 0) { console.log('\nNo hay leads ni oportunidades que borrar.') return } // ── Eliminar leads y oportunidades ── const deleted = await prisma.leadCaptura.deleteMany({ where: { board: { in: ['LEADS', 'OPORTUNIDADES'] } }, }) console.log(`\nEliminados: ${deleted.count} registros`) // ── Conteo después ── const totalDespues = await prisma.leadCaptura.count() const leadsDespues = await prisma.leadCaptura.count({ where: { board: 'LEADS' } }) const opDespues = await prisma.leadCaptura.count({ where: { board: 'OPORTUNIDADES' } }) const otrosDespues = totalDespues - leadsDespues - opDespues console.log('\n── Después de limpiar ──') console.log(` Total LeadCaptura: ${totalDespues}`) console.log(` board = LEADS: ${leadsDespues}`) console.log(` board = OPORTUNIDADES: ${opDespues}`) console.log(` Otros (APARTADO, etc.): ${otrosDespues}`) console.log('\n✅ Limpieza completada.') } main() .catch((err) => { console.error('❌ Error:', err) process.exit(1) }) .finally(() => prisma.$disconnect())