All checks were successful
Deploy to Production / Build and Deploy (push) Successful in 7s
- Add brainstorm feature with collaborative canvas, AI idea generation, live cursors, playback, and export - Add PDF upload/extraction/ingestion pipeline with pgvector document search (RAG) - Add document Q&A overlay with streaming chat and PDF preview - Add note attachments UI with status polling, grid layout, and auto-scroll - Add task extraction AI tool and agent executor improvements - Fix NoteEmbedding missing updatedAt column, re-index 66 notes with 1536-dim embeddings - Fix brainstorm 'Create Note' button: add success toast and redirect to created note - Fix memory echo notification infinite polling - Fix chat route to always include document_search tool - Add brainstorm i18n keys across all 14 locales - Add socket server for real-time brainstorm collaboration - Add hierarchical notebook selector and organize notebook dialog improvements - Add sidebar brainstorm section with session management - Update prisma schema with brainstorm tables, attachments, and document chunks
86 lines
2.6 KiB
TypeScript
86 lines
2.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import prisma from '@/lib/prisma'
|
|
import { auth } from '@/auth'
|
|
import { z } from 'zod'
|
|
import { logActivity } from '@/lib/brainstorm-collab'
|
|
|
|
const joinSchema = z.object({
|
|
token: z.string().min(1),
|
|
})
|
|
|
|
export async function POST(request: NextRequest) {
|
|
const session = await auth()
|
|
if (!session?.user?.id) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
try {
|
|
const body = await request.json()
|
|
const { token } = joinSchema.parse(body)
|
|
|
|
const brainstormSession = await prisma.brainstormSession.findFirst({
|
|
where: { inviteToken: token },
|
|
select: { id: true, inviteExpiry: true, userId: true, seedIdea: true },
|
|
})
|
|
|
|
if (!brainstormSession) {
|
|
return NextResponse.json({ error: 'Invalid invite token' }, { status: 404 })
|
|
}
|
|
|
|
if (brainstormSession.inviteExpiry && brainstormSession.inviteExpiry < new Date()) {
|
|
return NextResponse.json({ error: 'Invite expired' }, { status: 410 })
|
|
}
|
|
|
|
const existing = await prisma.brainstormParticipant.findFirst({
|
|
where: { sessionId: brainstormSession.id, userId: session.user.id },
|
|
})
|
|
|
|
if (existing) {
|
|
return NextResponse.json({
|
|
success: true,
|
|
sessionId: brainstormSession.id,
|
|
role: existing.role,
|
|
})
|
|
}
|
|
|
|
const participant = await prisma.brainstormParticipant.create({
|
|
data: {
|
|
sessionId: brainstormSession.id,
|
|
userId: session.user.id,
|
|
role: 'editor',
|
|
},
|
|
})
|
|
|
|
await logActivity(brainstormSession.id, 'joined', session.user.id)
|
|
|
|
try {
|
|
const joinerName = session.user.name || 'Someone'
|
|
const seedPreview = brainstormSession.seedIdea.length > 40
|
|
? brainstormSession.seedIdea.substring(0, 40) + '…'
|
|
: brainstormSession.seedIdea
|
|
await prisma.notification.create({
|
|
data: {
|
|
userId: brainstormSession.userId,
|
|
type: 'brainstorm_joined',
|
|
title: `${joinerName} joined your brainstorm`,
|
|
message: seedPreview,
|
|
actionUrl: `/brainstorm?session=${brainstormSession.id}`,
|
|
relatedId: brainstormSession.id,
|
|
},
|
|
})
|
|
} catch {}
|
|
|
|
return NextResponse.json({
|
|
success: true,
|
|
sessionId: brainstormSession.id,
|
|
role: participant.role,
|
|
}, { status: 201 })
|
|
} catch (error: any) {
|
|
if (error instanceof z.ZodError) {
|
|
return NextResponse.json({ error: error.issues }, { status: 400 })
|
|
}
|
|
console.error('Error joining brainstorm:', error)
|
|
return NextResponse.json({ error: 'Failed to join' }, { status: 500 })
|
|
}
|
|
}
|