101 lines
2.6 KiB
TypeScript
101 lines
2.6 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { prisma } from '@/lib/db/prisma'
|
|
import { getCurrentUser } from '@/lib/auth'
|
|
import { canAccessWorkspace } from '@/lib/db/workspace-access'
|
|
|
|
// GET /api/workspaces/[id]/handoff-notes
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const user = await getCurrentUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const workspaceId = params.id
|
|
const access = await canAccessWorkspace(user.id, workspaceId)
|
|
if (!access) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
}
|
|
|
|
// Get notes that haven't expired
|
|
const notes = await prisma.handoffNote.findMany({
|
|
where: {
|
|
workspaceId,
|
|
expiresAt: { gte: new Date() }
|
|
},
|
|
orderBy: { createdAt: 'desc' },
|
|
include: {
|
|
createdBy: { select: { id: true, name: true } }
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({ notes })
|
|
} catch (error) {
|
|
console.error('Failed to fetch handoff notes:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch handoff notes' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// POST /api/workspaces/[id]/handoff-notes
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: { id: string } }
|
|
) {
|
|
try {
|
|
const user = await getCurrentUser()
|
|
if (!user) {
|
|
return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
}
|
|
|
|
const workspaceId = params.id
|
|
const access = await canAccessWorkspace(user.id, workspaceId)
|
|
if (!access || access.role === 'VIEWER') {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { content, category, priority, expiresAt } = body
|
|
|
|
const note = await prisma.handoffNote.create({
|
|
data: {
|
|
workspaceId,
|
|
content,
|
|
category: category || 'GENERAL',
|
|
priority: priority || 'NORMAL',
|
|
expiresAt: new Date(expiresAt),
|
|
createdById: user.id,
|
|
acknowledgedBy: []
|
|
},
|
|
include: {
|
|
createdBy: { select: { id: true, name: true } }
|
|
}
|
|
})
|
|
|
|
// Log audit
|
|
await prisma.auditLog.create({
|
|
data: {
|
|
workspaceId,
|
|
userId: user.id,
|
|
action: 'CREATE',
|
|
entityType: 'HANDOFF_NOTE',
|
|
entityId: note.id,
|
|
details: { category, priority }
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({ note })
|
|
} catch (error) {
|
|
console.error('Failed to create handoff note:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create handoff note' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|