Notifications Engine
Relevant source files
The following files were used as context for generating this wiki page:
- src/main/java/com/example/demo/controllers/NotificationController.java
- src/main/java/com/example/demo/services/NotificationService.java
- src/main/java/com/example/demo/entity/Notification.java
- src/main/java/com/example/demo/services/CommentService.java
- src/main/resources/static/js/api.js
- src/main/resources/templates/projects.html
- src/main/resources/static/js/projects.js
The Notifications Engine is a core service within the Dashboard Task Management (DTM) project designed to alert users about critical task-related events. It facilitates real-time-like communication by tracking events such as task assignments, status changes, new comments, user mentions, and overdue deadlines.
The system encompasses a backend architecture built on Spring Boot for persistence and business logic, and a frontend integration using Thymeleaf and JavaScript to display alerts and manage user interactions with notifications.
Architecture and Data Flow
The engine follows a standard service-oriented architecture. Events triggered in other modules (like CommentService) call the NotificationService to persist alerts in a PostgreSQL database. Users then retrieve these notifications via a REST API.
Logic Flow for Notification Creation
When a significant event occurs (e.g., a new comment is added), the system identifies the relevant participants and creates notification records for each.
Data Model
The Notification entity stores the state of an alert, linking it to a specific user and, optionally, a specific task.
| Field | Type | Description |
|---|---|---|
id | Long | Primary key |
user | User | Recipient of the notification |
type | String | Categories: TASK_ASSIGNED, STATUS_CHANGED, NEW_COMMENT, MENTION, TASK_OVERDUE |
task | Task | Optional link to the relevant task |
message | String | Descriptive text of the notification |
isRead | boolean | Read status (default: false) |
createdAt | LocalDateTime | Timestamp of creation |
Sources: src/main/java/com/example/demo/entity/Notification.java:13-39, README.md:144-150
Backend Components
NotificationService
The service layer manages the business logic for creating, retrieving, and marking notifications as read.
notifyTaskParticipants: Sends notifications to the task creator and all assignees, excluding the user who performed the action.markTaskNotificationsAsRead: Automatically clears notifications for a specific task when a user opens that task's detail page.wasOverdueNotificationSentToday: A check used by schedulers to prevent duplicate overdue alerts within the same day.
Sources: src/main/java/com/example/demo/services/NotificationService.java:33-100
NotificationController (REST API)
Exposes endpoints for the frontend to interact with the notifications.
| Method | Endpoint | Description |
|---|---|---|
GET | /api/notifications | Retrieves all notifications for the current user |
GET | /api/notifications/unread | Retrieves only unread notifications |
GET | /api/notifications/count | Returns a count of unread notifications for the UI badge |
PATCH | /api/notifications/{id}/read | Marks a specific notification as read |
PATCH | /api/notifications/read-all | Marks all user notifications as read |
DELETE | /api/notifications/{id} | Deletes a notification (ADMIN only) |
Sources: src/main/java/com/example/demo/controllers/NotificationController.java:25-65
Frontend Integration
The user interface features a "bell" icon in the navbar that displays an unread count badge. Clicking this icon opens a dropdown list of recent alerts.
Sequence: Loading and Viewing Notifications
The following sequence illustrates how the frontend retrieves and displays notifications.
Implementation Detail: The Notif-Badge
The unread count is injected via Thymeleaf and managed dynamically via JavaScript:
<button class="notif-btn" onclick="toggleNotif()" title="Уведомления">
🔔
<span class="notif-badge" th:text="${unreadCount}"
th:style="${unreadCount == 0} ? 'display:none' : ''">0</span>
</button>
Sources: src/main/resources/templates/projects.html:21-24
Summary
The Notifications Engine provides an automated way to keep users informed of project activity. By integrating deeply with the CommentService and task status updates, it ensures that participants are notified of changes in real-time. The system supports administrative cleanup and provides a streamlined user experience through automatic "read" marking and centralized notification management.