135 lines
3.4 KiB
TypeScript
135 lines
3.4 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'
|
|
import { addDays } from 'date-fns'
|
|
|
|
// GET /api/workspaces/[id]/treatment-plan
|
|
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 })
|
|
}
|
|
|
|
const plan = await prisma.treatmentPlan.findUnique({
|
|
where: { workspaceId },
|
|
include: {
|
|
milestones: {
|
|
orderBy: { cycleNumber: 'asc' }
|
|
}
|
|
}
|
|
})
|
|
|
|
if (!plan) {
|
|
return NextResponse.json({ plan: null })
|
|
}
|
|
|
|
return NextResponse.json({ plan })
|
|
} catch (error) {
|
|
console.error('Failed to fetch treatment plan:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to fetch treatment plan' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|
|
|
|
// POST /api/workspaces/[id]/treatment-plan
|
|
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 { title, totalCycles, startDate, cycleType, cycleDays } = body
|
|
|
|
// Validate required fields
|
|
if (!title || !totalCycles || !startDate) {
|
|
return NextResponse.json(
|
|
{ error: 'Missing required fields' },
|
|
{ status: 400 }
|
|
)
|
|
}
|
|
|
|
// Calculate estimated end date
|
|
const start = new Date(startDate)
|
|
const estimatedEnd = addDays(start, cycleDays * totalCycles)
|
|
|
|
// Create or update treatment plan
|
|
const plan = await prisma.treatmentPlan.upsert({
|
|
where: { workspaceId },
|
|
create: {
|
|
workspaceId,
|
|
title,
|
|
totalCycles,
|
|
startDate: start,
|
|
estimatedEnd,
|
|
cycleType,
|
|
cycleDays,
|
|
createdById: user.id,
|
|
milestones: {
|
|
create: Array.from({ length: totalCycles }, (_, i) => ({
|
|
cycleNumber: i + 1,
|
|
date: addDays(start, cycleDays * i),
|
|
status: i === 0 ? 'UPCOMING' : 'UPCOMING'
|
|
}))
|
|
}
|
|
},
|
|
update: {
|
|
title,
|
|
totalCycles,
|
|
startDate: start,
|
|
estimatedEnd,
|
|
cycleType,
|
|
cycleDays,
|
|
},
|
|
include: {
|
|
milestones: {
|
|
orderBy: { cycleNumber: 'asc' }
|
|
}
|
|
}
|
|
})
|
|
|
|
// Log audit
|
|
await prisma.auditLog.create({
|
|
data: {
|
|
workspaceId,
|
|
userId: user.id,
|
|
action: 'CREATE',
|
|
entityType: 'TREATMENT_PLAN',
|
|
entityId: plan.id,
|
|
details: { title, totalCycles }
|
|
}
|
|
})
|
|
|
|
return NextResponse.json({ plan })
|
|
} catch (error) {
|
|
console.error('Failed to create treatment plan:', error)
|
|
return NextResponse.json(
|
|
{ error: 'Failed to create treatment plan' },
|
|
{ status: 500 }
|
|
)
|
|
}
|
|
}
|