AI Newsletter Digest improvements: fixed QP soft line break decoding, URL extraction, and content cleaning
This commit is contained in:
@@ -0,0 +1,111 @@
|
||||
import { NextRequest, NextResponse } from 'next/server'
|
||||
import { prisma } from '@/lib/db/prisma'
|
||||
import { getCurrentUser } from '@/lib/auth'
|
||||
import { canAccessWorkspace } from '@/lib/db/workspace-access'
|
||||
|
||||
// POST /api/workspaces/[id]/treatment-plan/complete-cycle
|
||||
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 })
|
||||
}
|
||||
|
||||
// Get current plan
|
||||
const plan = await prisma.treatmentPlan.findUnique({
|
||||
where: { workspaceId },
|
||||
include: { milestones: true }
|
||||
})
|
||||
|
||||
if (!plan) {
|
||||
return NextResponse.json(
|
||||
{ error: 'No treatment plan found' },
|
||||
{ status: 404 }
|
||||
)
|
||||
}
|
||||
|
||||
if (plan.status !== 'ACTIVE') {
|
||||
return NextResponse.json(
|
||||
{ error: 'Treatment plan is not active' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
if (plan.currentCycle >= plan.totalCycles) {
|
||||
return NextResponse.json(
|
||||
{ error: 'All cycles already completed' },
|
||||
{ status: 400 }
|
||||
)
|
||||
}
|
||||
|
||||
const nextCycle = plan.currentCycle + 1
|
||||
|
||||
// Update milestone
|
||||
const milestone = plan.milestones.find(m => m.cycleNumber === nextCycle)
|
||||
if (milestone) {
|
||||
await prisma.treatmentMilestone.update({
|
||||
where: { id: milestone.id },
|
||||
data: {
|
||||
status: 'COMPLETED',
|
||||
date: new Date()
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
// Update plan
|
||||
const isComplete = nextCycle >= plan.totalCycles
|
||||
const updatedPlan = await prisma.treatmentPlan.update({
|
||||
where: { workspaceId },
|
||||
data: {
|
||||
currentCycle: nextCycle,
|
||||
status: isComplete ? 'COMPLETED' : 'ACTIVE'
|
||||
},
|
||||
include: {
|
||||
milestones: {
|
||||
orderBy: { cycleNumber: 'asc' }
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
// Log audit
|
||||
await prisma.auditLog.create({
|
||||
data: {
|
||||
workspaceId,
|
||||
userId: user.id,
|
||||
action: 'COMPLETE_CYCLE',
|
||||
entityType: 'TREATMENT_PLAN',
|
||||
entityId: plan.id,
|
||||
details: {
|
||||
cycleCompleted: nextCycle,
|
||||
totalCycles: plan.totalCycles,
|
||||
isComplete
|
||||
}
|
||||
}
|
||||
})
|
||||
|
||||
return NextResponse.json({
|
||||
plan: updatedPlan,
|
||||
milestone: milestone,
|
||||
celebration: {
|
||||
cycleNumber: nextCycle,
|
||||
isHalfway: nextCycle === Math.floor(plan.totalCycles / 2),
|
||||
isFinal: isComplete
|
||||
}
|
||||
})
|
||||
} catch (error) {
|
||||
console.error('Failed to complete cycle:', error)
|
||||
return NextResponse.json(
|
||||
{ error: 'Failed to complete cycle' },
|
||||
{ status: 500 }
|
||||
)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user