import { NextRequest, NextResponse } from 'next/server' import { db } from '@/lib/db' import { isAuthenticated } from '@/lib/auth' const VALID_STATUSES = ['NEW', 'IN_PROGRESS', 'DONE', 'SPAM'] const STATUS_LABELS: Record = { NEW: 'Новая', IN_PROGRESS: 'В работе', DONE: 'Обработана', SPAM: 'Спам', } export async function PATCH(request: NextRequest) { try { const authHeader = request.headers.get('authorization') const authed = await isAuthenticated(authHeader) if (!authed) { return NextResponse.json({ error: 'Не авторизован' }, { status: 401 }) } const body = await request.json() const { ids, status, action } = body if (action === 'mark_all_new') { const newLeads = await db.lead.findMany({ where: { status: 'NEW' }, select: { id: true } }) const result = await db.lead.updateMany({ where: { status: 'NEW' }, data: { status: 'IN_PROGRESS' }, }) // Log activity for each for (const lead of newLeads) { await db.activityLog.create({ data: { leadId: lead.id, action: 'status_change', oldValue: 'Новая', newValue: 'В работе', details: 'Массовое взятие в работу', }, }) } return NextResponse.json({ updated: result.count }) } if (action === 'delete_spam') { const spamLeads = await db.lead.findMany({ where: { status: 'SPAM' }, select: { id: true } }) const spamIds = spamLeads.map((l) => l.id) // Delete activity logs and comments first if (spamIds.length > 0) { await db.activityLog.deleteMany({ where: { leadId: { in: spamIds } } }) await db.leadComment.deleteMany({ where: { leadId: { in: spamIds } } }) } const result = await db.lead.deleteMany({ where: { status: 'SPAM' }, }) return NextResponse.json({ deleted: result.count }) } if (action === 'delete_selected') { if (!ids || !Array.isArray(ids) || ids.length === 0) { return NextResponse.json({ error: 'Укажите IDs' }, { status: 400 }) } // Delete activity logs and comments first await db.activityLog.deleteMany({ where: { leadId: { in: ids } } }) await db.leadComment.deleteMany({ where: { leadId: { in: ids } } }) const result = await db.lead.deleteMany({ where: { id: { in: ids } }, }) return NextResponse.json({ deleted: result.count }) } if (!ids || !Array.isArray(ids) || ids.length === 0) { return NextResponse.json({ error: 'Укажите IDs' }, { status: 400 }) } if (!status || !VALID_STATUSES.includes(status)) { return NextResponse.json({ error: 'Некорректный статус' }, { status: 400 }) } // Get current statuses for activity logging const currentLeads = await db.lead.findMany({ where: { id: { in: ids } }, select: { id: true, status: true }, }) const result = await db.lead.updateMany({ where: { id: { in: ids } }, data: { status }, }) // Log activity for each changed lead for (const lead of currentLeads) { if (lead.status !== status) { await db.activityLog.create({ data: { leadId: lead.id, action: 'status_change', oldValue: STATUS_LABELS[lead.status] || lead.status, newValue: STATUS_LABELS[status] || status, details: 'Массовое изменение статуса', }, }) } } return NextResponse.json({ updated: result.count }) } catch (error) { console.error('Error bulk updating:', error) return NextResponse.json({ error: 'Ошибка массового обновления' }, { status: 500 }) } }