135 Zeilen
3.7 KiB
JavaScript
135 Zeilen
3.7 KiB
JavaScript
/**
|
|
* TASKMATE - Notifications Routes
|
|
* ================================
|
|
* API-Endpunkte für das Benachrichtigungssystem
|
|
*/
|
|
|
|
const express = require('express');
|
|
const router = express.Router();
|
|
const notificationService = require('../services/notificationService');
|
|
const logger = require('../utils/logger');
|
|
|
|
/**
|
|
* GET /api/notifications
|
|
* Alle Benachrichtigungen des Users abrufen
|
|
*/
|
|
router.get('/', (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const limit = parseInt(req.query.limit) || 50;
|
|
|
|
const notifications = notificationService.getForUser(userId, limit);
|
|
const unreadCount = notificationService.getUnreadCount(userId);
|
|
|
|
res.json({
|
|
notifications,
|
|
unreadCount
|
|
});
|
|
} catch (error) {
|
|
logger.error('Fehler beim Abrufen der Benachrichtigungen:', error);
|
|
res.status(500).json({ error: 'Fehler beim Abrufen der Benachrichtigungen' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* GET /api/notifications/count
|
|
* Ungelesene Anzahl ermitteln
|
|
*/
|
|
router.get('/count', (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const count = notificationService.getUnreadCount(userId);
|
|
|
|
res.json({ count });
|
|
} catch (error) {
|
|
logger.error('Fehler beim Ermitteln der Anzahl:', error);
|
|
res.status(500).json({ error: 'Fehler beim Ermitteln der Anzahl' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* PUT /api/notifications/:id/read
|
|
* Als gelesen markieren
|
|
*/
|
|
router.put('/:id/read', (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const notificationId = parseInt(req.params.id);
|
|
|
|
const success = notificationService.markAsRead(notificationId, userId);
|
|
|
|
if (!success) {
|
|
return res.status(404).json({ error: 'Benachrichtigung nicht gefunden' });
|
|
}
|
|
|
|
// Aktualisierte Zählung senden
|
|
const io = req.app.get('io');
|
|
const count = notificationService.getUnreadCount(userId);
|
|
if (io) {
|
|
io.to(`user:${userId}`).emit('notification:count', { count });
|
|
}
|
|
|
|
res.json({ success: true, unreadCount: count });
|
|
} catch (error) {
|
|
logger.error('Fehler beim Markieren als gelesen:', error);
|
|
res.status(500).json({ error: 'Fehler beim Markieren als gelesen' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* PUT /api/notifications/read-all
|
|
* Alle als gelesen markieren
|
|
*/
|
|
router.put('/read-all', (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
|
|
const count = notificationService.markAllAsRead(userId);
|
|
|
|
// Aktualisierte Zählung senden
|
|
const io = req.app.get('io');
|
|
if (io) {
|
|
io.to(`user:${userId}`).emit('notification:count', { count: 0 });
|
|
}
|
|
|
|
res.json({ success: true, markedCount: count, unreadCount: 0 });
|
|
} catch (error) {
|
|
logger.error('Fehler beim Markieren aller als gelesen:', error);
|
|
res.status(500).json({ error: 'Fehler beim Markieren aller als gelesen' });
|
|
}
|
|
});
|
|
|
|
/**
|
|
* DELETE /api/notifications/:id
|
|
* Benachrichtigung löschen (nur nicht-persistente)
|
|
*/
|
|
router.delete('/:id', (req, res) => {
|
|
try {
|
|
const userId = req.user.id;
|
|
const notificationId = parseInt(req.params.id);
|
|
|
|
const success = notificationService.delete(notificationId, userId);
|
|
|
|
if (!success) {
|
|
return res.status(400).json({
|
|
error: 'Benachrichtigung nicht gefunden oder kann nicht gelöscht werden'
|
|
});
|
|
}
|
|
|
|
// Aktualisierte Zählung senden
|
|
const io = req.app.get('io');
|
|
const count = notificationService.getUnreadCount(userId);
|
|
if (io) {
|
|
io.to(`user:${userId}`).emit('notification:count', { count });
|
|
io.to(`user:${userId}`).emit('notification:deleted', { notificationId });
|
|
}
|
|
|
|
res.json({ success: true, unreadCount: count });
|
|
} catch (error) {
|
|
logger.error('Fehler beim Löschen der Benachrichtigung:', error);
|
|
res.status(500).json({ error: 'Fehler beim Löschen der Benachrichtigung' });
|
|
}
|
|
});
|
|
|
|
module.exports = router;
|