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:
@@ -319,11 +319,18 @@ async function migrate() {
|
||||
)
|
||||
`);
|
||||
|
||||
// pairs.answer_type → TEXT[] (multi-select)
|
||||
// pairs.answer_type → single TEXT (was TEXT[], now back to single value + new 'question' type)
|
||||
await query(`ALTER TABLE pairs DROP CONSTRAINT IF EXISTS pairs_answer_type_check`).catch(() => {});
|
||||
await query(`
|
||||
ALTER TABLE pairs ALTER COLUMN answer_type TYPE TEXT[]
|
||||
USING ARRAY[answer_type]::TEXT[]
|
||||
ALTER TABLE pairs ALTER COLUMN answer_type TYPE TEXT
|
||||
USING (CASE
|
||||
WHEN answer_type IS NULL OR array_length(answer_type::TEXT[], 1) IS NULL THEN 'text'
|
||||
ELSE (answer_type::TEXT[])[1]
|
||||
END)
|
||||
`).catch(() => {});
|
||||
await query(`
|
||||
ALTER TABLE pairs ADD CONSTRAINT pairs_answer_type_check
|
||||
CHECK (answer_type IN ('yes_no', 'text', 'question', 'word'))
|
||||
`).catch(() => {});
|
||||
|
||||
// statements.answer — boolean nullable (for yes/no correct answer)
|
||||
|
||||
@@ -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]) {
|
||||
|
||||
Reference in New Issue
Block a user