92 lines
2.9 KiB
TypeScript
92 lines
2.9 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server'
|
|
import { getServerSession } from 'next-auth'
|
|
import { authOptions } from '@/lib/auth'
|
|
import { prisma } from '@/lib/db'
|
|
|
|
export async function GET(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session?.user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const { id } = await params
|
|
|
|
const notes = await prisma.taskNote.findMany({
|
|
where: { taskId: id },
|
|
include: { user: { select: { id: true, displayName: true, email: true } } },
|
|
orderBy: { createdAt: 'asc' },
|
|
})
|
|
|
|
return NextResponse.json(notes)
|
|
} catch (error) {
|
|
console.error('Task notes GET error:', error)
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
}
|
|
|
|
export async function POST(
|
|
request: NextRequest,
|
|
{ params }: { params: Promise<{ id: string }> }
|
|
) {
|
|
try {
|
|
const session = await getServerSession(authOptions)
|
|
if (!session?.user) return NextResponse.json({ error: 'Unauthorized' }, { status: 401 })
|
|
|
|
const { id } = await params
|
|
const userId = (session.user as any).id
|
|
const userRoles = (session.user as any).roles || []
|
|
const isPrivileged = userRoles.includes('Admin') || userRoles.includes('Manager')
|
|
|
|
const task = await prisma.task.findUnique({
|
|
where: { id },
|
|
include: { assignments: { select: { userId: true } } },
|
|
})
|
|
if (!task) return NextResponse.json({ error: 'Task not found' }, { status: 404 })
|
|
|
|
const isAssigned = task.assignments.some((a) => a.userId === userId)
|
|
if (!isPrivileged && !isAssigned) {
|
|
return NextResponse.json({ error: 'Forbidden' }, { status: 403 })
|
|
}
|
|
|
|
const body = await request.json()
|
|
const { content, status } = body
|
|
|
|
if (!content?.trim()) {
|
|
return NextResponse.json({ error: 'Note content is required' }, { status: 400 })
|
|
}
|
|
|
|
const note = await prisma.taskNote.create({
|
|
data: { taskId: id, userId, content: content.trim() },
|
|
include: { user: { select: { id: true, displayName: true, email: true } } },
|
|
})
|
|
|
|
if (status && ['COMPLETED', 'NOT_STARTED', 'IN_PROGRESS'].includes(status)) {
|
|
await prisma.task.update({
|
|
where: { id },
|
|
data: {
|
|
status,
|
|
...(status === 'COMPLETED'
|
|
? { completedAt: new Date(), completedBy: userId }
|
|
: { completedAt: null, completedBy: null }),
|
|
},
|
|
})
|
|
}
|
|
|
|
await prisma.auditLog.create({
|
|
data: {
|
|
userId,
|
|
action: 'TASK_NOTE_ADDED',
|
|
entityType: 'Task',
|
|
entityId: id,
|
|
newValues: { noteId: note.id, statusChange: status ?? null },
|
|
},
|
|
})
|
|
|
|
return NextResponse.json(note, { status: 201 })
|
|
} catch (error) {
|
|
console.error('Task notes POST error:', error)
|
|
return NextResponse.json({ error: 'Internal server error' }, { status: 500 })
|
|
}
|
|
}
|