2025-11-03 20:35:01 +00:00
|
|
|
|
const express = require('express');
|
|
|
|
|
|
const router = express.Router();
|
|
|
|
|
|
const { authenticate } = require('../middleware/auth');
|
2025-11-21 01:14:56 +00:00
|
|
|
|
const { log } = require('../middleware/logger');
|
2025-11-03 20:35:01 +00:00
|
|
|
|
const Notification = require('../models/Notification');
|
|
|
|
|
|
|
|
|
|
|
|
// Получить уведомления пользователя
|
|
|
|
|
|
router.get('/', authenticate, async (req, res) => {
|
|
|
|
|
|
try {
|
|
|
|
|
|
const { page = 1, limit = 50 } = req.query;
|
|
|
|
|
|
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('info', 'Загрузка уведомлений', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
username: req.user.username,
|
|
|
|
|
|
page,
|
|
|
|
|
|
limit
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-03 20:35:01 +00:00
|
|
|
|
const notifications = await Notification.find({ recipient: req.user._id })
|
|
|
|
|
|
.populate('sender', 'username firstName lastName photoUrl')
|
2025-11-21 01:14:56 +00:00
|
|
|
|
.populate('post', 'content imageUrl images')
|
2025-11-03 20:35:01 +00:00
|
|
|
|
.sort({ createdAt: -1 })
|
|
|
|
|
|
.limit(limit * 1)
|
|
|
|
|
|
.skip((page - 1) * limit)
|
|
|
|
|
|
.exec();
|
|
|
|
|
|
|
|
|
|
|
|
const count = await Notification.countDocuments({ recipient: req.user._id });
|
|
|
|
|
|
const unreadCount = await Notification.countDocuments({
|
|
|
|
|
|
recipient: req.user._id,
|
|
|
|
|
|
read: false
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('info', 'Уведомления загружены', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
count: notifications.length,
|
|
|
|
|
|
total: count,
|
|
|
|
|
|
unread: unreadCount
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-03 20:35:01 +00:00
|
|
|
|
res.json({
|
|
|
|
|
|
notifications,
|
|
|
|
|
|
totalPages: Math.ceil(count / limit),
|
|
|
|
|
|
currentPage: page,
|
|
|
|
|
|
unreadCount
|
|
|
|
|
|
});
|
|
|
|
|
|
} catch (error) {
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('error', 'Ошибка получения уведомлений', {
|
|
|
|
|
|
userId: req.user?._id,
|
|
|
|
|
|
error: error.message,
|
|
|
|
|
|
stack: error.stack
|
|
|
|
|
|
});
|
2025-11-03 20:35:01 +00:00
|
|
|
|
res.status(500).json({ error: 'Ошибка сервера' });
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Отметить уведомление как прочитанное
|
|
|
|
|
|
router.put('/:id/read', authenticate, async (req, res) => {
|
|
|
|
|
|
try {
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('info', 'Отметка уведомления как прочитанное', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
notificationId: req.params.id
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-03 20:35:01 +00:00
|
|
|
|
const notification = await Notification.findOne({
|
|
|
|
|
|
_id: req.params.id,
|
|
|
|
|
|
recipient: req.user._id
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
if (!notification) {
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('warn', 'Уведомление не найдено', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
notificationId: req.params.id
|
|
|
|
|
|
});
|
2025-11-03 20:35:01 +00:00
|
|
|
|
return res.status(404).json({ error: 'Уведомление не найдено' });
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
notification.read = true;
|
|
|
|
|
|
await notification.save();
|
|
|
|
|
|
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('info', 'Уведомление отмечено как прочитанное', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
notificationId: req.params.id
|
|
|
|
|
|
});
|
|
|
|
|
|
|
2025-11-03 20:35:01 +00:00
|
|
|
|
res.json({ message: 'Уведомление прочитано' });
|
|
|
|
|
|
} catch (error) {
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('error', 'Ошибка обновления уведомления', {
|
|
|
|
|
|
userId: req.user?._id,
|
|
|
|
|
|
notificationId: req.params.id,
|
|
|
|
|
|
error: error.message,
|
|
|
|
|
|
stack: error.stack
|
|
|
|
|
|
});
|
2025-11-03 20:35:01 +00:00
|
|
|
|
res.status(500).json({ error: 'Ошибка сервера' });
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
// Отметить все уведомления как прочитанные
|
|
|
|
|
|
router.put('/read-all', authenticate, async (req, res) => {
|
|
|
|
|
|
try {
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('info', 'Отметка всех уведомлений как прочитанные', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
username: req.user.username
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
const result = await Notification.updateMany(
|
2025-11-03 20:35:01 +00:00
|
|
|
|
{ recipient: req.user._id, read: false },
|
|
|
|
|
|
{ read: true }
|
|
|
|
|
|
);
|
|
|
|
|
|
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('info', 'Все уведомления отмечены', {
|
|
|
|
|
|
userId: req.user._id,
|
|
|
|
|
|
updated: result.modifiedCount
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
res.json({
|
|
|
|
|
|
message: 'Все уведомления прочитаны',
|
|
|
|
|
|
updated: result.modifiedCount
|
|
|
|
|
|
});
|
2025-11-03 20:35:01 +00:00
|
|
|
|
} catch (error) {
|
2025-11-21 01:14:56 +00:00
|
|
|
|
log('error', 'Ошибка обновления уведомлений', {
|
|
|
|
|
|
userId: req.user?._id,
|
|
|
|
|
|
error: error.message,
|
|
|
|
|
|
stack: error.stack
|
|
|
|
|
|
});
|
2025-11-03 20:35:01 +00:00
|
|
|
|
res.status(500).json({ error: 'Ошибка сервера' });
|
|
|
|
|
|
}
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
|
|
module.exports = router;
|
|
|
|
|
|
|