This commit is contained in:
2026-03-05 23:50:43 +05:00
parent 91f408aca2
commit e43688a618
3 changed files with 168 additions and 0 deletions

View File

@@ -460,7 +460,44 @@ module.exports = function(app, db, upload) {
});
});
});
// GET /api/chat/unread-summary сводка непрочитанных сообщений по задачам
router.get('/api/chat/unread-summary', requireAuth, (req, res) => {
const userId = req.session.user.id;
const query = `
SELECT
m.task_id,
t.title,
COUNT(*) as unread_count
FROM task_chat_messages m
JOIN tasks t ON m.task_id = t.id
LEFT JOIN task_chat_reads r ON m.id = r.message_id AND r.user_id = ?
WHERE m.user_id != ?
AND m.is_deleted = 0
AND r.id IS NULL
GROUP BY m.task_id, t.title
ORDER BY MAX(m.created_at) DESC
LIMIT 50
`;
db.all(query, [userId, userId], (err, rows) => {
if (err) {
console.error('❌ Ошибка получения сводки непрочитанных:', err);
return res.status(500).json({ error: 'Ошибка получения сводки' });
}
const totalUnread = rows.reduce((sum, row) => sum + row.unread_count, 0);
res.json({
tasks: rows.map(r => ({
taskId: r.task_id,
title: r.title,
unreadCount: r.unread_count
})),
totalUnread
});
});
});
// Вспомогательная функция для уведомлений участников
function notifyTaskParticipants(taskId, senderId, message) {
db.all(`