diff --git a/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/SchoolReportsRepository.java b/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/SchoolReportsRepository.java index 99e1880e..ea20c0ea 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/SchoolReportsRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/SchoolReportsRepository.java @@ -36,6 +36,9 @@ public interface SchoolReportsRepository extends JpaRepository getReportGuidsByReportType(String reportType); + @Query("select c.schoolOfRecord from SchoolReportsLightEntity c where c.reportTypeCode=:reportType") + List getReportSchoolOfRecordsByReportType(String reportType); + @Modifying @Query(value="update SCHOOL_REPORT set REPORT_TYPE_CODE = :reportTypeTo, update_date = SYSDATE, update_user = 'Batch ' || :batchId || ' Archive Process' where school_of_record in (:schoolOfRecords) and REPORT_TYPE_CODE = :reportTypeFrom", nativeQuery=true) Integer archiveSchoolReports(List schoolOfRecords, String reportTypeFrom, String reportTypeTo, long batchId); diff --git a/api/src/main/java/ca/bc/gov/educ/api/grad/report/service/CommonService.java b/api/src/main/java/ca/bc/gov/educ/api/grad/report/service/CommonService.java index 68db35e0..abbc76ac 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/grad/report/service/CommonService.java +++ b/api/src/main/java/ca/bc/gov/educ/api/grad/report/service/CommonService.java @@ -889,6 +889,15 @@ public List getStudentIDsByStudentGuidsAndReportType(List studentG @Transactional public Integer archiveSchoolReports(long batchId, List schoolOfRecords, String reportType) { + if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) { + return archiveSchoolReportsBySchoolOfRecordAndReportType(batchId, schoolOfRecords, reportType); + } else { + schoolOfRecords = schoolReportsRepository.getReportSchoolOfRecordsByReportType(reportType); + return archiveSchoolReportsBySchoolOfRecordAndReportType(batchId, schoolOfRecords, reportType); + } + } + + private Integer archiveSchoolReportsBySchoolOfRecordAndReportType(long batchId, List schoolOfRecords, String reportType) { Integer updatedReportsCount = 0; Integer deletedReportsCount = 0; Integer originalReportsCount = 0; @@ -901,14 +910,6 @@ public Integer archiveSchoolReports(long batchId, List schoolOfRecords, deletedReportsCount += schoolReportsRepository.deleteSchoolOfRecordsNotMatchingSchoolReports(reportGuids, schoolOfRecords, archivedReportType); logger.debug("{} School Reports deleted", deletedReportsCount); } - } else { - List reportGuids = schoolReportsRepository.getReportGuidsByReportType(reportType); - originalReportsCount += schoolReportsRepository.countByReportType(reportType); - updatedReportsCount += schoolReportsRepository.archiveSchoolReports(reportType, archivedReportType, batchId); - if(updatedReportsCount > 0 && originalReportsCount.equals(updatedReportsCount)) { - deletedReportsCount += schoolReportsRepository.deleteAllNotMatchingSchoolReports(reportGuids, archivedReportType); - logger.debug("{} School Reports deleted", deletedReportsCount); - } } return updatedReportsCount; } diff --git a/api/src/test/java/ca/bc/gov/educ/api/grad/report/service/CommonServiceTest.java b/api/src/test/java/ca/bc/gov/educ/api/grad/report/service/CommonServiceTest.java index 755ed304..d5cd7006 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/grad/report/service/CommonServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/grad/report/service/CommonServiceTest.java @@ -1800,6 +1800,7 @@ public Page map(Function converter) { public void testArchiveSchoolReports() { UUID schoolReportGuid = UUID.randomUUID(); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.getReportSchoolOfRecordsByReportType("reportType".toUpperCase())).thenReturn(List.of("12345678")); Mockito.when(schoolReportsRepository.deleteSchoolOfRecordsNotMatchingSchoolReports(List.of(schoolReportGuid), List.of("12345678"), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.deleteAllNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.archiveSchoolReports(List.of("12345678"), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(1); @@ -1807,18 +1808,21 @@ public void testArchiveSchoolReports() { assertThat(count).isNotNull().isEqualTo(1); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(0); + Mockito.when(schoolReportsRepository.getReportSchoolOfRecordsByReportType("reportType".toUpperCase())).thenReturn(List.of("12345678")); Mockito.when(schoolReportsRepository.deleteAllNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(0); Mockito.when(schoolReportsRepository.archiveSchoolReports(List.of("12345678"), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(0); count = commonService.archiveSchoolReports(1L, List.of("12345678"), "reportType".toUpperCase()); assertThat(count).isNotNull().isEqualTo(0); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.getReportSchoolOfRecordsByReportType("reportType".toUpperCase())).thenReturn(List.of("12345678")); Mockito.when(schoolReportsRepository.deleteAllNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.archiveSchoolReports("reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(1); count = commonService.archiveSchoolReports(1L, List.of(), "reportType".toUpperCase()); - assertThat(count).isNotNull().isEqualTo(1); + assertThat(count).isNotNull().isEqualTo(0); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(0); + Mockito.when(schoolReportsRepository.getReportSchoolOfRecordsByReportType("reportType".toUpperCase())).thenReturn(List.of("12345678")); Mockito.when(schoolReportsRepository.deleteAllNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(0); Mockito.when(schoolReportsRepository.archiveSchoolReports("reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(0); count = commonService.archiveSchoolReports(1L, List.of(), "reportType".toUpperCase()); @@ -1828,6 +1832,7 @@ public void testArchiveSchoolReports() { @Test public void testArchiveSchoolReportsEmpty() { Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(0); + Mockito.when(schoolReportsRepository.getReportSchoolOfRecordsByReportType("reportType".toUpperCase())).thenReturn(List.of("12345678")); Mockito.when(schoolReportsRepository.archiveSchoolReports(new ArrayList<>(), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(0); Integer count = commonService.archiveSchoolReports(1L, new ArrayList<>(), "reportType".toUpperCase()); assertThat(count).isNotNull().isEqualTo(0); @@ -1837,6 +1842,7 @@ public void testArchiveSchoolReportsEmpty() { public void testDeleteSchoolReports() { UUID schoolReportGuid = UUID.randomUUID(); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.getReportSchoolOfRecordsByReportType("reportType".toUpperCase())).thenReturn(List.of("12345678")); Mockito.when(schoolReportsRepository.deleteSchoolOfRecordsNotMatchingSchoolReports(List.of(schoolReportGuid), List.of("12345678"), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.deleteAllNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.archiveSchoolReports(List.of("12345678"), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(1);