чат2
This commit is contained in:
37
api-chat.js
37
api-chat.js
@@ -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(`
|
||||
|
||||
Reference in New Issue
Block a user