feat: fase 2+3 — expansion masiva multi-perfil + bug fixes + hardening
Fase 2 (expansion): - Francesca: +6 etapas matematicas (5-10), +7 etapas lectura (3-9), +2 ortografia - Sebastian: +8 etapas matematicas (4-11), +6 lectura, +3 historia, +3 geografia, +1 banderas - Nuevas paginas: ortografia, lectura, geografia, matematicas - ~900 ejercicios nuevos, paridad ~500 por perfil Fase 3 (bug fixes + hardening): - Fix: SessionLog auto-create en grade route - Fix: engine cache (no borrar antes de revisar) - Fix: FSRS cards actualizadas en flujo curriculum - Fix: responseMs real (no hardcodeado 3000) - Fix: options dentro de content en sebastian etapa-1 - Fix: trazo-letra consonantes crash - Security: execFileSync anti-command-injection en TTS - DB: 18 cascade deletes + indices + unique constraints - APIs debug: reset, state, seed - Docker: espeak-ng + prisma + user/group - E2E: 23 tests nuevos (curriculum-flow.spec.ts) - Code review: tailwind colors, middleware dev mode, eslint config Verificado: tsc exit 0, build exit 0, 61 E2E tests pass
This commit is contained in:
@@ -0,0 +1,25 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { prisma } from "@/lib/db"
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { childId } = await request.json()
|
||||
if (!childId) {
|
||||
return NextResponse.json({ error: "childId required" }, { status: 400 })
|
||||
}
|
||||
|
||||
// Delete in dependency order
|
||||
await prisma.skillAttempt.deleteMany({
|
||||
where: { sessionLog: { childId } },
|
||||
})
|
||||
await prisma.sessionLog.deleteMany({ where: { childId } })
|
||||
await prisma.fsrsCard.deleteMany({ where: { childId } })
|
||||
await prisma.curriculumProgress.deleteMany({ where: { childId } })
|
||||
await prisma.milestone.deleteMany({ where: { childId } })
|
||||
|
||||
return NextResponse.json({ success: true, message: `Reset state for child ${childId}` })
|
||||
} catch (error) {
|
||||
console.error("Debug reset error:", error)
|
||||
return NextResponse.json({ error: "Internal error" }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,112 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { prisma } from "@/lib/db"
|
||||
import { getStagesForProfile } from "@/lib/curriculum/engine"
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const { childId, profile = "ISABELLA", topic = "lectura", stages: stageIds, markAllCorrect = false } = await request.json()
|
||||
if (!childId) {
|
||||
return NextResponse.json({ error: "childId required" }, { status: 400 })
|
||||
}
|
||||
|
||||
const child = await prisma.child.findUnique({
|
||||
where: { id: childId },
|
||||
select: { id: true, name: true, profile: true },
|
||||
})
|
||||
|
||||
if (!child) {
|
||||
return NextResponse.json({ error: "Child not found" }, { status: 404 })
|
||||
}
|
||||
|
||||
const resolvedProfile = (profile || child.profile).toUpperCase()
|
||||
|
||||
// Get stages for this topic
|
||||
const stages = await getStagesForProfile(resolvedProfile, topic)
|
||||
|
||||
// Filter to specific stages if requested
|
||||
const stageEntries = stageIds
|
||||
? Object.entries(stages).filter(([id]) => stageIds.includes(Number(id)))
|
||||
: Object.entries(stages)
|
||||
|
||||
let seeded = 0
|
||||
|
||||
for (const [stageId, stage] of stageEntries) {
|
||||
// Ensure curriculum progress exists
|
||||
const exists = await prisma.curriculumProgress.findUnique({
|
||||
where: { childId_topic_stage: { childId, topic, stage: Number(stageId) } },
|
||||
})
|
||||
|
||||
if (!exists) {
|
||||
await prisma.curriculumProgress.create({
|
||||
data: { childId, topic, stage: Number(stageId) },
|
||||
})
|
||||
}
|
||||
|
||||
// Create a session log
|
||||
const sessionLog = await prisma.sessionLog.create({
|
||||
data: { childId, startedAt: new Date() },
|
||||
})
|
||||
|
||||
// Create skill attempts for each exercise in the stage
|
||||
for (const ex of stage.exercises) {
|
||||
await prisma.skillAttempt.create({
|
||||
data: {
|
||||
sessionLogId: sessionLog.id,
|
||||
skillCode: ex.skillCode,
|
||||
promptLevel: 0,
|
||||
correct: markAllCorrect,
|
||||
responseMs: 2000,
|
||||
errorType: markAllCorrect ? null : (ex.errorType || "discriminacion-auditiva"),
|
||||
exerciseId: ex.id,
|
||||
stage: ex.stage,
|
||||
},
|
||||
})
|
||||
|
||||
// Create FSRS card
|
||||
await prisma.fsrsCard.create({
|
||||
data: {
|
||||
childId,
|
||||
skillCode: ex.skillCode,
|
||||
dueAt: new Date(Date.now() + 86400000),
|
||||
stability: markAllCorrect ? 3.0 : 0.5,
|
||||
difficulty: markAllCorrect ? 4.0 : 7.0,
|
||||
reps: 1,
|
||||
lapses: markAllCorrect ? 0 : 1,
|
||||
},
|
||||
})
|
||||
|
||||
seeded++
|
||||
}
|
||||
|
||||
// Mark stage as completed if markAllCorrect
|
||||
if (markAllCorrect) {
|
||||
await prisma.curriculumProgress.updateMany({
|
||||
where: { childId, topic, stage: Number(stageId) },
|
||||
data: { completedAt: new Date() },
|
||||
})
|
||||
|
||||
// Advance to next stage
|
||||
const nextStage = Number(stageId) + 1
|
||||
const nextExists = await prisma.curriculumProgress.findUnique({
|
||||
where: { childId_topic_stage: { childId, topic, stage: nextStage } },
|
||||
})
|
||||
if (!nextExists) {
|
||||
await prisma.curriculumProgress.create({
|
||||
data: { childId, topic, stage: nextStage },
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
child: { id: child.id, name: child.name, profile: resolvedProfile },
|
||||
topic,
|
||||
stagesSeeded: stageEntries.length,
|
||||
exercisesSeeded: seeded,
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Debug seed error:", error)
|
||||
return NextResponse.json({ error: "Internal error" }, { status: 500 })
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
import { NextRequest, NextResponse } from "next/server"
|
||||
import { prisma } from "@/lib/db"
|
||||
import { getStagesForProfile } from "@/lib/curriculum/engine"
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
const childId = request.nextUrl.searchParams.get("childId")
|
||||
const profile = request.nextUrl.searchParams.get("profile") || "ISABELLA"
|
||||
const topic = request.nextUrl.searchParams.get("topic") || "lectura"
|
||||
|
||||
if (!childId) {
|
||||
return NextResponse.json({ error: "childId required" }, { status: 400 })
|
||||
}
|
||||
|
||||
const child = await prisma.child.findUnique({
|
||||
where: { id: childId },
|
||||
select: { id: true, name: true, profile: true },
|
||||
})
|
||||
|
||||
const progress = await prisma.curriculumProgress.findMany({
|
||||
where: { childId },
|
||||
orderBy: [{ topic: "asc" }, { stage: "asc" }],
|
||||
})
|
||||
|
||||
const skillAttempts = await prisma.skillAttempt.findMany({
|
||||
where: { sessionLog: { childId } },
|
||||
orderBy: { createdAt: "desc" },
|
||||
take: 20,
|
||||
})
|
||||
|
||||
const fsrsCards = await prisma.fsrsCard.findMany({
|
||||
where: { childId },
|
||||
orderBy: { dueAt: "asc" },
|
||||
})
|
||||
|
||||
const sessionLogs = await prisma.sessionLog.findMany({
|
||||
where: { childId },
|
||||
orderBy: { startedAt: "desc" },
|
||||
take: 5,
|
||||
})
|
||||
|
||||
// Count mastered exercises for this topic
|
||||
const mastered = new Set(
|
||||
skillAttempts
|
||||
.filter((a) => a.exerciseId && a.exerciseId.startsWith(`${topic[0]}${topic[1]}`) && a.correct)
|
||||
.map((a) => a.exerciseId),
|
||||
)
|
||||
|
||||
const stages = await getStagesForProfile(profile, topic)
|
||||
const totalExercises = Object.values(stages).reduce((sum, s) => sum + s.exercises.length, 0)
|
||||
|
||||
return NextResponse.json({
|
||||
child,
|
||||
progress,
|
||||
skillAttempts: skillAttempts.slice(0, 10),
|
||||
fsrsCards: fsrsCards.slice(0, 10),
|
||||
sessionLogs,
|
||||
stats: {
|
||||
totalExercisesInTopic: totalExercises,
|
||||
masteredInTopic: mastered.size,
|
||||
progressPercent: totalExercises > 0 ? Math.round((mastered.size / totalExercises) * 100) : 0,
|
||||
currentStage: progress.find((p) => !p.completedAt)?.stage || null,
|
||||
currentTopic: progress.find((p) => !p.completedAt)?.topic || null,
|
||||
},
|
||||
})
|
||||
} catch (error) {
|
||||
console.error("Debug state error:", error)
|
||||
return NextResponse.json({ error: "Internal error" }, { status: 500 })
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user