Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import org.example.team6backend.incident.dto.UpdateIncidentStatusRequest;
import org.example.team6backend.incident.entity.Incident;
import org.example.team6backend.incident.service.IncidentService;
import org.example.team6backend.notification.service.NotificationService;
import org.example.team6backend.security.CustomUserDetails;
import org.example.team6backend.user.dto.UserResponse;
import org.example.team6backend.user.entity.AppUser;
Expand All @@ -29,11 +30,14 @@ public class IncidentController {
private final IncidentService incidentService;
private final UserService userService;
private final UserMapper userMapper;
private final NotificationService notificationService;

public IncidentController(IncidentService incidentService, UserService userService, UserMapper userMapper) {
public IncidentController(IncidentService incidentService, UserService userService, UserMapper userMapper,
NotificationService notificationService) {
this.incidentService = incidentService;
this.userService = userService;
this.userMapper = userMapper;
this.notificationService = notificationService;
}

/** Create new incident */
Expand Down Expand Up @@ -77,7 +81,11 @@ public Page<IncidentResponse> getAllIncidents(Pageable pageable) {
@GetMapping("/{id}")
public IncidentResponse getIncidentById(@PathVariable Long id,
@AuthenticationPrincipal CustomUserDetails userDetails) {
return IncidentResponse.fromEntityWithDocuments(incidentService.getById(id, userDetails.getUser()));

AppUser currentUser = userDetails.getUser();
notificationService.markNotificationAsReadForIncident(currentUser.getId(), id);

return IncidentResponse.fromEntityWithDocuments(incidentService.getById(id, currentUser));
}

@PreAuthorize("hasRole('ADMIN')")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,8 @@ public List<NotificationResponse> getUserNotifications(@AuthenticationPrincipal

String userId = userDetails.getUser().getId();

return notificationService.getUserNotifications(userId).stream().map(NotificationResponse::fromEntity).toList();
return notificationService.getUnreadNotifications(userId).stream().map(NotificationResponse::fromEntity)
.toList();
}

@GetMapping("/user/unread-count")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@ public interface NotificationRepository extends JpaRepository<Notification, Long

List<Notification> findByUserIdOrderByCreatedAtDesc(String userId);

List<Notification> findByUserIdAndReadFalse(String userId);
List<Notification> findByUserIdAndReadFalseOrderByCreatedAtDesc(String userId);

long countByUserIdAndReadFalse(String userId);

List<Notification> findByUserIdAndIncidentIdAndReadFalse(String userId, Long incidentId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import org.example.team6backend.notification.repository.NotificationRepository;
import org.example.team6backend.user.entity.AppUser;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

Expand All @@ -32,7 +33,7 @@ public List<Notification> getUserNotifications(String userId) {
}

public List<Notification> getUnreadNotifications(String userId) {
return notificationRepository.findByUserIdAndReadFalse(userId);
return notificationRepository.findByUserIdAndReadFalseOrderByCreatedAtDesc(userId);
}

public long getUnreadCount(String userId) {
Expand All @@ -50,4 +51,16 @@ public void markAsRead(Long notificationId, String userId) {
notification.setRead(true);
notificationRepository.save(notification);
}

@Transactional
public void markNotificationAsReadForIncident(String userId, Long incidentId) {
List<Notification> notifications = notificationRepository.findByUserIdAndIncidentIdAndReadFalse(userId,
incidentId);

for (Notification notification : notifications) {
notification.setRead(true);
}

notificationRepository.saveAll(notifications);
}
}
22 changes: 8 additions & 14 deletions src/main/resources/templates/dashboard.html
Original file line number Diff line number Diff line change
Expand Up @@ -961,20 +961,14 @@ <h3>Recent incidents</h3>
}

async function openNotification(notificationId, incidentId) {
try {
const response = await fetch(`/notifications/${notificationId}/read`, {
method: 'PATCH'
});

if (!response.ok) {
throw new Error(`Failed to mark notifications as read: ${response.status}`);
}

loadNotificationCount();
window.location.href = `/incidents/${incidentId}`;
} catch (error) {
console.error('Error opening notification', error);
}
const badge = document.getElementById("notificationBadge");
if (badge) {
badge.style.display = "none";
}

if (incidentId) {
window.location.href = `/incidents/${incidentId}`;
}
}

function formatDate(dateString) {
Expand Down
Loading