diff --git a/backend/src/routes/exam-prep.js b/backend/src/routes/exam-prep.js index 68b55e3..65cc5a7 100644 --- a/backend/src/routes/exam-prep.js +++ b/backend/src/routes/exam-prep.js @@ -227,6 +227,34 @@ const SQL = { ORDER BY started_at DESC LIMIT ? `), + + /* ── Study plan (F10) ───────────────────────────────────────── */ + getPlan: db.prepare(` + SELECT exam_date, daily_target, weak_focus, created_at, updated_at + FROM exam_user_plan + WHERE user_id = ? AND exam_key = ? + `), + upsertPlan: db.prepare(` + INSERT INTO exam_user_plan + (user_id, exam_key, exam_date, daily_target, weak_focus, created_at, updated_at) + VALUES (?, ?, ?, ?, ?, ?, ?) + ON CONFLICT(user_id, exam_key) DO UPDATE SET + exam_date = excluded.exam_date, + daily_target = excluded.daily_target, + weak_focus = excluded.weak_focus, + updated_at = excluded.updated_at + `), + deletePlan: db.prepare(`DELETE FROM exam_user_plan WHERE user_id = ? AND exam_key = ?`), + + /* Tasks solved today (distinct, mc+open+long); used by "today" progress. */ + tasksSolvedToday: db.prepare(` + SELECT COUNT(DISTINCT a.exam_task_id) AS solved + FROM exam_attempts a + JOIN exam_tasks t ON t.id = a.exam_task_id + WHERE a.user_id = ? AND t.exam_key = ? + AND a.is_correct = 1 + AND DATE(a.created_at / 1000, 'unixepoch') = DATE('now') + `), }; /* ── GET /api/exam-prep/tracks ── @@ -505,6 +533,110 @@ function stripPreview(html) { return text.length > 100 ? text.slice(0, 100) + '…' : text; } +/* ────────────────────────────────────────────────────────────────── + Study plan (F10) — by exam date + ────────────────────────────────────────────────────────────────── */ + +function isIsoDate(s) { + return typeof s === 'string' && /^\d{4}-\d{2}-\d{2}$/.test(s); +} + +function daysBetween(yyyymmdd) { + // Calendar days from today to yyyymmdd (UTC). Negative if in past. + const [y, m, d] = yyyymmdd.split('-').map(Number); + const target = Date.UTC(y, m - 1, d); + const today = new Date(); + const todayStart = Date.UTC(today.getUTCFullYear(), today.getUTCMonth(), today.getUTCDate()); + return Math.round((target - todayStart) / 86400000); +} + +function buildPlanPayload(row, userId, examKey) { + if (!row) return { plan: null }; + + const counts = SQL.countTasks.get(examKey); + const progress = SQL.userProgress.get(userId, examKey); + const todayRow = SQL.tasksSolvedToday.get(userId, examKey); + + const daysLeft = daysBetween(row.exam_date); + const tasksLeft = Math.max(0, counts.total - progress.tasks_solved); + + // Auto daily target if user hasn't customized: ceil(left / days) + let dailyTarget = row.daily_target; + if (!dailyTarget && daysLeft > 0) { + dailyTarget = Math.max(5, Math.min(50, Math.ceil(tasksLeft / daysLeft))); + } + + return { + plan: { + exam_date: row.exam_date, + daily_target: dailyTarget, + weak_focus: !!row.weak_focus, + created_at: row.created_at, + updated_at: row.updated_at, + days_left: daysLeft, + tasks_left: tasksLeft, + today: { + solved: todayRow.solved, + target: dailyTarget, + }, + }, + }; +} + +/* ── GET /api/exam-prep/:examKey/plan ── + Returns { plan: null } if user has no plan for this track. */ +// @public-by-design: router-level authMiddleware (line 6) covers this route +router.get('/:examKey/plan', (req, res) => { + const { examKey } = req.params; + if (!SQL.getTrack.get(examKey)) return res.status(404).json({ error: 'Unknown exam track' }); + const row = SQL.getPlan.get(req.user.id, examKey); + res.json(buildPlanPayload(row, req.user.id, examKey)); +}); + +/* ── PUT /api/exam-prep/:examKey/plan ── + Body: { exam_date: 'YYYY-MM-DD', daily_target?: int 5-50, weak_focus?: 0|1 } + Upserts the plan. exam_date is required; the other fields are optional. */ +// @public-by-design: router-level authMiddleware (line 6) covers this route +router.put('/:examKey/plan', (req, res) => { + const { examKey } = req.params; + if (!SQL.getTrack.get(examKey)) return res.status(404).json({ error: 'Unknown exam track' }); + + const examDate = req.body?.exam_date; + if (!isIsoDate(examDate)) return res.status(400).json({ error: 'exam_date must be YYYY-MM-DD' }); + + let dailyTarget = req.body?.daily_target; + if (dailyTarget != null) { + dailyTarget = Number(dailyTarget); + if (!Number.isInteger(dailyTarget) || dailyTarget < 5 || dailyTarget > 50) { + return res.status(400).json({ error: 'daily_target must be integer 5-50' }); + } + } else { + dailyTarget = 10; // sensible default; client can override + } + + const weakFocus = req.body?.weak_focus ? 1 : 0; + + const existing = SQL.getPlan.get(req.user.id, examKey); + const now = Date.now(); + SQL.upsertPlan.run( + req.user.id, examKey, examDate, dailyTarget, weakFocus, + existing ? existing.created_at : now, now + ); + + const row = SQL.getPlan.get(req.user.id, examKey); + res.json(buildPlanPayload(row, req.user.id, examKey)); +}); + +/* ── DELETE /api/exam-prep/:examKey/plan ── + Remove the user's plan for this track. */ +// @public-by-design: router-level authMiddleware (line 6) covers this route +router.delete('/:examKey/plan', (req, res) => { + const { examKey } = req.params; + if (!SQL.getTrack.get(examKey)) return res.status(404).json({ error: 'Unknown exam track' }); + SQL.deletePlan.run(req.user.id, examKey); + res.json({ ok: true }); +}); + /* ────────────────────────────────────────────────────────────────── Mock exam (F9) ────────────────────────────────────────────────────────────────── */ diff --git a/frontend/css/exam-prep.css b/frontend/css/exam-prep.css index f6eec77..833b8e7 100644 --- a/frontend/css/exam-prep.css +++ b/frontend/css/exam-prep.css @@ -830,6 +830,77 @@ font-size: .72rem; color: var(--text-3); } +/* ═══════════════════════════════════════════════════════════════ + Plan widget (`dh-plan-*`) — used by dashboard (F10) + ═══════════════════════════════════════════════════════════════ */ + +.dh-plan-head { + display: flex; align-items: center; justify-content: space-between; + margin-bottom: 14px; +} +.dh-plan-head h3 { margin-bottom: 0; } +.dh-plan-edit { + width: 32px; height: 32px; + border: 1.5px solid var(--border-h); + border-radius: 8px; + background: var(--surface); + color: var(--text-2); + cursor: pointer; + display: flex; align-items: center; justify-content: center; + transition: all .15s; +} +.dh-plan-edit:hover { border-color: var(--violet); color: var(--violet); } +.dh-plan-edit svg { width: 14px; height: 14px; stroke-width: 2; } + +.dh-plan-grid { + display: grid; + grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); + gap: 12px; +} +.dh-plan-stat { + background: rgba(155,93,229,.05); + border-radius: 10px; + padding: 14px 16px; +} + +.dh-plan-expired { + margin-top: 12px; padding: 10px 14px; + background: rgba(248,150,30,.10); color: #B45309; + border-left: 3px solid #F8961E; + border-radius: 6px; + font-size: .82rem; +} + +/* Modal form */ +.dh-plan-form { + display: flex; flex-direction: column; gap: 14px; +} +.dh-plan-field label { + display: block; font-size: .78rem; font-weight: 700; color: var(--text-2); + text-transform: uppercase; letter-spacing: .04em; margin-bottom: 6px; +} +.dh-plan-input { + width: 100%; + padding: 9px 12px; + border: 1.5px solid var(--border-h); + border-radius: 9px; + background: var(--surface); color: var(--text); + font-family: 'Manrope', sans-serif; font-size: .92rem; +} +.dh-plan-input:focus { outline: none; border-color: var(--violet); } +.dh-plan-hint { font-size: .72rem; color: var(--text-3); margin-top: 4px; } + +.dh-plan-derived-card { + display: flex; flex-direction: column; gap: 5px; + padding: 10px 12px; + background: rgba(6,214,160,.06); + border-left: 3px solid #06D6A0; + border-radius: 6px; + font-size: .85rem; + color: var(--text-2); +} +.dh-plan-derived-card b { color: var(--text); } + /* ── Mobile tweaks ─────────────────────────────────────────────── */ @media (max-width: 640px) { .ep-wrap { padding: 20px 16px 60px; } diff --git a/frontend/js/exam-prep/api.js b/frontend/js/exam-prep/api.js index 126e4a6..8111e88 100644 --- a/frontend/js/exam-prep/api.js +++ b/frontend/js/exam-prep/api.js @@ -22,6 +22,7 @@ getDashboard: (examKey) => LS.api(`${base(examKey)}/dashboard`), getPlan: (examKey) => LS.api(`${base(examKey)}/plan`), savePlan: (examKey, body) => LS.api(`${base(examKey)}/plan`, { method: 'PUT', body }), + deletePlan: (examKey) => LS.api(`${base(examKey)}/plan`, { method: 'DELETE' }), saveAttempt: (body) => LS.api(`/api/exam-prep/attempts`, { method: 'POST', body }), startMock: (examKey, body) => LS.api(`${base(examKey)}/mock/start`, { method: 'POST', body }), mockAnswer: (mockId, body) => LS.api(`/api/exam-prep/mock/${mockId}/answer`, { method: 'POST', body }), diff --git a/frontend/js/exam-prep/dashboard.js b/frontend/js/exam-prep/dashboard.js index 1c1e3a3..c234578 100644 --- a/frontend/js/exam-prep/dashboard.js +++ b/frontend/js/exam-prep/dashboard.js @@ -27,8 +27,9 @@ ? Math.round((progress.correct_attempts / progress.total_attempts) * 100) : null; - // Fetch F4 live aggregates in parallel with rendering shell. + // Fetch F4 live aggregates + F10 plan in parallel with rendering shell. const dashPromise = EP.api.getDashboard(track.exam_key).catch(() => null); + const planPromise = EP.api.getPlan(track.exam_key).catch(() => null); main.innerHTML = `