Skip to content

Commit bdfc5b4

Browse files
authored
feat: 알림 스케줄러 적용
2 parents 9b722af + a6dc62f commit bdfc5b4

3 files changed

Lines changed: 43 additions & 0 deletions

File tree

src/main/java/com/example/Tokkit_server/notification/repository/MerchantNotificationRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@
55
import com.example.Tokkit_server.notification.enums.NotificationCategory;
66
import io.lettuce.core.dynamic.annotation.Param;
77
import org.springframework.data.jpa.repository.JpaRepository;
8+
import org.springframework.data.jpa.repository.Modifying;
89
import org.springframework.data.jpa.repository.Query;
910

11+
import java.time.LocalDateTime;
1012
import java.util.List;
1113
import java.util.Optional;
1214

@@ -20,4 +22,8 @@ public interface MerchantNotificationRepository extends JpaRepository<MerchantNo
2022
Optional<MerchantNotification> findByIdAndMerchant(Long id, Merchant merchant);
2123

2224
List<MerchantNotification> findByMerchantAndDeletedFalse(Merchant merchant);
25+
26+
@Modifying
27+
@Query("UPDATE MerchantNotification n SET n.deleted = true WHERE n.deleted = false AND n.createdAt < :cutoff")
28+
int softDeleteOldNotifications(@org.springframework.data.repository.query.Param("cutoff") LocalDateTime cutoff);
2329
}

src/main/java/com/example/Tokkit_server/notification/repository/NotificationRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
package com.example.Tokkit_server.notification.repository;
22

3+
import java.time.LocalDateTime;
34
import java.util.List;
45
import java.util.Optional;
56

67
import com.example.Tokkit_server.notification.entity.Notification;
78
import com.example.Tokkit_server.notification.enums.NotificationCategory;
89
import com.example.Tokkit_server.user.entity.User;
910
import org.springframework.data.jpa.repository.JpaRepository;
11+
import org.springframework.data.jpa.repository.Modifying;
1012
import org.springframework.data.jpa.repository.Query;
1113
import org.springframework.data.repository.query.Param;
1214

@@ -23,4 +25,8 @@ public interface NotificationRepository extends JpaRepository<Notification, Long
2325
Optional<Notification> findByIdAndUser(Long id, User user);
2426

2527
List<Notification> findByUserAndDeletedFalse(User user);
28+
29+
@Modifying
30+
@Query("UPDATE Notification n SET n.deleted = true WHERE n.deleted = false AND n.createdAt < :cutoff")
31+
int softDeleteOldNotifications(@Param("cutoff") LocalDateTime cutoff);
2632
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package com.example.Tokkit_server.notification.scheduler;
2+
3+
import com.example.Tokkit_server.notification.repository.MerchantNotificationRepository;
4+
import com.example.Tokkit_server.notification.repository.NotificationRepository;
5+
import lombok.RequiredArgsConstructor;
6+
import lombok.extern.slf4j.Slf4j;
7+
import org.springframework.scheduling.annotation.Scheduled;
8+
import org.springframework.stereotype.Component;
9+
import org.springframework.transaction.annotation.Transactional;
10+
11+
import java.time.LocalDate;
12+
import java.time.LocalDateTime;
13+
14+
@Slf4j
15+
@RequiredArgsConstructor
16+
@Component
17+
public class NotificationCleanupScheduler {
18+
19+
private final NotificationRepository notificationRepository;
20+
private final MerchantNotificationRepository merchantNotificationRepository;
21+
22+
@Scheduled(cron = "0 0 0 * * *")
23+
@Transactional
24+
public void cleanOldNotifications() {
25+
LocalDateTime cutoff = LocalDate.now().minusDays(7).atStartOfDay();
26+
int updatedCount = notificationRepository.softDeleteOldNotifications(cutoff);
27+
int updatedMerchantCount = merchantNotificationRepository.softDeleteOldNotifications(cutoff);
28+
log.info("🧹 [유저 알림 정리] {}개의 알림 soft delete 처리 완료", updatedCount);
29+
log.info("🧹 [가맹점주 알림 정리] {}개의 알림 soft delete 처리 완료", updatedMerchantCount);
30+
}
31+
}

0 commit comments

Comments
 (0)