Skip to content

Commit 97dbc26

Browse files
committed
refactor(notification): add query to exclude stale read notifications and fix receiver selection bug during job application creation
1 parent ba98dcb commit 97dbc26

File tree

6 files changed

+27
-9
lines changed

6 files changed

+27
-9
lines changed

apps/jobboard-backend/src/main/java/org/bounswe/jobboardbackend/exception/ErrorCode.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,9 @@ public enum ErrorCode {
8686
RESUME_FILE_REQUIRED(HttpStatus.BAD_REQUEST),
8787
RESUME_FILE_CONTENT_TYPE_INVALID(HttpStatus.UNSUPPORTED_MEDIA_TYPE),
8888
RESUME_FILE_UPLOAD_FAILED(HttpStatus.INTERNAL_SERVER_ERROR),
89-
RESUME_FILE_NOT_FOUND(HttpStatus.NOT_FOUND);
89+
RESUME_FILE_NOT_FOUND(HttpStatus.NOT_FOUND),
90+
91+
NOTIFICATION_NOT_FOUND(HttpStatus.NOT_FOUND);
9092

9193

9294
public final HttpStatus status;

apps/jobboard-backend/src/main/java/org/bounswe/jobboardbackend/jobapplication/service/JobApplicationService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ public JobApplicationResponse create(CreateJobApplicationRequest dto) {
156156
);
157157

158158
notificationService.notifyUser(
159-
jobSeeker.getUsername(),
159+
jobPost.getEmployer().getUsername(),
160160
"New Job Application Request",
161161
NotificationType.JOB_APPLICATION_APPROVED,
162162
message,

apps/jobboard-backend/src/main/java/org/bounswe/jobboardbackend/notification/dto/NotificationResponse.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public static NotificationResponse fromEntity(Notification n) {
2323
n.getTitle(),
2424
n.getNotificationType(),
2525
n.getMessage(),
26-
n.getTimestamp(),
26+
n.getCreatedAt(),
2727
n.isReadFlag(),
2828
n.getUsername(),
2929
n.getLinkId()

apps/jobboard-backend/src/main/java/org/bounswe/jobboardbackend/notification/model/Notification.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public class Notification {
2727
@Column(length = 500)
2828
private String message;
2929

30-
private Long timestamp;
30+
private Long createdAt;
31+
32+
private Long updatedAt;
3133

3234
private boolean readFlag;
3335
private Long linkId;

apps/jobboard-backend/src/main/java/org/bounswe/jobboardbackend/notification/repository/NotificationRepository.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,20 @@
22

33
import org.bounswe.jobboardbackend.notification.model.Notification;
44
import org.springframework.data.jpa.repository.JpaRepository;
5+
import org.springframework.data.jpa.repository.Query;
56

67
import java.util.List;
78

89
public interface NotificationRepository extends JpaRepository<Notification, Long> {
910

1011
//List<Notification> findByUsernameAndReadFlagFalse(String username);
11-
List<Notification> findByUsername(String username);
12+
//List<Notification> findByUsername(String username);
13+
@Query("""
14+
SELECT n FROM Notification n
15+
WHERE n.username = :username
16+
AND (n.readFlag = false OR n.updatedAt >= :oneDayAgo)
17+
""")
18+
List<Notification> findActiveNotificationsByUsername(String username, long oneDayAgo);
19+
20+
1221
}

apps/jobboard-backend/src/main/java/org/bounswe/jobboardbackend/notification/service/NotificationService.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package org.bounswe.jobboardbackend.notification.service;
22

33
import lombok.RequiredArgsConstructor;
4+
import org.bounswe.jobboardbackend.exception.ErrorCode;
5+
import org.bounswe.jobboardbackend.exception.HandleException;
46
import org.bounswe.jobboardbackend.notification.dto.NotificationMessage;
57
import org.bounswe.jobboardbackend.notification.dto.NotificationResponse;
68
import org.bounswe.jobboardbackend.notification.model.Notification;
@@ -22,7 +24,7 @@ public void notifyAll(String title, NotificationType notificationType, String me
2224
long now = System.currentTimeMillis();
2325

2426
Notification notification =
25-
new Notification(null, null, title, notificationType, message, now, false, linkId);
27+
new Notification(null, null, title, notificationType, message, now, now, false, linkId);
2628
repository.save(notification);
2729

2830
NotificationMessage payload =
@@ -35,7 +37,7 @@ public void notifyUser(String username, String title, NotificationType notificat
3537
long now = System.currentTimeMillis();
3638

3739
Notification notification =
38-
new Notification(null, username, title, notificationType, message, now, false, linkId);
40+
new Notification(null, username, title, notificationType, message, now, now, false, linkId);
3941
repository.save(notification);
4042

4143
NotificationMessage payload =
@@ -49,22 +51,25 @@ public void notifyUser(String username, String title, NotificationType notificat
4951
}
5052

5153
public List<NotificationResponse> getNotificationsForUser(String username) {
52-
return repository.findByUsername(username)
54+
long oneDayAgo = System.currentTimeMillis() - 24 * 60 * 60 * 1000;
55+
return repository.findActiveNotificationsByUsername(username, oneDayAgo)
5356
.stream()
5457
.map(NotificationResponse::fromEntity)
5558
.toList();
5659
}
5760

5861
public void markAsRead(Long id, String username) {
62+
long now = System.currentTimeMillis();
5963
Notification n = repository.findById(id)
60-
.orElseThrow(() -> new IllegalArgumentException("Notification not found: " + id));
64+
.orElseThrow(() -> new HandleException(ErrorCode.NOTIFICATION_NOT_FOUND, "Notification not found"));
6165

6266
// simple ownership check; adjust to your security model
6367
if (n.getUsername() != null && !n.getUsername().equals(username)) {
6468
throw new IllegalStateException("Not allowed to modify this notification");
6569
}
6670

6771
n.setReadFlag(true);
72+
n.setUpdatedAt(now);
6873
repository.save(n);
6974
}
7075
}

0 commit comments

Comments
 (0)