refactor: answer_type single TEXT + new 'question' type

- Convert TEXT[] back to TEXT (take first element of existing arrays)
- Valid values: yes_no, text, question, word
- API validation updated for single string

Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
2026-05-25 16:11:15 +02:00
parent 7c8d5bfaaf
commit b0a67df328
2 changed files with 18 additions and 11 deletions

View File

@@ -2,12 +2,11 @@ const router = require('express').Router();
const { query } = require('../db');
const STATUSES = ['draft', 'blocked', 'published'];
const ANSWER_TYPES = new Set(['yes_no', 'text', 'word']);
const ANSWER_TYPES = new Set(['yes_no', 'text', 'question', 'word']);
function validateAnswerType(val) {
const arr = Array.isArray(val) ? val : (val ? [val] : []);
if (!arr.length) return 'answer_type must be a non-empty array of: yes_no, text, word';
if (!arr.every(t => ANSWER_TYPES.has(t))) return `answer_type values must be: ${[...ANSWER_TYPES].join(', ')}`;
if (!val || typeof val !== 'string') return `answer_type must be one of: ${[...ANSWER_TYPES].join(', ')}`;
if (!ANSWER_TYPES.has(val)) return `answer_type must be one of: ${[...ANSWER_TYPES].join(', ')}`;
return null;
}
@@ -50,13 +49,12 @@ router.post('/', async (req, res, next) => {
const atErr = validateAnswerType(answer_type);
if (atErr) return res.status(400).json({ error: atErr });
const atArr = Array.isArray(answer_type) ? answer_type : [answer_type];
const result = await query(
`INSERT INTO pairs
(answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic)
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
[atArr, difficulty_level || null, question_id || null,
[answer_type, difficulty_level || null, question_id || null,
positive_statement_id || null, negative_statement_id || null, blocked_topic || null]
);
res.status(201).json(result.rows[0]);
@@ -74,8 +72,10 @@ router.patch('/:id', async (req, res, next) => {
if (req.body.status && !STATUSES.includes(req.body.status))
return res.status(400).json({ error: `status must be one of: ${STATUSES.join(', ')}` });
if (req.body.answer_type && !ANSWER_TYPES.includes(req.body.answer_type))
return res.status(400).json({ error: `answer_type must be one of: ${ANSWER_TYPES.join(', ')}` });
if (req.body.answer_type) {
const atErr = validateAnswerType(req.body.answer_type);
if (atErr) return res.status(400).json({ error: atErr });
}
const tsField = STATUS_TIMESTAMP[req.body.status];
if (tsField && !req.body[tsField]) {