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 DROP CONSTRAINT IF EXISTS pairs_answer_type_check`).catch(() => {});
|
||||||
await query(`
|
await query(`
|
||||||
ALTER TABLE pairs ALTER COLUMN answer_type TYPE TEXT[]
|
ALTER TABLE pairs ALTER COLUMN answer_type TYPE TEXT
|
||||||
USING ARRAY[answer_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(() => {});
|
`).catch(() => {});
|
||||||
|
|
||||||
// statements.answer — boolean nullable (for yes/no correct answer)
|
// statements.answer — boolean nullable (for yes/no correct answer)
|
||||||
|
|||||||
@@ -2,12 +2,11 @@ const router = require('express').Router();
|
|||||||
const { query } = require('../db');
|
const { query } = require('../db');
|
||||||
|
|
||||||
const STATUSES = ['draft', 'blocked', 'published'];
|
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) {
|
function validateAnswerType(val) {
|
||||||
const arr = Array.isArray(val) ? val : (val ? [val] : []);
|
if (!val || typeof val !== 'string') return `answer_type must be one of: ${[...ANSWER_TYPES].join(', ')}`;
|
||||||
if (!arr.length) return 'answer_type must be a non-empty array of: yes_no, text, word';
|
if (!ANSWER_TYPES.has(val)) return `answer_type must be one of: ${[...ANSWER_TYPES].join(', ')}`;
|
||||||
if (!arr.every(t => ANSWER_TYPES.has(t))) return `answer_type values must be: ${[...ANSWER_TYPES].join(', ')}`;
|
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -50,13 +49,12 @@ router.post('/', async (req, res, next) => {
|
|||||||
|
|
||||||
const atErr = validateAnswerType(answer_type);
|
const atErr = validateAnswerType(answer_type);
|
||||||
if (atErr) return res.status(400).json({ error: atErr });
|
if (atErr) return res.status(400).json({ error: atErr });
|
||||||
const atArr = Array.isArray(answer_type) ? answer_type : [answer_type];
|
|
||||||
|
|
||||||
const result = await query(
|
const result = await query(
|
||||||
`INSERT INTO pairs
|
`INSERT INTO pairs
|
||||||
(answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic)
|
(answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic)
|
||||||
VALUES ($1, $2, $3, $4, $5, $6) RETURNING *`,
|
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]
|
positive_statement_id || null, negative_statement_id || null, blocked_topic || null]
|
||||||
);
|
);
|
||||||
res.status(201).json(result.rows[0]);
|
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))
|
if (req.body.status && !STATUSES.includes(req.body.status))
|
||||||
return res.status(400).json({ error: `status must be one of: ${STATUSES.join(', ')}` });
|
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))
|
if (req.body.answer_type) {
|
||||||
return res.status(400).json({ error: `answer_type must be one of: ${ANSWER_TYPES.join(', ')}` });
|
const atErr = validateAnswerType(req.body.answer_type);
|
||||||
|
if (atErr) return res.status(400).json({ error: atErr });
|
||||||
|
}
|
||||||
|
|
||||||
const tsField = STATUS_TIMESTAMP[req.body.status];
|
const tsField = STATUS_TIMESTAMP[req.body.status];
|
||||||
if (tsField && !req.body[tsField]) {
|
if (tsField && !req.body[tsField]) {
|
||||||
|
|||||||
Reference in New Issue
Block a user