Skip to content

Commit 34cf44a

Browse files
committed
feat: add manual test call button for assessment
Adds a "Test Call Now" button on the assessment setup page and a trigger API endpoint to bypass cron scheduling for debugging.
1 parent 461ab34 commit 34cf44a

2 files changed

Lines changed: 110 additions & 0 deletions

File tree

  • src/app
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
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+
}

src/app/elderly/[id]/assessment/page.tsx

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,25 @@ export default function AssessmentPage() {
146146
}
147147
}
148148

149+
const [triggering, setTriggering] = useState(false);
150+
151+
async function triggerTestCall() {
152+
setTriggering(true);
153+
setError("");
154+
setSuccess("");
155+
try {
156+
const res = await apiFetch(`/api/elderly/${id}/assessment/trigger`, { method: "POST" });
157+
const data = await res.json();
158+
if (!res.ok) throw new Error(data.error || "Failed to trigger");
159+
setSuccess("Assessment call triggered! You should receive a call shortly.");
160+
fetchData();
161+
} catch (err) {
162+
setError(err instanceof Error ? err.message : "Failed to trigger call");
163+
} finally {
164+
setTriggering(false);
165+
}
166+
}
167+
149168
const filledCount = questions.filter((q) => q.correctAnswer.trim()).length;
150169
const latestSession = sessions[0];
151170

@@ -234,6 +253,13 @@ export default function AssessmentPage() {
234253
>
235254
{config?.active ? "Deactivate" : "Activate"}
236255
</button>
256+
<button
257+
onClick={triggerTestCall}
258+
disabled={triggering || filledCount < 10}
259+
className="rounded-lg bg-indigo-100 px-4 py-2 text-sm font-medium text-indigo-700 hover:bg-indigo-200 transition-colors disabled:opacity-50 disabled:cursor-not-allowed"
260+
>
261+
{triggering ? "Calling..." : "Test Call Now"}
262+
</button>
237263
</div>
238264
{!config?.active && filledCount < 10 && (
239265
<p className="mt-2 text-xs text-amber-600">

0 commit comments

Comments
 (0)