fix(api): ignore unused tasks to stop warnings (#2213)
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
package org.booklore.repository;
|
||||
|
||||
import org.booklore.model.entity.TaskHistoryEntity;
|
||||
import org.booklore.model.enums.TaskType;
|
||||
import org.springframework.data.jpa.repository.JpaRepository;
|
||||
import org.springframework.data.jpa.repository.Query;
|
||||
import org.springframework.stereotype.Repository;
|
||||
@@ -10,8 +11,13 @@ import java.util.List;
|
||||
@Repository
|
||||
public interface TaskHistoryRepository extends JpaRepository<TaskHistoryEntity, String> {
|
||||
|
||||
@Query("SELECT t FROM TaskHistoryEntity t WHERE t.createdAt = " +
|
||||
"(SELECT MAX(t2.createdAt) FROM TaskHistoryEntity t2 WHERE t2.type = t.type) " +
|
||||
"ORDER BY t.createdAt DESC")
|
||||
List<TaskHistoryEntity> findLatestTaskForEachType();
|
||||
@Query("""
|
||||
SELECT t FROM TaskHistoryEntity t
|
||||
WHERE
|
||||
t.createdAt = (SELECT MAX(t2.createdAt) FROM TaskHistoryEntity t2 WHERE t2.type = t.type)
|
||||
AND
|
||||
t.type IN (:taskTypes)
|
||||
ORDER BY t.createdAt DESC
|
||||
""")
|
||||
List<TaskHistoryEntity> findLatestTaskForEachType(List<TaskType> taskTypes);
|
||||
}
|
||||
|
||||
@@ -110,7 +110,8 @@ public class TaskHistoryService {
|
||||
public TasksHistoryResponse getLatestTasksForEachType() {
|
||||
List<TaskHistoryEntity> latestTasks;
|
||||
try {
|
||||
latestTasks = taskHistoryRepository.findLatestTaskForEachType();
|
||||
// Limits to all task types so we ignore older deprecated task types that are no longer in the enum
|
||||
latestTasks = taskHistoryRepository.findLatestTaskForEachType(Arrays.asList(TaskType.values()));
|
||||
} catch (Exception e) {
|
||||
log.warn("Error fetching latest tasks, possibly due to removed enum values: {}", e.getMessage());
|
||||
latestTasks = Collections.emptyList();
|
||||
|
||||
@@ -147,7 +147,7 @@ class TaskHistoryServiceTest {
|
||||
.createdAt(FIXED_TIME.plusMinutes(5))
|
||||
.build();
|
||||
|
||||
when(taskHistoryRepository.findLatestTaskForEachType())
|
||||
when(taskHistoryRepository.findLatestTaskForEachType(any()))
|
||||
.thenReturn(Arrays.asList(importTask, exportTask));
|
||||
|
||||
TasksHistoryResponse response = taskHistoryService.getLatestTasksForEachType();
|
||||
@@ -162,7 +162,7 @@ class TaskHistoryServiceTest {
|
||||
|
||||
@Test
|
||||
void testGetLatestTasksForEachType_exceptionHandled() {
|
||||
when(taskHistoryRepository.findLatestTaskForEachType()).thenThrow(new RuntimeException("DB error"));
|
||||
when(taskHistoryRepository.findLatestTaskForEachType(any())).thenThrow(new RuntimeException("DB error"));
|
||||
TasksHistoryResponse response = taskHistoryService.getLatestTasksForEachType();
|
||||
assertNotNull(response);
|
||||
assertTrue(response.getTaskHistories().stream().allMatch(h -> h.getId() == null));
|
||||
@@ -178,7 +178,7 @@ class TaskHistoryServiceTest {
|
||||
.createdAt(FIXED_TIME)
|
||||
.build();
|
||||
|
||||
when(taskHistoryRepository.findLatestTaskForEachType()).thenReturn(Collections.singletonList(invalidTask));
|
||||
when(taskHistoryRepository.findLatestTaskForEachType(any())).thenReturn(Collections.singletonList(invalidTask));
|
||||
|
||||
TasksHistoryResponse response = taskHistoryService.getLatestTasksForEachType();
|
||||
assertNotNull(response);
|
||||
@@ -259,7 +259,7 @@ class TaskHistoryServiceTest {
|
||||
|
||||
@Test
|
||||
void testGetLatestTasksForEachType_emptyRepository() {
|
||||
when(taskHistoryRepository.findLatestTaskForEachType()).thenReturn(Collections.emptyList());
|
||||
when(taskHistoryRepository.findLatestTaskForEachType(any())).thenReturn(Collections.emptyList());
|
||||
TasksHistoryResponse response = taskHistoryService.getLatestTasksForEachType();
|
||||
assertNotNull(response);
|
||||
assertTrue(response.getTaskHistories().stream().allMatch(h -> h.getId() == null));
|
||||
@@ -275,7 +275,7 @@ class TaskHistoryServiceTest {
|
||||
.progressPercentage(0)
|
||||
.createdAt(FIXED_TIME)
|
||||
.build();
|
||||
when(taskHistoryRepository.findLatestTaskForEachType()).thenReturn(Collections.singletonList(dummyTask));
|
||||
when(taskHistoryRepository.findLatestTaskForEachType(any())).thenReturn(Collections.singletonList(dummyTask));
|
||||
|
||||
hiddenTypes.forEach(type -> {
|
||||
try {
|
||||
|
||||
Reference in New Issue
Block a user