110 lines
3.4 KiB
TypeScript
110 lines
3.4 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import prisma from '@/lib/prisma'
|
|
import { auth } from '@/auth'
|
|
import { logActivity } from '@/lib/brainstorm-collab'
|
|
import { emitToSession } from '@/lib/socket-emit'
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ sessionId: string }> }
|
|
) {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
try {
|
|
const { sessionId } = await params
|
|
|
|
const brainstormSession = await prisma.brainstormSession.findFirst({
|
|
where: {
|
|
id: sessionId,
|
|
OR: [
|
|
{ userId: session.user.id },
|
|
{ participants: { some: { userId: session.user.id } } },
|
|
],
|
|
},
|
|
include: {
|
|
ideas: {
|
|
include: {
|
|
noteRefs: {
|
|
include: {
|
|
note: { select: { id: true, title: true, labels: true } },
|
|
},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
})
|
|
|
|
if (!brainstormSession) {
|
|
return NextResponse.json({ error: 'Session not found' }, { status: 404 })
|
|
}
|
|
|
|
const noteImpactMap = new Map<string, { title: string; labels: string[]; acceptedCount: number; dismissedCount: number }>()
|
|
|
|
for (const idea of brainstormSession.ideas) {
|
|
for (const ref of idea.noteRefs) {
|
|
if (!ref.noteId || !ref.note) continue
|
|
const existing = noteImpactMap.get(ref.noteId) || {
|
|
title: ref.note.title || 'Untitled',
|
|
labels: (() => { try { return JSON.parse(ref.note.labels || '[]') } catch { return [] } })(),
|
|
acceptedCount: 0,
|
|
dismissedCount: 0,
|
|
}
|
|
if (ref.verdict === 'accepted') existing.acceptedCount++
|
|
else if (ref.verdict === 'dismissed') existing.dismissedCount++
|
|
noteImpactMap.set(ref.noteId, existing)
|
|
}
|
|
}
|
|
|
|
const updatePromises: Promise<any>[] = []
|
|
|
|
for (const [noteId, impact] of noteImpactMap) {
|
|
if (impact.acceptedCount === 0 && impact.dismissedCount > 0) {
|
|
if (!impact.labels.includes('brainstorm-dry')) {
|
|
impact.labels.push('brainstorm-dry')
|
|
updatePromises.push(
|
|
prisma.note.update({
|
|
where: { id: noteId },
|
|
data: { labels: JSON.stringify(impact.labels) },
|
|
})
|
|
)
|
|
}
|
|
}
|
|
}
|
|
|
|
if (updatePromises.length > 0) {
|
|
await prisma.$transaction(updatePromises)
|
|
}
|
|
|
|
const fruitful = Array.from(noteImpactMap.values()).filter(n => n.acceptedCount > 0).length
|
|
const dry = Array.from(noteImpactMap.values()).filter(n => n.acceptedCount === 0 && n.dismissedCount > 0).length
|
|
const totalRefs = Array.from(noteImpactMap.values()).length
|
|
|
|
await logActivity(sessionId, 'session_finalized', session.user.id, { notesEnriched: fruitful, notesMarkedDry: dry })
|
|
|
|
await emitToSession(sessionId, 'activity:new', {
|
|
action: 'session_finalized',
|
|
userId: session.user.id,
|
|
userName: session.user.name || 'Guest',
|
|
details: { notesEnriched: fruitful, notesMarkedDry: dry }
|
|
})
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
impact: {
|
|
notesSolicited: totalRefs,
|
|
notesEnriched: fruitful,
|
|
notesMarkedDry: dry,
|
|
},
|
|
})
|
|
} catch (error) {
|
|
console.error('Error finalizing brainstorm:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to finalize brainstorm' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|