Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Notify members of due books 2 days in advance #57

Merged
merged 1 commit into from
Oct 3, 2024
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 @@ -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