feat(quizzes): публичные ссылки по токену и флаг no_shuffle

- quizzes.public_token (VARCHAR 32, уникальный индекс) вместо числового id в публичных роутах /api/public-quizzes/:token
- бэкилл токенов для существующих строк в db/migration.sql + ensureQuizTables
- quiz_questions.no_shuffle — вопрос исключён из перемешивания
- POST /api/quizzes/:id/public-token/rotate — сброс утёкшей ссылки (автор/админ)
- restore: no_shuffle в INSERT quiz_questions, backup.selftest обновлён
- документация AGENTS.md + handoff
This commit is contained in:
dev
2026-12-03 00:33:50 +03:00
parent a92fa830a2
commit d9b785d36c
13 changed files with 157 additions and 58 deletions
+24 -12
View File
@@ -83,6 +83,7 @@ const {
quizError,
listQuizzes,
createQuiz,
rotatePublicToken,
getQuiz,
saveQuiz,
softDeleteQuiz,
@@ -8255,25 +8256,25 @@ app.post('/api/restore', requireAdmin, backupUploadGuard, uploadBackup.single('b
}
for (const x of (ndata.quizzes || [])) {
await client.query(
'INSERT INTO quizzes (id, title, description, kind, is_published, shuffle_questions, shuffle_options, show_results, max_attempts, time_limit_min, pass_percent, cover_path, allow_anonymous, author_id, created_at, updated_at, deleted_at, purge_at, show_answers, grade_scale_id, access_code, retry_cooldown_min, stats_public) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,(SELECT id FROM users WHERE id = $14),$15,$16,$17,$18,$19,(SELECT id FROM quiz_grade_scales WHERE id = $20),$21,$22,$23)',
'INSERT INTO quizzes (id, title, description, kind, is_published, shuffle_questions, shuffle_options, show_results, max_attempts, time_limit_min, pass_percent, cover_path, allow_anonymous, author_id, created_at, updated_at, deleted_at, purge_at, show_answers, grade_scale_id, access_code, retry_cooldown_min, stats_public, public_token) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11,$12,$13,(SELECT id FROM users WHERE id = $14),$15,$16,$17,$18,$19,(SELECT id FROM quiz_grade_scales WHERE id = $20),$21,$22,$23,$24)',
[x.id, x.title, x.description, x.kind, x.is_published === true, x.shuffle_questions === true,
x.shuffle_options === true, x.show_results !== false, x.max_attempts, x.time_limit_min,
x.pass_percent, x.cover_path, x.allow_anonymous === true, x.author_id,
x.created_at || null, x.updated_at || null,
x.deleted_at || null, x.purge_at || null,
x.show_answers !== false, x.grade_scale_id, x.access_code || null, x.retry_cooldown_min || 0,
x.stats_public === true]
x.stats_public === true, x.public_token]
);
}
const restoredQuizIds = new Set((ndata.quizzes || []).map(x => x.id));
for (const x of (ndata.quiz_questions || [])) {
if (!restoredQuizIds.has(x.quiz_id)) continue;
await client.query(
'INSERT INTO quiz_questions (id, quiz_id, sort_order, kind, text, description, weight, partial_credit, required, text_match, accepted_answers, min_length, options_inline, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11::text[],$12,$13,$14,$15)',
'INSERT INTO quiz_questions (id, quiz_id, sort_order, kind, text, description, weight, partial_credit, required, text_match, accepted_answers, min_length, options_inline, no_shuffle, created_at, updated_at) VALUES ($1,$2,$3,$4,$5,$6,$7,$8,$9,$10,$11::text[],$12,$13,$14,$15,$16)',
[x.id, x.quiz_id, x.sort_order, x.kind, x.text, x.description, x.weight,
x.partial_credit === true, x.required !== false, x.text_match,
Array.isArray(x.accepted_answers) ? x.accepted_answers : [],
x.min_length || 0, x.options_inline === true,
x.min_length || 0, x.options_inline === true, x.no_shuffle === true,
x.created_at || null, x.updated_at || null]
);
}
@@ -15749,6 +15750,17 @@ app.put('/api/quizzes/:id', requireAuth, async (req, res) => {
}
});
app.post('/api/quizzes/:id/public-token/rotate', requireAuth, async (req, res) => {
try {
const result = await rotatePublicToken(pool, reqInt(req.params.id), req.user);
await logAudit(req, 'quiz.public-token.rotate', { id: result.id });
broadcastQuizzesChanged();
res.json(result);
} catch (e) {
quizError(res, e, 'Не удалось обновить ссылку', 'quiz public token rotate');
}
});
app.delete('/api/quizzes/:id', requireAuth, async (req, res) => {
try {
const result = await softDeleteQuiz(pool, reqInt(req.params.id), req.user, await trashPurgeDays());
@@ -15893,17 +15905,17 @@ app.get('/api/quizzes/:id/stats', requireAuth, async (req, res) => {
}
});
app.get('/api/public-quizzes/:id', apiLimiter, async (req, res) => {
app.get('/api/public-quizzes/:token', apiLimiter, async (req, res) => {
try {
res.json(await getPublicQuiz(pool, reqInt(req.params.id)));
res.json(await getPublicQuiz(pool, req.params.token));
} catch (e) {
quizError(res, e, 'Тест не найден', 'public quiz get');
}
});
app.post('/api/public-quizzes/:id/attempts', apiLimiter, async (req, res) => {
app.post('/api/public-quizzes/:token/attempts', apiLimiter, async (req, res) => {
try {
res.json(await startPublicAttempt(pool, reqInt(req.params.id), {
res.json(await startPublicAttempt(pool, req.params.token, {
name: req.body?.name,
code: typeof req.body?.code === 'string' ? req.body.code : '',
}));
@@ -15912,12 +15924,12 @@ app.post('/api/public-quizzes/:id/attempts', apiLimiter, async (req, res) => {
}
});
app.put('/api/public-quizzes/:id/attempts/:attemptId', apiLimiter, async (req, res) => {
app.put('/api/public-quizzes/:token/attempts/:attemptId', apiLimiter, async (req, res) => {
try {
const token = typeof req.body?.token === 'string' ? req.body.token : '';
const result = await submitPublicAttempt(
pool,
reqInt(req.params.id),
req.params.token,
reqInt(req.params.attemptId),
token,
req.body || {}
@@ -15933,9 +15945,9 @@ app.put('/api/public-quizzes/:id/attempts/:attemptId', apiLimiter, async (req, r
}
});
app.get('/api/public-quizzes/:id/options', quizLookupLimiter, async (req, res) => {
app.get('/api/public-quizzes/:token/options', quizLookupLimiter, async (req, res) => {
try {
const result = await lookupPublicQuizOptions(pool, reqInt(req.params.id), {
const result = await lookupPublicQuizOptions(pool, req.params.token, {
field: req.query.field,
query: req.query.q,
code: req.query.code,