From b0a67df3281008275f89c6b851d3e91521855705 Mon Sep 17 00:00:00 2001 From: admin Date: Mon, 25 May 2026 16:11:15 +0200 Subject: [PATCH] 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 --- src/db-migrate.js | 13 ++++++++++--- src/routes/pairs.js | 16 ++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/src/db-migrate.js b/src/db-migrate.js index d90424f..a6845e5 100644 --- a/src/db-migrate.js +++ b/src/db-migrate.js @@ -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) diff --git a/src/routes/pairs.js b/src/routes/pairs.js index b417574..0944959 100644 --- a/src/routes/pairs.js +++ b/src/routes/pairs.js @@ -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]) {