Spaces:
Sleeping
Sleeping
| const Notification = require('../models/Notification'); | |
| const getNotifications = async (req, res) => { | |
| try { | |
| const notifications = await Notification.find({ recipient: req.user._id }) | |
| .sort({ createdAt: -1 }) | |
| .limit(20); | |
| res.json(notifications); | |
| } catch (error) { | |
| res.status(500).json({ message: error.message }); | |
| } | |
| }; | |
| const markAsRead = async (req, res) => { | |
| try { | |
| await Notification.updateMany( | |
| { recipient: req.user._id, isRead: false }, | |
| { $set: { isRead: true } } | |
| ); | |
| res.json({ message: 'Notifications marked as read' }); | |
| } catch (error) { | |
| res.status(500).json({ message: error.message }); | |
| } | |
| }; | |
| const createNotification = async (recipientId, type, content, link = '') => { | |
| try { | |
| const notification = await Notification.create({ | |
| recipient: recipientId, | |
| type, | |
| content, | |
| link | |
| }); | |
| return notification; | |
| } catch (error) { | |
| console.error('Failed to create notification:', error); | |
| } | |
| }; | |
| module.exports = { getNotifications, markAsRead, createNotification }; | |