diff --git a/src/main/java/com/server/crews/recruitment/domain/Recruitment.java b/src/main/java/com/server/crews/recruitment/domain/Recruitment.java index 7fe13075..8e0a0758 100644 --- a/src/main/java/com/server/crews/recruitment/domain/Recruitment.java +++ b/src/main/java/com/server/crews/recruitment/domain/Recruitment.java @@ -37,7 +37,8 @@ indexes = { @Index(columnList = "publisher_id", name = "idx_publisher_id"), @Index(columnList = "code", name = "idx_code"), - @Index(columnList = "title", name = "idx_title") + @Index(columnList = "title", name = "idx_title"), + @Index(columnList = "deadline, progress", name = "idx_deadline_progress") } ) @EntityListeners(AuditingEntityListener.class) diff --git a/src/main/java/com/server/crews/recruitment/repository/RecruitmentRepository.java b/src/main/java/com/server/crews/recruitment/repository/RecruitmentRepository.java index 9c6435e5..2acba6e2 100644 --- a/src/main/java/com/server/crews/recruitment/repository/RecruitmentRepository.java +++ b/src/main/java/com/server/crews/recruitment/repository/RecruitmentRepository.java @@ -1,6 +1,9 @@ package com.server.crews.recruitment.repository; import com.server.crews.recruitment.domain.Recruitment; +import com.server.crews.recruitment.domain.RecruitmentProgress; +import java.time.LocalDateTime; +import java.util.List; import java.util.Optional; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.jpa.repository.Query; @@ -41,4 +44,6 @@ public interface RecruitmentRepository extends JpaRepository where r.title = :title """) Optional findWithSectionsByTitle(@Param("title") String title); + + List findByDeadlineLessThanEqualAndProgressNot(LocalDateTime deadline, RecruitmentProgress progress); } diff --git a/src/main/java/com/server/crews/recruitment/service/RecruitmentService.java b/src/main/java/com/server/crews/recruitment/service/RecruitmentService.java index f01df843..297f83e6 100644 --- a/src/main/java/com/server/crews/recruitment/service/RecruitmentService.java +++ b/src/main/java/com/server/crews/recruitment/service/RecruitmentService.java @@ -146,11 +146,10 @@ public void updateDeadline(Long publisherId, DeadlineUpdateRequest request) { @Scheduled(cron = "${schedules.cron.closing-recruitment}") public void closeRecruitments() { LocalDateTime now = LocalDateTime.now(clock); - List recruitments = recruitmentRepository.findAll(); - List recruitmentsToBeClosed = recruitments.stream() - .filter(recruitment -> recruitment.hasOnOrAfterDeadline(now)) - .toList(); + List recruitmentsToBeClosed = recruitmentRepository.findByDeadlineLessThanEqualAndProgressNot(now, + RecruitmentProgress.ANNOUNCED); recruitmentsToBeClosed.forEach(Recruitment::close); + String closedRecruitmentIds = recruitmentsToBeClosed.stream() .map(Recruitment::getId) .map(String::valueOf) diff --git a/src/test/java/com/server/crews/recruitment/repository/RecruitmentRepositoryTest.java b/src/test/java/com/server/crews/recruitment/repository/RecruitmentRepositoryTest.java index 860b22f6..aa4f6cb2 100644 --- a/src/test/java/com/server/crews/recruitment/repository/RecruitmentRepositoryTest.java +++ b/src/test/java/com/server/crews/recruitment/repository/RecruitmentRepositoryTest.java @@ -11,8 +11,9 @@ import com.server.crews.auth.domain.Administrator; import com.server.crews.environ.repository.RepositoryTest; import com.server.crews.recruitment.domain.Recruitment; -import com.server.crews.recruitment.repository.RecruitmentRepository; +import com.server.crews.recruitment.domain.RecruitmentProgress; import jakarta.validation.ConstraintViolationException; +import java.time.LocalDateTime; import java.util.List; import org.junit.jupiter.api.DisplayName; import org.junit.jupiter.api.Test; @@ -51,4 +52,19 @@ void saveWithValidation() { assertThatThrownBy(() -> recruitmentRepository.save(recruitment)) .isInstanceOf(ConstraintViolationException.class); } + + @Test + @DisplayName("기준보다 이전 마감일과 모집 단계로 모집 공고 목록을 조회한다.") + void findByDeadlineLessThanEqualAndProgressNot() { + // given + Administrator publisher = createDefaultAdmin(); + createDefaultRecruitment(publisher); + + // when + List recruitments = recruitmentRepository.findByDeadlineLessThanEqualAndProgressNot( + LocalDateTime.of(2030, 10, 5, 18, 0), RecruitmentProgress.ANNOUNCED); + + // then + assertThat(recruitments).hasSize(1); + } }