Files
zeropost-engine/src/routes/generate.js
T
Ник (Claude) 05fa7644cc feat: user management — detail view, block/unblock, plan change
routes/admin.js: GET /users/:id (profile+channels+balance+transactions)
  PATCH /users/:id (is_blocked, plan_code, name)
  plan change: cancels active sub → creates new → credits reset
generate.js: check is_blocked before generation → 403 ACCOUNT_BLOCKED
DB: users.is_blocked BOOLEAN DEFAULT false
2026-06-13 00:14:11 +03:00

223 lines
9.7 KiB
JavaScript

const express = require('express');
const router = express.Router();
const { query } = require('../config/db');
const channelsSvc = require('../services/channels');
const generationQueue = require('../workers/generation');
const billing = require('../services/billing');
// Маппинг type → billing operation
const BILLING_OP = { post: 'text_post', article: 'article', topics: null };
// POST /api/generate — создать задачу генерации
router.post('/', async (req, res) => {
try {
const { type, topic, channelId, rubric, keywords = [], useCritique = true, customPrompt } = req.body;
const userId = req.headers['x-user-id'] ? parseInt(req.headers['x-user-id']) : null;
if (!type) return res.status(400).json({ error: 'type is required' });
if (!['post', 'article', 'topics'].includes(type)) return res.status(400).json({ error: 'Invalid type' });
if (type !== 'topics' && !topic) return res.status(400).json({ error: 'topic is required' });
if (type === 'post' && !channelId) return res.status(400).json({ error: 'channelId is required for posts' });
// Проверяем не заблокирован ли пользователь
if (userId) {
const { rows: [u] } = await require('../config/db').query('SELECT is_blocked FROM users WHERE id=$1', [userId]);
if (u?.is_blocked) return res.status(403).json({ error: 'Аккаунт заблокирован', code: 'ACCOUNT_BLOCKED' });
}
// Проверка и списание кредитов
const billingOp = BILLING_OP[type];
let billingResult = null;
if (userId && billingOp) {
const ck = await billing.check(userId, billingOp);
if (!ck.allowed) {
return res.status(402).json({ error: ck.reason, code: 'INSUFFICIENT_CREDITS', credits: ck.credits, cost: ck.cost });
}
billingResult = await billing.spend(userId, billingOp, { channel_id: channelId });
}
const { rows } = await query(
`INSERT INTO generation_jobs (user_id, channel_id, type, topic, rubric, status)
VALUES ($1,$2,$3,$4,$5,'pending') RETURNING id`,
[userId, channelId || null, type, topic || null, rubric || null]
);
const jobId = rows[0].id;
await generationQueue.add({ jobId, type, topic, channelId, rubric, keywords, useCritique, customPrompt });
res.json({ jobId, status: 'pending', credits_after: billingResult?.credits_after ?? null, cost: billingResult?.cost ?? 0 });
} catch (err) {
console.error('[Route] POST /generate', err);
res.status(500).json({ error: err.message });
}
});
// GET /api/generate/:id — статус и результат
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 });
}
});
// POST /api/generate/transform — синхронная трансформация поста (короче/длиннее/улучшить)
router.post('/transform', async (req, res) => {
try {
const { channelId, originalPost, action } = req.body;
const userId = parseInt(req.headers['x-user-id']) || null;
if (!channelId || !originalPost || !action) {
return res.status(400).json({ error: 'channelId, originalPost, action required' });
}
const channel = await channelsSvc.getChannel(channelId, userId);
if (!channel) return res.status(404).json({ error: 'Channel not found' });
const ai = require('../services/ai');
const result = await ai.transformPost(channel, { originalPost, action });
res.json(result);
} catch (err) {
console.error('[Route] POST /transform', err);
res.status(500).json({ error: err.message });
}
});
// POST /api/generate/post-image — синхронная генерация картинки к посту
router.post('/post-image', async (req, res) => {
try {
const { channelId, post } = req.body;
const userId = parseInt(req.headers['x-user-id']) || null;
if (!channelId || !post) return res.status(400).json({ error: 'channelId and post required' });
const channel = await channelsSvc.getChannel(channelId, userId);
if (!channel) return res.status(404).json({ error: 'Channel not found' });
// Списываем кредиты за картинку
let imgBilling = null;
if (userId) {
const ck = await billing.check(userId, 'image');
if (!ck.allowed) return res.status(402).json({ error: ck.reason, code: 'INSUFFICIENT_CREDITS', credits: ck.credits, cost: ck.cost });
imgBilling = await billing.spend(userId, 'image', { channel_id: channelId });
}
const { generatePostImage } = require('../services/postImages');
const result = await generatePostImage({ post, channel, style: channel.style || {} });
res.json({ ...result, credits_after: imgBilling?.credits_after ?? null, cost: imgBilling?.cost ?? 0 });
} catch (err) {
console.error('[Route] POST /post-image', err);
res.status(500).json({ error: err.message });
}
});
// GET /api/generate/image-styles — список доступных стилей картинок
router.get('/image-styles', async (_, res) => {
const { IMAGE_STYLES, IMAGE_PALETTES } = require('../services/postImages');
res.json({
styles: Object.entries(IMAGE_STYLES).map(([v, s]) => ({ value: v, label: s.label })),
palettes: Object.keys(IMAGE_PALETTES).map(v => ({
value: v,
label: { auto: 'Авто', dark: 'Тёмная', light: 'Светлая', warm: 'Тёплая', cool: 'Холодная', mono: 'Монохром', vibrant: 'Яркая' }[v] || v
})),
});
});
// POST /api/generate/topics-ideas — синхронные идеи тем для канала
router.post('/hashtags', async (req, res) => {
try {
const { channelId, postText, count = 8 } = req.body;
const userId = req.headers['x-user-id'] ? parseInt(req.headers['x-user-id']) : null;
if (!postText?.trim()) return res.status(400).json({ error: 'postText обязателен' });
const channel = channelId ? await channelsSvc.getChannel(channelId, userId) : null;
const ai = require('../services/ai');
const tags = await ai.generateHashtags(channel, { postText, count });
res.json({ hashtags: tags });
} catch (err) {
res.status(500).json({ error: err.message });
}
});
router.post('/topics-ideas', async (req, res) => {
try {
const { channelId, count = 7 } = req.body;
const userId = parseInt(req.headers['x-user-id']) || null;
if (!channelId) return res.status(400).json({ error: 'channelId required' });
const channel = await channelsSvc.getChannel(channelId, userId);
if (!channel) return res.status(404).json({ error: 'Channel not found' });
const ai = require('../services/ai');
const result = await ai.generateTopics(channel, count);
res.json(result);
} catch (err) {
console.error('[Route] POST /topics-ideas', err);
res.status(500).json({ error: err.message });
}
});
// POST /api/generate/from-url — прочитать URL и написать пост в стиле канала
router.post('/from-url', async (req, res) => {
try {
const { channelId, url } = req.body;
const userId = parseInt(req.headers['x-user-id']) || null;
if (!channelId || !url) return res.status(400).json({ error: 'channelId and url required' });
const channel = await channelsSvc.getChannel(channelId, userId);
if (!channel) return res.status(404).json({ error: 'Channel not found' });
const { generateFromUrl } = require('../services/fromUrl');
const result = await generateFromUrl({ url, channelId, channel });
res.json(result);
} catch (err) {
console.error('[Route] POST /generate/from-url', err);
res.status(500).json({ error: err.message });
}
});
module.exports = router;
// ── Topic Bank API ──────────────────────────────────────────
const topicBank = require('../services/topicBank');
// GET /api/generate/topics-bank/:channelId — список тем из банка
router.get('/topics-bank/:channelId', async (req, res) => {
try {
const topics = await topicBank.listTopics(req.params.channelId, {
limit: 30,
includeUsed: req.query.includeUsed === 'true',
});
const { rows: [{ cnt }] } = await require('../config/db').query(
'SELECT count(*)::int as cnt FROM channel_topics WHERE channel_id=$1 AND is_used=false',
[req.params.channelId]
);
res.json({ topics, total_unused: cnt });
} catch (err) { res.status(500).json({ error: err.message }); }
});
// POST /api/generate/topics-bank/:channelId/refill — пополнить банк вручную
router.post('/topics-bank/:channelId/refill', async (req, res) => {
try {
const saved = await topicBank.refillManual(req.params.channelId);
res.json({ ok: true, added: saved.length, topics: saved });
} catch (err) { res.status(500).json({ error: err.message }); }
});
// POST /api/generate/topics-bank/:channelId/add — добавить темы вручную
router.post('/topics-bank/:channelId/add', async (req, res) => {
try {
const { topics = [] } = req.body;
const saved = await topicBank.addManual(req.params.channelId, topics);
res.json({ ok: true, added: saved.length });
} catch (err) { res.status(500).json({ error: err.message }); }
});
// DELETE /api/generate/topics-bank/:id — удалить тему
router.delete('/topics-bank/item/:id', async (req, res) => {
try {
await require('../config/db').query('DELETE FROM channel_topics WHERE id=$1', [req.params.id]);
res.json({ ok: true });
} catch (err) { res.status(500).json({ error: err.message }); }
});
module.exports = router;