From ef97a8ad47694786f0f91f3d45af0f6c32aa228c Mon Sep 17 00:00:00 2001 From: XpnsiveSharks Date: Sat, 4 Apr 2026 22:16:45 +0800 Subject: [PATCH] fix: admin notification --- .../controller/NotificationController.java | 6 +++--- .../ucms_backend/service/AttachmentService.java | 15 +++++++++++++++ .../com/ucms_backend/service/TicketService.java | 12 ++++++++++++ .../service/AttachmentServiceTest.java | 4 ++++ 4 files changed, 34 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/ucms_backend/controller/NotificationController.java b/src/main/java/com/ucms_backend/controller/NotificationController.java index 2d630c1..e404b25 100644 --- a/src/main/java/com/ucms_backend/controller/NotificationController.java +++ b/src/main/java/com/ucms_backend/controller/NotificationController.java @@ -25,21 +25,21 @@ public NotificationController(NotificationService notificationService) { } @GetMapping - @PreAuthorize("hasRole('STUDENT')") + @PreAuthorize("hasAnyRole('STUDENT','ADMIN')") public ResponseEntity>> getNotifications() { List notifications = notificationService.getNotifications(getUserId()); return ResponseEntity.ok(ApiResponse.ok("Notifications retrieved", notifications)); } @PatchMapping("/{id}/read") - @PreAuthorize("hasRole('STUDENT')") + @PreAuthorize("hasAnyRole('STUDENT','ADMIN')") public ResponseEntity> markAsRead(@PathVariable Long id) { NotificationResponse notification = notificationService.markAsRead(id, getUserId()); return ResponseEntity.ok(ApiResponse.ok("Notification marked as read", notification)); } @PatchMapping("/read-all") - @PreAuthorize("hasRole('STUDENT')") + @PreAuthorize("hasAnyRole('STUDENT','ADMIN')") public ResponseEntity> markAllAsRead() { notificationService.markAllAsRead(getUserId()); return ResponseEntity.ok(ApiResponse.ok("All notifications marked as read")); diff --git a/src/main/java/com/ucms_backend/service/AttachmentService.java b/src/main/java/com/ucms_backend/service/AttachmentService.java index 9733a53..67a701f 100644 --- a/src/main/java/com/ucms_backend/service/AttachmentService.java +++ b/src/main/java/com/ucms_backend/service/AttachmentService.java @@ -25,6 +25,7 @@ public class AttachmentService { private final TicketRepository ticketRepository; private final TicketAttachmentRepository ticketAttachmentRepository; private final ProfileRepository profileRepository; + private final NotificationService notificationService; private final SupabaseStorageService supabaseStorageService; private final long maxSizeBytes; private final Set allowedMimeTypes; @@ -33,12 +34,14 @@ public AttachmentService( TicketRepository ticketRepository, TicketAttachmentRepository ticketAttachmentRepository, ProfileRepository profileRepository, + NotificationService notificationService, SupabaseStorageService supabaseStorageService, AttachmentProperties attachmentProperties ) { this.ticketRepository = ticketRepository; this.ticketAttachmentRepository = ticketAttachmentRepository; this.profileRepository = profileRepository; + this.notificationService = notificationService; this.supabaseStorageService = supabaseStorageService; this.maxSizeBytes = attachmentProperties.getMaxSizeBytes(); this.allowedMimeTypes = attachmentProperties.getAllowedMimeTypes(); @@ -83,6 +86,18 @@ public AttachmentResponse uploadAttachment(Long ticketId, MultipartFile file) { TicketAttachment saved = ticketAttachmentRepository.save(attachment); String signedUrl = supabaseStorageService.generateSignedUrl(storagePath); + List admins = profileRepository.findByRole("ADMIN"); + String filename = saved.getOriginalFilename() != null ? saved.getOriginalFilename() : "file"; + for (Profile admin : admins) { + if (admin != null && admin.getAuthUserId() != null) { + notificationService.createNotification( + admin.getAuthUserId(), + ticket.getId(), + "Student uploaded attachment on ticket #" + ticket.getTicketNumber() + ": " + filename + ); + } + } + return AttachmentResponse.from(saved, signedUrl); } diff --git a/src/main/java/com/ucms_backend/service/TicketService.java b/src/main/java/com/ucms_backend/service/TicketService.java index d2f3c15..c4b00d4 100644 --- a/src/main/java/com/ucms_backend/service/TicketService.java +++ b/src/main/java/com/ucms_backend/service/TicketService.java @@ -164,6 +164,15 @@ private Set findTicketIdsWithResponses(List tickets) { return new HashSet<>(foundIds); } + private void notifyAdmins(Ticket ticket, String message) { + List admins = profileRepository.findByRole("ADMIN"); + for (Profile admin : admins) { + if (admin != null && admin.getAuthUserId() != null) { + notificationService.createNotification(admin.getAuthUserId(), ticket.getId(), message); + } + } + } + public TicketResponse createTicket(CreateTicketRequest request) { UUID userId = SecurityUtils.getCurrentUserId(); Profile profile = profileRepository.findById(userId) @@ -190,6 +199,7 @@ public TicketResponse createTicket(CreateTicketRequest request) { applyUrgency(ticket); Ticket saved = ticketRepository.save(ticket); publishTicketEvent(saved, "TICKET_CREATED", "STUDENT"); + notifyAdmins(saved, "New ticket submitted: #" + saved.getTicketNumber()); return toResponse(saved); } @@ -322,6 +332,8 @@ public TicketResponse confirmResolved(Long ticketId) { "You have confirmed your ticket as resolved." ); + notifyAdmins(saved, "Student confirmed ticket as resolved: #" + saved.getTicketNumber()); + return toResponse(saved); } diff --git a/src/test/java/com/ucms_backend/service/AttachmentServiceTest.java b/src/test/java/com/ucms_backend/service/AttachmentServiceTest.java index eaba2fc..ea859a2 100644 --- a/src/test/java/com/ucms_backend/service/AttachmentServiceTest.java +++ b/src/test/java/com/ucms_backend/service/AttachmentServiceTest.java @@ -47,6 +47,9 @@ class AttachmentServiceTest { @Mock private ProfileRepository profileRepository; + @Mock + private NotificationService notificationService; + @Mock private SupabaseStorageService supabaseStorageService; @@ -66,6 +69,7 @@ void setUp() { ticketRepository, ticketAttachmentRepository, profileRepository, + notificationService, supabaseStorageService, attachmentProperties );