diff --git a/src/main/java/com/libraryman_api/LibrarymanApiApplication.java b/src/main/java/com/libraryman_api/LibrarymanApiApplication.java index df9a8cf..9daf51e 100644 --- a/src/main/java/com/libraryman_api/LibrarymanApiApplication.java +++ b/src/main/java/com/libraryman_api/LibrarymanApiApplication.java @@ -3,9 +3,11 @@ import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.scheduling.annotation.EnableAsync; +import org.springframework.scheduling.annotation.EnableScheduling; @SpringBootApplication @EnableAsync +@EnableScheduling public class LibrarymanApiApplication { public static void main(String[] args) { diff --git a/src/main/java/com/libraryman_api/notification/NotificationRepository.java b/src/main/java/com/libraryman_api/notification/NotificationRepository.java index 7311298..27f9d74 100644 --- a/src/main/java/com/libraryman_api/notification/NotificationRepository.java +++ b/src/main/java/com/libraryman_api/notification/NotificationRepository.java @@ -1,11 +1,19 @@ package com.libraryman_api.notification; +import com.libraryman_api.borrowing.Borrowings; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.query.Param; import org.springframework.stereotype.Repository; +import java.util.Date; import java.util.List; @Repository public interface NotificationRepository extends JpaRepository { List findByMember_memberId(int memberId); + + @Query("SELECT b FROM Borrowings b WHERE b.dueDate BETWEEN :today AND :twoDaysFromNow") + List findBorrowingsDueInDays(@Param("today") Date today, @Param("twoDaysFromNow") Date twoDaysFromNow); } + diff --git a/src/main/java/com/libraryman_api/notification/NotificationService.java b/src/main/java/com/libraryman_api/notification/NotificationService.java index 1d37dde..0ad4205 100644 --- a/src/main/java/com/libraryman_api/notification/NotificationService.java +++ b/src/main/java/com/libraryman_api/notification/NotificationService.java @@ -5,12 +5,16 @@ import com.libraryman_api.exception.ResourceNotFoundException; import com.libraryman_api.member.MemberRepository; import com.libraryman_api.member.Members; +import org.springframework.scheduling.annotation.Scheduled; import org.springframework.stereotype.Service; import java.sql.Timestamp; import java.time.LocalDateTime; import java.time.ZoneId; import java.time.format.DateTimeFormatter; +import java.util.Calendar; +import java.util.Date; +import java.util.List; /** @@ -212,6 +216,28 @@ private void sendNotification(Notifications notification) { ); } + // Scheduled method to send reminders for books due in 2 days + @Scheduled(cron = "0 0 10 * * ?") // Runs every day at 10 AM + + public void sendDueDateReminders(){ + Calendar calendar = Calendar.getInstance(); + + // Get today's date + Date today = calendar.getTime(); + + // Move to two days from now + calendar.add(Calendar.DAY_OF_YEAR, 2); + Date twoDaysFromNow = calendar.getTime(); + + // Fetch borrowings due soon + List borrowingsDueSoon = notificationRepository.findBorrowingsDueInDays(today, twoDaysFromNow); + + // Send reminders for each borrowing + for (Borrowings borrowing : borrowingsDueSoon) { + reminderNotification(borrowing); + } + } + /** * Builds the email content based on the notification type, member name, and notification message. *