Notifications

This commit is contained in:
2026-02-01 20:54:13 -06:00
parent 6c02bd5448
commit 48e0884bf7
14 changed files with 1628 additions and 7 deletions

View File

@@ -0,0 +1,233 @@
import { Response } from 'express';
import { AuthenticatedRequest } from '../middleware/auth.middleware';
import * as notificationService from '../services/notification.service';
import { NotificationFilter } from '../types';
/**
* GET /api/notifications
* List all notifications for the authenticated user with pagination
* Query params: page, limit, is_read, notification_type, start_date, end_date
*/
export async function getAll(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
if (!req.user?.userId) {
res.status(401).json({
success: false,
error: 'Unauthorized',
});
return;
}
const page = parseInt(req.query.page as string, 10) || 1;
const limit = Math.min(parseInt(req.query.limit as string, 10) || 20, 100);
const filters: NotificationFilter = {};
if (req.query.is_read !== undefined) {
filters.is_read = req.query.is_read === 'true';
}
if (req.query.notification_type) {
filters.notification_type = req.query.notification_type as 'NEGATIVE_FLOW' | 'SYSTEM_ALERT' | 'MAINTENANCE';
}
if (req.query.start_date) {
filters.start_date = new Date(req.query.start_date as string);
}
if (req.query.end_date) {
filters.end_date = new Date(req.query.end_date as string);
}
const result = await notificationService.getAllForUser(
req.user.userId,
filters,
{ page, limit }
);
res.status(200).json({
success: true,
data: result.notifications,
pagination: result.pagination,
});
} catch (error) {
console.error('Error fetching notifications:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch notifications',
});
}
}
/**
* GET /api/notifications/unread-count
* Get count of unread notifications for the authenticated user
*/
export async function getUnreadCount(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
if (!req.user?.userId) {
res.status(401).json({
success: false,
error: 'Unauthorized',
});
return;
}
const count = await notificationService.getUnreadCount(req.user.userId);
res.status(200).json({
success: true,
data: { count },
});
} catch (error) {
console.error('Error fetching unread count:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch unread count',
});
}
}
/**
* GET /api/notifications/:id
* Get a single notification by ID
*/
export async function getById(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
if (!req.user?.userId) {
res.status(401).json({
success: false,
error: 'Unauthorized',
});
return;
}
const { id } = req.params;
const notification = await notificationService.getById(id, req.user.userId);
if (!notification) {
res.status(404).json({
success: false,
error: 'Notification not found',
});
return;
}
res.status(200).json({
success: true,
data: notification,
});
} catch (error) {
console.error('Error fetching notification:', error);
res.status(500).json({
success: false,
error: 'Failed to fetch notification',
});
}
}
/**
* PATCH /api/notifications/:id/read
* Mark a notification as read
*/
export async function markAsRead(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
if (!req.user?.userId) {
res.status(401).json({
success: false,
error: 'Unauthorized',
});
return;
}
const { id } = req.params;
const notification = await notificationService.markAsRead(id, req.user.userId);
if (!notification) {
res.status(404).json({
success: false,
error: 'Notification not found',
});
return;
}
res.status(200).json({
success: true,
data: notification,
});
} catch (error) {
console.error('Error marking notification as read:', error);
res.status(500).json({
success: false,
error: 'Failed to mark notification as read',
});
}
}
/**
* PATCH /api/notifications/read-all
* Mark all notifications as read for the authenticated user
*/
export async function markAllAsRead(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
if (!req.user?.userId) {
res.status(401).json({
success: false,
error: 'Unauthorized',
});
return;
}
const count = await notificationService.markAllAsRead(req.user.userId);
res.status(200).json({
success: true,
data: { count },
message: `Marked ${count} notification(s) as read`,
});
} catch (error) {
console.error('Error marking all notifications as read:', error);
res.status(500).json({
success: false,
error: 'Failed to mark all notifications as read',
});
}
}
/**
* DELETE /api/notifications/:id
* Delete a notification
*/
export async function deleteNotification(req: AuthenticatedRequest, res: Response): Promise<void> {
try {
if (!req.user?.userId) {
res.status(401).json({
success: false,
error: 'Unauthorized',
});
return;
}
const { id } = req.params;
const deleted = await notificationService.deleteNotification(id, req.user.userId);
if (!deleted) {
res.status(404).json({
success: false,
error: 'Notification not found',
});
return;
}
res.status(200).json({
success: true,
message: 'Notification deleted successfully',
});
} catch (error) {
console.error('Error deleting notification:', error);
res.status(500).json({
success: false,
error: 'Failed to delete notification',
});
}
}