diff --git a/src/db-migrate.js b/src/db-migrate.js index e6a0264..d90424f 100644 --- a/src/db-migrate.js +++ b/src/db-migrate.js @@ -319,6 +319,16 @@ async function migrate() { ) `); + // pairs.answer_type → TEXT[] (multi-select) + 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[] + `).catch(() => {}); + + // statements.answer — boolean nullable (for yes/no correct answer) + await query(`ALTER TABLE statements ADD COLUMN IF NOT EXISTS answer BOOLEAN`); + // objects_created on pictures await query(`ALTER TABLE pictures ADD COLUMN IF NOT EXISTS objects_created BOOLEAN NOT NULL DEFAULT false`); await query(`ALTER TABLE pictures ADD COLUMN IF NOT EXISTS objects_created_at TIMESTAMPTZ`); diff --git a/src/routes/pairs.js b/src/routes/pairs.js index dde475a..b417574 100644 --- a/src/routes/pairs.js +++ b/src/routes/pairs.js @@ -2,7 +2,14 @@ const router = require('express').Router(); const { query } = require('../db'); const STATUSES = ['draft', 'blocked', 'published']; -const ANSWER_TYPES = ['yes_no', 'text', 'word']; +const ANSWER_TYPES = new Set(['yes_no', 'text', '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(', ')}`; + return null; +} const STATUS_TIMESTAMP = { published: 'published_at', @@ -41,14 +48,15 @@ router.post('/', async (req, res, next) => { const { answer_type, difficulty_level, question_id, positive_statement_id, negative_statement_id, blocked_topic } = req.body; - if (!answer_type || !ANSWER_TYPES.includes(answer_type)) - return res.status(400).json({ error: `answer_type required, must be one of: ${ANSWER_TYPES.join(', ')}` }); + 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 *`, - [answer_type, difficulty_level || null, question_id || null, + [atArr, difficulty_level || null, question_id || null, positive_statement_id || null, negative_statement_id || null, blocked_topic || null] ); res.status(201).json(result.rows[0]); diff --git a/src/routes/statements.js b/src/routes/statements.js index 3c6e697..3d5936f 100644 --- a/src/routes/statements.js +++ b/src/routes/statements.js @@ -58,16 +58,17 @@ router.post('/', async (req, res, next) => { const { negative_sentence_de, negative_sentence_en, negative_sentence_sv, positive_sentence_de, positive_sentence_en, positive_sentence_sv, - blocked_topic, + blocked_topic, answer, } = req.body; const result = await query( `INSERT INTO statements (negative_sentence_de, negative_sentence_en, negative_sentence_sv, - positive_sentence_de, positive_sentence_en, positive_sentence_sv, blocked_topic) - VALUES ($1,$2,$3,$4,$5,$6,$7) RETURNING *`, + positive_sentence_de, positive_sentence_en, positive_sentence_sv, + blocked_topic, answer) + VALUES ($1,$2,$3,$4,$5,$6,$7,$8) RETURNING *`, [negative_sentence_de || null, negative_sentence_en || null, negative_sentence_sv || null, positive_sentence_de || null, positive_sentence_en || null, positive_sentence_sv || null, - blocked_topic || null] + blocked_topic || null, answer !== undefined ? answer : null] ); res.status(201).json({ ...result.rows[0], positive_word_ids: [], negative_word_ids: [] }); } catch (err) { next(err); } @@ -78,7 +79,8 @@ router.patch('/:id', async (req, res, next) => { try { const allowed = ['status', 'blocked_topic', 'published_at', 'blocked_at', 'negative_sentence_de', 'negative_sentence_en', 'negative_sentence_sv', - 'positive_sentence_de', 'positive_sentence_en', 'positive_sentence_sv']; + 'positive_sentence_de', 'positive_sentence_en', 'positive_sentence_sv', + 'answer']; const fields = Object.keys(req.body).filter(k => allowed.includes(k)); if (!fields.length) return res.status(400).json({ error: 'No valid fields provided' });