From 0d93da4cfe056ff57e2ff329463b3584783e773e Mon Sep 17 00:00:00 2001 From: Alexander Rybakov <83988488+arybakov-cgi@users.noreply.github.com> Date: Mon, 19 Aug 2024 11:13:55 -0600 Subject: [PATCH] GRAD2-2322 (#274) School Reports Archive Process: All Reports by type --- .../report/repository/SchoolReportsRepository.java | 8 ++++++++ .../api/grad/report/service/CommonService.java | 13 +++++++++++-- .../api/grad/report/service/CommonServiceTest.java | 14 ++++++++++++++ 3 files changed, 33 insertions(+), 2 deletions(-) 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 979ad177..92e26de3 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 @@ -34,8 +34,16 @@ public interface SchoolReportsRepository extends JpaRepository schoolOfRecords, String reportTypeFrom, String reportTypeTo, long batchId); + @Modifying + @Query(value="update SCHOOL_REPORT set REPORT_TYPE_CODE = :reportTypeTo, update_date = SYSDATE, update_user = 'Batch ' || :batchId || ' Archive Process' where REPORT_TYPE_CODE = :reportTypeFrom", nativeQuery=true) + Integer archiveSchoolReports(String reportTypeFrom, String reportTypeTo, long batchId); + @Modifying @Query(value="delete from SCHOOL_REPORT where school_of_record in (:schoolOfRecords) and REPORT_TYPE_CODE = :reportType and UPDATE_DATE <= SYSDATE - 1", nativeQuery=true) Integer deleteSchoolReports(List schoolOfRecords, String reportType); + @Modifying + @Query(value="delete from SCHOOL_REPORT where REPORT_TYPE_CODE = :reportType and UPDATE_DATE <= SYSDATE - 1", nativeQuery=true) + Integer deleteSchoolReports(String reportType); + } 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 ad246d1e..2803a9c0 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 @@ -849,6 +849,8 @@ public Integer countBySchoolOfRecordsAndReportType(List schoolOfRecords, Integer reportsCount = 0; if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) { reportsCount += schoolReportsRepository.countBySchoolOfRecordsAndReportType(schoolOfRecords, reportType); + } else { + reportsCount += schoolReportsRepository.countByReportType(reportType); } return reportsCount; } @@ -858,15 +860,22 @@ public Integer archiveSchoolReports(long batchId, List schoolOfRecords, Integer updatedReportsCount = 0; Integer deletedReportsCount = 0; Integer originalReportsCount = 0; + reportType = StringUtils.upperCase(StringUtils.endsWithIgnoreCase(reportType, "ARC") ? StringUtils.removeEndIgnoreCase(reportType, "ARC") : reportType); + String archivedReportType = StringUtils.upperCase(StringUtils.endsWith(reportType, "ARC") ? reportType : reportType + "ARC"); if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) { - reportType = StringUtils.upperCase(StringUtils.endsWithIgnoreCase(reportType, "ARC") ? StringUtils.removeEndIgnoreCase(reportType, "ARC") : reportType); - String archivedReportType = StringUtils.upperCase(StringUtils.endsWith(reportType, "ARC") ? reportType : reportType + "ARC"); originalReportsCount += schoolReportsRepository.countBySchoolOfRecordsAndReportType(schoolOfRecords, reportType); updatedReportsCount += schoolReportsRepository.archiveSchoolReports(schoolOfRecords, reportType, archivedReportType, batchId); if(originalReportsCount.equals(updatedReportsCount)) { deletedReportsCount += schoolReportsRepository.deleteSchoolReports(schoolOfRecords, archivedReportType); logger.debug("{} School Reports deleted", deletedReportsCount); } + } else { + originalReportsCount += schoolReportsRepository.countByReportType(reportType); + updatedReportsCount += schoolReportsRepository.archiveSchoolReports(reportType, archivedReportType, batchId); + if(originalReportsCount.equals(updatedReportsCount)) { + deletedReportsCount += schoolReportsRepository.deleteSchoolReports(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 3fe83c70..3e6882ed 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 @@ -1685,6 +1685,10 @@ public void testCountBySchoolOfRecordsAndReportType() { Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType")).thenReturn(1); Integer count = commonService.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType"); assertThat(count).isNotNull().isEqualTo(1); + + Mockito.when(schoolReportsRepository.countByReportType("reportType")).thenReturn(1); + count = commonService.countBySchoolOfRecordsAndReportType(List.of(), "reportType"); + assertThat(count).isNotNull().isEqualTo(1); } @Test @@ -1698,6 +1702,16 @@ public void testArchiveSchoolReports() { Mockito.when(schoolReportsRepository.archiveSchoolReports(List.of("12345678"), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(0); count = commonService.archiveSchoolReports(1L, List.of("12345678"), "reportType"); assertThat(count).isNotNull().isEqualTo(0); + + Mockito.when(schoolReportsRepository.deleteSchoolReports("reportTypeARC".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.archiveSchoolReports("reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(1); + count = commonService.archiveSchoolReports(1L, List.of(), "reportType"); + assertThat(count).isNotNull().isEqualTo(1); + + Mockito.when(schoolReportsRepository.deleteSchoolReports("reportTypeARC".toUpperCase())).thenReturn(0); + Mockito.when(schoolReportsRepository.archiveSchoolReports("reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(0); + count = commonService.archiveSchoolReports(1L, List.of(), "reportType"); + assertThat(count).isNotNull().isEqualTo(0); } @Test