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

scheduler to set outdated interview to Expired #32

Merged
merged 1 commit into from
Jan 18, 2025
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
2 changes: 2 additions & 0 deletions src/main/java/com/tnite/jobwinner/JobWinnerApplication.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.scheduling.annotation.EnableScheduling;

@EnableScheduling
@SpringBootApplication
public class JobWinnerApplication {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package com.tnite.jobwinner.jobs;

import com.tnite.jobwinner.service.InterviewService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

@Component
@RequiredArgsConstructor
@Slf4j
public class SetOutdatedInterviewToExpiredJob {

@Autowired
private final InterviewService interviewService;

@Scheduled(cron = "0 0 1 * * ?") // Runs every night at 1:00 AM
public void updateOutdatedInterviewToExpired() {
log.info("Running scheduled job to set outdated interview to expired");
interviewService.updateOutdatedInterviewToExpired()
.subscribe(); // Executes the reactive operation
}
}
Original file line number Diff line number Diff line change
@@ -1,9 +1,16 @@
package com.tnite.jobwinner.repo;

import com.tnite.jobwinner.model.Interview;
import org.springframework.data.r2dbc.repository.Modifying;
import org.springframework.data.r2dbc.repository.Query;
import org.springframework.data.repository.reactive.ReactiveCrudRepository;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

public interface InterviewRepository extends ReactiveCrudRepository<Interview, Integer> {
Flux<Interview> findAllByJobApplicationId(Integer jobApplicationId);

@Modifying
@Query("UPDATE interview SET status = 'Expired' WHERE interview_date < CURRENT_DATE AND status != 'Expired'")
Mono<Integer> updateExpiredInterviews();
}
15 changes: 12 additions & 3 deletions src/main/java/com/tnite/jobwinner/service/InterviewService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

Expand Down Expand Up @@ -74,9 +75,6 @@ public Flux<Interview> getInterviewByJobApplicationId(Integer jobApplicationId)
}

public Flux<Interview> allInterview() {
// return interviewRepository.findAll()
// .doOnComplete(() -> log.info("Retrieved all interviews"))
// .doOnError(e -> log.error("Failed to retrieve interviews", e));
return interviewRepository.findAll()
.flatMap(interview ->
jobApplicationRepository.findById(interview.getJobApplicationId())
Expand Down Expand Up @@ -113,4 +111,15 @@ public Mono<Interview> getInterviewById(Integer id) {
.doOnError(e -> log.error("Failed to retrieve interview with id {}", id, e));
}

@Transactional
public Mono<Integer> updateOutdatedInterviewToExpired() {
return interviewRepository.updateExpiredInterviews()
.doOnNext(count -> {
if (count > 0) {
log.info("{} interview(s) marked as expired.", count);
} else {
log.info("No expired interviews found.");
}
});
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

import java.time.LocalDate;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.*;

Expand Down Expand Up @@ -146,4 +147,31 @@ void testAllInterview() {
verify(interviewRepository, times(1)).findAll();
verify(jobApplicationRepository, times(2)).findById(anyInt());
}

@Test
void testUpdateOutdatedInterviewToExpiredWithThreeRecords() {
when(interviewRepository.updateExpiredInterviews()).thenReturn(Mono.just(3));

Mono<Integer> result = interviewService.updateOutdatedInterviewToExpired();

StepVerifier.create(result)
.assertNext(updatedRows -> assertEquals(3, updatedRows, "Expected 3 rows to be updated"))
.verifyComplete();

verify(interviewRepository, times(1)).updateExpiredInterviews();
}

@Test
void testUpdateOutdatedInterviewToExpiredNoRecordsToUpdate() {
when(interviewRepository.updateExpiredInterviews())
.thenReturn(Mono.just(0));

Mono<Integer> result = interviewService.updateOutdatedInterviewToExpired();

StepVerifier.create(result)
.assertNext(updatedRows -> assertEquals(0, updatedRows, "Expected no rows to be updated"))
.verifyComplete();

verify(interviewRepository, times(1)).updateExpiredInterviews();
}
}
Loading