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 @@ -25,21 +25,21 @@ public NotificationController(NotificationService notificationService) {
}

@GetMapping
@PreAuthorize("hasRole('STUDENT')")
@PreAuthorize("hasAnyRole('STUDENT','ADMIN')")
public ResponseEntity<ApiResponse<List<NotificationResponse>>> getNotifications() {
List<NotificationResponse> notifications = notificationService.getNotifications(getUserId());
return ResponseEntity.ok(ApiResponse.ok("Notifications retrieved", notifications));
}

@PatchMapping("/{id}/read")
@PreAuthorize("hasRole('STUDENT')")
@PreAuthorize("hasAnyRole('STUDENT','ADMIN')")
public ResponseEntity<ApiResponse<NotificationResponse>> 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<ApiResponse<Void>> markAllAsRead() {
notificationService.markAllAsRead(getUserId());
return ResponseEntity.ok(ApiResponse.ok("All notifications marked as read"));
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/com/ucms_backend/service/AttachmentService.java
Original file line number Diff line number Diff line change
Expand Up @@ -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<String> allowedMimeTypes;
Expand All @@ -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();
Expand Down Expand Up @@ -83,6 +86,18 @@ public AttachmentResponse uploadAttachment(Long ticketId, MultipartFile file) {
TicketAttachment saved = ticketAttachmentRepository.save(attachment);
String signedUrl = supabaseStorageService.generateSignedUrl(storagePath);

List<Profile> 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);
}

Expand Down
12 changes: 12 additions & 0 deletions src/main/java/com/ucms_backend/service/TicketService.java
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,15 @@ private Set<Long> findTicketIdsWithResponses(List<Ticket> tickets) {
return new HashSet<>(foundIds);
}

private void notifyAdmins(Ticket ticket, String message) {
List<Profile> 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)
Expand All @@ -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);
}

Expand Down Expand Up @@ -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);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ class AttachmentServiceTest {
@Mock
private ProfileRepository profileRepository;

@Mock
private NotificationService notificationService;

@Mock
private SupabaseStorageService supabaseStorageService;

Expand All @@ -66,6 +69,7 @@ void setUp() {
ticketRepository,
ticketAttachmentRepository,
profileRepository,
notificationService,
supabaseStorageService,
attachmentProperties
);
Expand Down
Loading