const express = require('express'); const router = express.Router(); const { query } = require('../config/db'); const generationQueue = require('../workers/generation'); // POST /api/generate - create generation job router.post('/', async (req, res) => { try { const { type, topic, tone = 'neutral', language = 'ru', channelContext = '', keywords = [], channelId } = req.body; if (!type || !topic) return res.status(400).json({ error: 'type and topic are required' }); if (!['post', 'article', 'topics'].includes(type)) return res.status(400).json({ error: 'Invalid type' }); const { rows } = await query( `INSERT INTO generation_jobs (type, channel_id, topic, status) VALUES ($1,$2,$3,'pending') RETURNING id`, [type, channelId || null, topic] ); const jobId = rows[0].id; await generationQueue.add({ jobId, type, topic, tone, language, channelContext, keywords }); res.json({ jobId, status: 'pending' }); } catch (err) { console.error('[Route] POST /generate', err); res.status(500).json({ error: err.message }); } }); // GET /api/generate/:id - get job status router.get('/:id', async (req, res) => { try { const { rows } = await query(`SELECT * FROM generation_jobs WHERE id=$1`, [req.params.id]); if (!rows.length) return res.status(404).json({ error: 'Job not found' }); res.json(rows[0]); } catch (err) { res.status(500).json({ error: err.message }); } }); module.exports = router;