Skip to content

Commit

Permalink
Notify members of due books 2 days in advance
Browse files Browse the repository at this point in the history
  • Loading branch information
Rohityadav9575 committed Oct 3, 2024
1 parent e8905ac commit 10f5741
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
Original file line number Diff line number Diff line change
@@ -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<Notifications, Integer> {
List<Notifications> findByMember_memberId(int memberId);

@Query("SELECT b FROM Borrowings b WHERE b.dueDate BETWEEN :today AND :twoDaysFromNow")
List<Borrowings> findBorrowingsDueInDays(@Param("today") Date today, @Param("twoDaysFromNow") Date twoDaysFromNow);
}

Original file line number Diff line number Diff line change
Expand Up @@ -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;


/**
Expand Down Expand Up @@ -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<Borrowings> 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.
*
Expand Down

0 comments on commit 10f5741

Please sign in to comment.