Files
dashboard/backend/src/controllers/notification.controller.ts
2026-03-12 16:42:21 +01:00

101 lines
4.4 KiB
TypeScript

// =============================================================================
// Notification Controller
// =============================================================================
import { Request, Response } from 'express';
import notificationService from '../services/notification.service';
import logger from '../utils/logger';
function isValidUUID(s: string): boolean {
return /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i.test(s);
}
class NotificationController {
/** GET /api/notifications — returns all notifications for the authenticated user. */
async getNotifications(req: Request, res: Response): Promise<void> {
try {
const userId = req.user!.id;
const notifications = await notificationService.getByUser(userId);
res.status(200).json({ success: true, data: notifications });
} catch (error) {
logger.error('NotificationController.getNotifications error', { error });
res.status(500).json({ success: false, message: 'Notifications konnten nicht geladen werden' });
}
}
/** GET /api/notifications/count — returns unread count for the authenticated user. */
async getUnreadCount(req: Request, res: Response): Promise<void> {
try {
const userId = req.user!.id;
const count = await notificationService.getUnreadCount(userId);
res.status(200).json({ success: true, data: { count } });
} catch (error) {
logger.error('NotificationController.getUnreadCount error', { error });
res.status(500).json({ success: false, message: 'Anzahl konnte nicht geladen werden' });
}
}
/** PATCH /api/notifications/:id/read — marks a single notification as read. */
async markAsRead(req: Request, res: Response): Promise<void> {
try {
const { id } = req.params as Record<string, string>;
if (!isValidUUID(id)) {
res.status(400).json({ success: false, message: 'Ungültige Notification-ID' });
return;
}
const userId = req.user!.id;
const updated = await notificationService.markAsRead(id, userId);
if (!updated) {
res.status(404).json({ success: false, message: 'Notification nicht gefunden' });
return;
}
res.status(200).json({ success: true, message: 'Als gelesen markiert' });
} catch (error) {
logger.error('NotificationController.markAsRead error', { error });
res.status(500).json({ success: false, message: 'Notification konnte nicht aktualisiert werden' });
}
}
/** POST /api/notifications/mark-all-read — marks all notifications as read. */
async markAllRead(req: Request, res: Response): Promise<void> {
try {
const userId = req.user!.id;
await notificationService.markAllRead(userId);
res.status(200).json({ success: true, message: 'Alle als gelesen markiert' });
} catch (error) {
logger.error('NotificationController.markAllRead error', { error });
res.status(500).json({ success: false, message: 'Notifications konnten nicht aktualisiert werden' });
}
}
/** POST /api/notifications/dismiss-by-type — marks all unread notifications of a given type as read. */
async dismissByType(req: Request, res: Response): Promise<void> {
try {
const { quellTyp } = req.body;
if (!quellTyp || typeof quellTyp !== 'string') {
res.status(400).json({ success: false, message: 'quellTyp ist erforderlich' });
return;
}
const userId = req.user!.id;
await notificationService.dismissByType(userId, quellTyp);
res.status(200).json({ success: true, message: 'Notifications als gelesen markiert' });
} catch (error) {
logger.error('NotificationController.dismissByType error', { error });
res.status(500).json({ success: false, message: 'Notifications konnten nicht aktualisiert werden' });
}
}
/** DELETE /api/notifications/read — deletes all read notifications for the authenticated user. */
async deleteAllRead(req: Request, res: Response): Promise<void> {
try {
const userId = req.user!.id;
await notificationService.deleteAllRead(userId);
res.status(200).json({ success: true, message: 'Gelesene Notifications gelöscht' });
} catch (error) {
logger.error('NotificationController.deleteAllRead error', { error });
res.status(500).json({ success: false, message: 'Gelesene Notifications konnten nicht gelöscht werden' });
}
}
}
export default new NotificationController();