ваше сообщение коммита
This commit is contained in:
@@ -122,10 +122,10 @@ router.get('/', requireAuth, async (req, res, next) => {
|
||||
const tagIdArr = tagIds.split(',').map(Number).filter(Boolean);
|
||||
if (tagIdArr.length > 0) {
|
||||
sql += `
|
||||
JOIN user_tags ut ON ut.user_id = u.id
|
||||
WHERE ut.tag_id = ANY($${idx++})
|
||||
JOIN user_tag_links utl ON utl.user_id = u.id
|
||||
WHERE utl.tag_id = ANY($${idx++})
|
||||
GROUP BY u.id
|
||||
HAVING COUNT(DISTINCT ut.tag_id) = $${idx++}
|
||||
HAVING COUNT(DISTINCT utl.tag_id) = $${idx++}
|
||||
`;
|
||||
params.push(tagIdArr);
|
||||
params.push(tagIdArr.length);
|
||||
@@ -321,7 +321,7 @@ router.get('/:id', async (req, res, next) => {
|
||||
try {
|
||||
const query = db.getQuery();
|
||||
// Получаем пользователя
|
||||
const userResult = await query('SELECT id, first_name, last_name, created_at, preferred_language FROM users WHERE id = $1', [userId]);
|
||||
const userResult = await query('SELECT id, first_name, last_name, created_at, preferred_language, is_blocked FROM users WHERE id = $1', [userId]);
|
||||
if (userResult.rows.length === 0) {
|
||||
return res.status(404).json({ error: 'User not found' });
|
||||
}
|
||||
@@ -339,7 +339,8 @@ router.get('/:id', async (req, res, next) => {
|
||||
telegram: identityMap.telegram || null,
|
||||
wallet: identityMap.wallet || null,
|
||||
created_at: user.created_at,
|
||||
preferred_language: user.preferred_language || []
|
||||
preferred_language: user.preferred_language || [],
|
||||
is_blocked: user.is_blocked || false
|
||||
});
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
@@ -432,4 +433,57 @@ router.post('/import', requireAuth, async (req, res) => {
|
||||
}
|
||||
});
|
||||
|
||||
// --- Работа с тегами пользователя через user_tag_links ---
|
||||
// PATCH /api/users/:id/tags — установить теги пользователю
|
||||
router.patch('/:id/tags', async (req, res) => {
|
||||
const userId = Number(req.params.id);
|
||||
const { tags } = req.body; // массив tagIds (id строк из таблицы тегов)
|
||||
if (!Array.isArray(tags)) {
|
||||
return res.status(400).json({ error: 'tags должен быть массивом' });
|
||||
}
|
||||
try {
|
||||
// Удаляем старые связи
|
||||
await db.getQuery()('DELETE FROM user_tag_links WHERE user_id = $1', [userId]);
|
||||
// Добавляем новые связи
|
||||
for (const tagId of tags) {
|
||||
await db.getQuery()(
|
||||
'INSERT INTO user_tag_links (user_id, tag_id) VALUES ($1, $2) ON CONFLICT DO NOTHING',
|
||||
[userId, tagId]
|
||||
);
|
||||
}
|
||||
res.json({ success: true });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// GET /api/users/:id/tags — получить все теги пользователя
|
||||
router.get('/:id/tags', async (req, res) => {
|
||||
const userId = Number(req.params.id);
|
||||
try {
|
||||
const result = await db.getQuery()(
|
||||
'SELECT tag_id FROM user_tag_links WHERE user_id = $1',
|
||||
[userId]
|
||||
);
|
||||
res.json({ tags: result.rows.map(r => r.tag_id) });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/users/:id/tags/:tagId — удалить тег у пользователя
|
||||
router.delete('/:id/tags/:tagId', async (req, res) => {
|
||||
const userId = Number(req.params.id);
|
||||
const tagId = Number(req.params.tagId);
|
||||
try {
|
||||
await db.getQuery()(
|
||||
'DELETE FROM user_tag_links WHERE user_id = $1 AND tag_id = $2',
|
||||
[userId, tagId]
|
||||
);
|
||||
res.json({ success: true });
|
||||
} catch (e) {
|
||||
res.status(500).json({ error: e.message });
|
||||
}
|
||||
});
|
||||
|
||||
module.exports = router;
|
||||
|
||||
Reference in New Issue
Block a user