|
| 1 | +import { NextRequest, NextResponse } from "next/server"; |
| 2 | +import { getCurrentUser } from "@/lib/auth"; |
| 3 | +import { getProfileAccess } from "@/lib/access"; |
| 4 | +import { prisma } from "@/lib/prisma"; |
| 5 | +import { executeAssessmentCall } from "@/lib/assessment-call"; |
| 6 | + |
| 7 | +export async function POST( |
| 8 | + _req: NextRequest, |
| 9 | + { params }: { params: Promise<{ id: string }> } |
| 10 | +) { |
| 11 | + const user = await getCurrentUser(); |
| 12 | + if (!user) return NextResponse.json({ error: "Unauthorized" }, { status: 401 }); |
| 13 | + |
| 14 | + const { id } = await params; |
| 15 | + const role = await getProfileAccess(user, id); |
| 16 | + if (!role) return NextResponse.json({ error: "Not found" }, { status: 404 }); |
| 17 | + |
| 18 | + const profile = await prisma.elderlyProfile.findUnique({ where: { id } }); |
| 19 | + if (!profile || !profile.phoneVerified) { |
| 20 | + return NextResponse.json({ error: "Phone not verified" }, { status: 400 }); |
| 21 | + } |
| 22 | + |
| 23 | + const config = await prisma.assessmentConfig.findUnique({ |
| 24 | + where: { elderlyProfileId: id }, |
| 25 | + }); |
| 26 | + |
| 27 | + if (!config) { |
| 28 | + return NextResponse.json({ error: "No assessment config found" }, { status: 400 }); |
| 29 | + } |
| 30 | + |
| 31 | + const allQuestions = await prisma.assessmentQuestion.findMany({ |
| 32 | + where: { |
| 33 | + elderlyProfileId: id, |
| 34 | + correctAnswer: { not: "" }, |
| 35 | + }, |
| 36 | + }); |
| 37 | + |
| 38 | + if (allQuestions.length < 10) { |
| 39 | + return NextResponse.json({ |
| 40 | + error: `Only ${allQuestions.length} questions with answers (need 10+)`, |
| 41 | + questions: allQuestions.length, |
| 42 | + }, { status: 400 }); |
| 43 | + } |
| 44 | + |
| 45 | + const todayStr = new Date().toLocaleDateString("en-CA"); |
| 46 | + |
| 47 | + // Clean up any existing session for today |
| 48 | + const existing = await prisma.assessmentSession.findFirst({ |
| 49 | + where: { configId: config.id, date: todayStr }, |
| 50 | + }); |
| 51 | + if (existing) { |
| 52 | + await prisma.assessmentAnswer.deleteMany({ where: { sessionId: existing.id } }); |
| 53 | + await prisma.assessmentSession.delete({ where: { id: existing.id } }); |
| 54 | + } |
| 55 | + |
| 56 | + const shuffled = allQuestions.sort(() => Math.random() - 0.5); |
| 57 | + const selected = shuffled.slice(0, config.questionsPerCall); |
| 58 | + |
| 59 | + const session = await prisma.assessmentSession.create({ |
| 60 | + data: { |
| 61 | + elderlyProfileId: id, |
| 62 | + configId: config.id, |
| 63 | + date: todayStr, |
| 64 | + status: "PENDING", |
| 65 | + answers: { |
| 66 | + create: selected.map((q) => ({ |
| 67 | + questionId: q.id, |
| 68 | + questionText: q.questionText, |
| 69 | + correctAnswer: q.correctAnswer, |
| 70 | + })), |
| 71 | + }, |
| 72 | + }, |
| 73 | + }); |
| 74 | + |
| 75 | + try { |
| 76 | + await executeAssessmentCall(session.id); |
| 77 | + return NextResponse.json({ ok: true, sessionId: session.id }); |
| 78 | + } catch (err) { |
| 79 | + return NextResponse.json({ |
| 80 | + error: "Call failed", |
| 81 | + details: err instanceof Error ? err.message : String(err), |
| 82 | + }, { status: 500 }); |
| 83 | + } |
| 84 | +} |
0 commit comments