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
199 lines
5.2 KiB
TypeScript
199 lines
5.2 KiB
TypeScript
'use server'
|
|
|
|
import { revalidatePath } from 'next/cache'
|
|
import prisma from '@/lib/prisma'
|
|
import { auth } from '@/auth'
|
|
|
|
export async function createBrainstormShare(
|
|
sessionId: string,
|
|
recipientEmail: string,
|
|
permission: 'editor' | 'viewer' = 'editor'
|
|
) {
|
|
const session = await auth()
|
|
if (!session?.user?.id) throw new Error('Unauthorized')
|
|
|
|
const brainstorm = await prisma.brainstormSession.findUnique({
|
|
where: { id: sessionId },
|
|
})
|
|
if (!brainstorm) throw new Error('Session not found')
|
|
if (brainstorm.userId !== session.user.id)
|
|
throw new Error('Only the owner can share')
|
|
|
|
const recipient = await prisma.user.findUnique({
|
|
where: { email: recipientEmail },
|
|
})
|
|
if (!recipient) throw new Error('No account found with this email')
|
|
if (recipient.id === session.user.id)
|
|
throw new Error('Cannot share with yourself')
|
|
|
|
const existing = await prisma.brainstormShare.findUnique({
|
|
where: { sessionId_userId: { sessionId, userId: recipient.id } },
|
|
})
|
|
|
|
if (existing) {
|
|
switch (existing.status) {
|
|
case 'accepted':
|
|
return { success: true, message: 'already_shared' }
|
|
case 'pending':
|
|
return { success: true, message: 'already_pending' }
|
|
case 'declined':
|
|
case 'removed':
|
|
await prisma.brainstormShare.update({
|
|
where: { id: existing.id },
|
|
data: {
|
|
status: 'pending',
|
|
permission,
|
|
notifiedAt: new Date(),
|
|
respondedAt: null,
|
|
},
|
|
})
|
|
return { success: true, message: 're_invited' }
|
|
}
|
|
}
|
|
|
|
await prisma.brainstormShare.create({
|
|
data: {
|
|
sessionId,
|
|
userId: recipient.id,
|
|
sharedBy: session.user.id,
|
|
status: 'pending',
|
|
permission,
|
|
notifiedAt: new Date(),
|
|
},
|
|
})
|
|
|
|
return { success: true, message: 'invited' }
|
|
}
|
|
|
|
export async function getPendingBrainstormShares() {
|
|
const session = await auth()
|
|
if (!session?.user?.id) return []
|
|
|
|
try {
|
|
return await prisma.brainstormShare.findMany({
|
|
where: { userId: session.user.id, status: 'pending' },
|
|
include: {
|
|
session: {
|
|
select: { id: true, seedIdea: true, createdAt: true },
|
|
},
|
|
sharer: {
|
|
select: { id: true, name: true, email: true, image: true },
|
|
},
|
|
},
|
|
orderBy: { createdAt: 'desc' },
|
|
})
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export async function respondToBrainstormShare(
|
|
shareId: string,
|
|
action: 'accept' | 'decline'
|
|
) {
|
|
const session = await auth()
|
|
if (!session?.user?.id) throw new Error('Unauthorized')
|
|
|
|
const share = await prisma.brainstormShare.findUnique({
|
|
where: { id: shareId },
|
|
})
|
|
if (!share) throw new Error('Share not found')
|
|
if (share.userId !== session.user.id) throw new Error('Unauthorized')
|
|
if (share.status !== 'pending')
|
|
throw new Error(`Share already ${share.status}`)
|
|
|
|
const newStatus = action === 'accept' ? 'accepted' : 'declined'
|
|
|
|
await prisma.brainstormShare.update({
|
|
where: { id: shareId },
|
|
data: { status: newStatus, respondedAt: new Date() },
|
|
})
|
|
|
|
if (action === 'accept') {
|
|
await prisma.brainstormParticipant.upsert({
|
|
where: {
|
|
sessionId_userId: {
|
|
sessionId: share.sessionId,
|
|
userId: session.user.id,
|
|
},
|
|
},
|
|
create: {
|
|
sessionId: share.sessionId,
|
|
userId: session.user.id,
|
|
role: share.permission === 'viewer' ? 'viewer' : 'editor',
|
|
},
|
|
update: {
|
|
role: share.permission === 'viewer' ? 'viewer' : 'editor',
|
|
},
|
|
})
|
|
}
|
|
|
|
revalidatePath('/')
|
|
return { success: true }
|
|
}
|
|
|
|
export async function ensureAcceptedSharesHaveParticipants() {
|
|
const session = await auth()
|
|
if (!session?.user?.id) return
|
|
|
|
const acceptedShares = await prisma.brainstormShare.findMany({
|
|
where: { userId: session.user.id, status: 'accepted' },
|
|
select: { sessionId: true, permission: true },
|
|
})
|
|
|
|
for (const share of acceptedShares) {
|
|
await prisma.brainstormParticipant.upsert({
|
|
where: {
|
|
sessionId_userId: {
|
|
sessionId: share.sessionId,
|
|
userId: session.user.id,
|
|
},
|
|
},
|
|
create: {
|
|
sessionId: share.sessionId,
|
|
userId: session.user.id,
|
|
role: share.permission === 'viewer' ? 'viewer' : 'editor',
|
|
},
|
|
update: {},
|
|
})
|
|
}
|
|
}
|
|
|
|
export async function getAcceptedBrainstormShares() {
|
|
const session = await auth()
|
|
if (!session?.user?.id) return []
|
|
|
|
try {
|
|
return await prisma.brainstormShare.findMany({
|
|
where: { userId: session.user.id, status: 'accepted' },
|
|
include: {
|
|
session: {
|
|
select: {
|
|
id: true,
|
|
seedIdea: true,
|
|
createdAt: true,
|
|
updatedAt: true,
|
|
ideas: { where: { status: 'active' }, select: { id: true } },
|
|
},
|
|
},
|
|
},
|
|
orderBy: { session: { updatedAt: 'desc' } },
|
|
})
|
|
} catch {
|
|
return []
|
|
}
|
|
}
|
|
|
|
export async function removeBrainstormShare(sessionId: string) {
|
|
const session = await auth()
|
|
if (!session?.user?.id) throw new Error('Unauthorized')
|
|
|
|
await prisma.brainstormShare.updateMany({
|
|
where: { sessionId, userId: session.user.id, status: 'accepted' },
|
|
data: { status: 'removed' },
|
|
})
|
|
|
|
revalidatePath('/')
|
|
return { success: true }
|
|
}
|