c4645e3475
Phase 1 — Quick wins: - Image lazy-loading on series/resource cards - Newsletter signup added to Episodes page (before highlights) - Per-route meta tags via usePageMeta hook (title, og:title, og:description) - Breadcrumbs on study index, section, and notes pages - SVG completion checkmark badges on study section list - Analytics time-range filter (7d / 30d / 90d) in admin panel Phase 2 — Medium features: - Related episodes on archived series detail pages - Resource library two-tier filter (type + tag chips) - Global search (Fuse.js) moved below sticky header as full-width bar - Q&A anonymous upvoting with localStorage dedup + admin pin/unpin - Study enrollment funnel tracking (firstVisitAt, firstCompletionAt) with funnel chart in analytics Phase 3 — Larger features: - Study section comments (auto-approve for enrolled users, admin moderation panel) - Study completion certificate (canvas render, PNG download, shareable public URL) - Episode script full-text search (mammoth docx extraction, server-side search, admin upload UI) - Reflection questions renamed from Discussion Questions; quiz answers can be shared to section discussion - Public certificate route at /certificate/:token with og meta tags - Comment moderation panel added to admin under Manage > Study Comments Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
116 lines
4.4 KiB
JavaScript
116 lines
4.4 KiB
JavaScript
import { randomUUID } from 'node:crypto'
|
|
import { requireAdminAuth } from '../auth.js'
|
|
import { requireStudyAuth, normalizeStudySlug, isStudyUserEnrolled, getStudyTitleBySlug } from '../study-helpers.js'
|
|
import { state } from '../state.js'
|
|
import { loadUserProgress, queueStudyCertificatesWrite } from '../data.js'
|
|
|
|
/**
|
|
* Returns the list of required section IDs for a study (only released sections).
|
|
*/
|
|
function getRequiredSectionIds(studySlug) {
|
|
const content = state.cachedSiteContent
|
|
if (!content || !Array.isArray(content.studies)) return null
|
|
const study = content.studies.find(s => s?.slug === studySlug)
|
|
if (!study || !Array.isArray(study.sections)) return null
|
|
return study.sections
|
|
.filter(s => s?.status !== 'unreleased' && s?.id)
|
|
.map(s => s.id)
|
|
}
|
|
|
|
export function register(app) {
|
|
// GET — check eligibility and return existing certificate token if one exists
|
|
app.get('/api/study-certificate/:studySlug', requireStudyAuth, async (req, res) => {
|
|
const user = req.studyUser
|
|
const studySlug = normalizeStudySlug(req.params.studySlug)
|
|
if (!studySlug) { res.status(400).json({ message: 'Invalid study slug.' }); return }
|
|
|
|
if (!isStudyUserEnrolled(user, studySlug)) {
|
|
res.status(403).json({ message: 'Not enrolled in this study.' }); return
|
|
}
|
|
|
|
const requiredIds = getRequiredSectionIds(studySlug)
|
|
if (!requiredIds || requiredIds.length === 0) {
|
|
res.status(404).json({ message: 'Study sections not found.' }); return
|
|
}
|
|
|
|
const progress = await loadUserProgress(user.id)
|
|
const completedIds = progress.byStudy[studySlug]?.completedSectionIds ?? []
|
|
const eligible = requiredIds.every(id => completedIds.includes(id))
|
|
|
|
const existing = state.studyCertificates.find(c => c.userId === user.id && c.studySlug === studySlug)
|
|
|
|
res.json({
|
|
eligible,
|
|
studyTitle: getStudyTitleBySlug(studySlug) || studySlug,
|
|
completedCount: completedIds.filter(id => requiredIds.includes(id)).length,
|
|
totalCount: requiredIds.length,
|
|
token: existing?.token ?? null,
|
|
issuedAt: existing?.issuedAt ?? null,
|
|
})
|
|
})
|
|
|
|
// POST — issue/re-issue a certificate (must be eligible)
|
|
app.post('/api/study-certificate/:studySlug', requireStudyAuth, async (req, res) => {
|
|
const user = req.studyUser
|
|
const studySlug = normalizeStudySlug(req.params.studySlug)
|
|
if (!studySlug) { res.status(400).json({ message: 'Invalid study slug.' }); return }
|
|
|
|
if (!isStudyUserEnrolled(user, studySlug)) {
|
|
res.status(403).json({ message: 'Not enrolled in this study.' }); return
|
|
}
|
|
|
|
const requiredIds = getRequiredSectionIds(studySlug)
|
|
if (!requiredIds || requiredIds.length === 0) {
|
|
res.status(404).json({ message: 'Study sections not found.' }); return
|
|
}
|
|
|
|
const progress = await loadUserProgress(user.id)
|
|
const completedIds = progress.byStudy[studySlug]?.completedSectionIds ?? []
|
|
const eligible = requiredIds.every(id => completedIds.includes(id))
|
|
|
|
if (!eligible) {
|
|
res.status(403).json({ message: 'Complete all sections to earn your certificate.' }); return
|
|
}
|
|
|
|
// Find or create
|
|
let cert = state.studyCertificates.find(c => c.userId === user.id && c.studySlug === studySlug)
|
|
if (!cert) {
|
|
cert = {
|
|
id: randomUUID(),
|
|
token: randomUUID(),
|
|
userId: user.id,
|
|
studySlug,
|
|
studyTitle: getStudyTitleBySlug(studySlug) || studySlug,
|
|
displayName: user.displayName || user.username,
|
|
issuedAt: new Date().toISOString(),
|
|
}
|
|
state.studyCertificates.push(cert)
|
|
queueStudyCertificatesWrite()
|
|
}
|
|
|
|
res.json({ ok: true, token: cert.token, issuedAt: cert.issuedAt })
|
|
})
|
|
|
|
// GET — public certificate page data (no auth, by token)
|
|
app.get('/api/public/certificate/:token', (req, res) => {
|
|
const { token } = req.params
|
|
if (!token || typeof token !== 'string' || token.length > 100) {
|
|
res.status(400).json({ message: 'Invalid token.' }); return
|
|
}
|
|
|
|
const cert = state.studyCertificates.find(c => c.token === token)
|
|
if (!cert) { res.status(404).json({ message: 'Certificate not found.' }); return }
|
|
|
|
res.json({
|
|
studyTitle: cert.studyTitle,
|
|
displayName: cert.displayName,
|
|
issuedAt: cert.issuedAt,
|
|
})
|
|
})
|
|
|
|
// Admin: list all certificates
|
|
app.get('/api/admin-study-certificates', requireAdminAuth, (_req, res) => {
|
|
res.json({ certificates: state.studyCertificates })
|
|
})
|
|
}
|