From a44f90dfbc98ff4d57e204b82d176e26cd0f8968 Mon Sep 17 00:00:00 2001 From: arybakov Date: Fri, 6 Sep 2024 17:26:14 -0600 Subject: [PATCH] GRAD2-2817 Fix for duplicated archive school reports --- .../report/repository/SchoolReportsRepository.java | 14 ++++++++------ .../api/grad/report/service/CommonService.java | 6 ++++-- .../api/grad/report/service/CommonServiceTest.java | 12 +++++++----- 3 files changed, 19 insertions(+), 13 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 92e26de3..56b6d31d 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 @@ -30,6 +30,12 @@ public interface SchoolReportsRepository extends JpaRepository getReportGuidsBySchoolOfRecordsAndReportType(List schoolOfRecords, String reportType); + + @Query("select c.id from SchoolReportsLightEntity c where c.reportTypeCode=:reportType") + List getReportGuidsByReportType(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); @@ -39,11 +45,7 @@ public interface SchoolReportsRepository extends JpaRepository 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); + @Query(value="delete from SCHOOL_REPORT where SCHOOL_REPORT_ID not in (:schoolReportGuids) and REPORT_TYPE_CODE = :archivedReportType", nativeQuery=true) + Integer deleteNotMatchingSchoolReports(List schoolReportGuids, String archivedReportType); } 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 48858f67..7fda4ae4 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 @@ -894,17 +894,19 @@ public Integer archiveSchoolReports(long batchId, List schoolOfRecords, Integer originalReportsCount = 0; String archivedReportType = StringUtils.appendIfMissing(reportType, "ARC", "ARC"); if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) { + List reportGuids = schoolReportsRepository.getReportGuidsBySchoolOfRecordsAndReportType(schoolOfRecords, reportType); originalReportsCount += schoolReportsRepository.countBySchoolOfRecordsAndReportType(schoolOfRecords, reportType); updatedReportsCount += schoolReportsRepository.archiveSchoolReports(schoolOfRecords, reportType, archivedReportType, batchId); if(updatedReportsCount > 0 && originalReportsCount.equals(updatedReportsCount)) { - deletedReportsCount += schoolReportsRepository.deleteSchoolReports(schoolOfRecords, archivedReportType); + deletedReportsCount += schoolReportsRepository.deleteNotMatchingSchoolReports(reportGuids, 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.deleteSchoolReports(archivedReportType); + deletedReportsCount += schoolReportsRepository.deleteNotMatchingSchoolReports(reportGuids, archivedReportType); logger.debug("{} School Reports deleted", deletedReportsCount); } } 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 f2d785bc..ef36aba0 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 @@ -1798,26 +1798,27 @@ public Page map(Function converter) { @Test public void testArchiveSchoolReports() { + UUID schoolReportGuid = UUID.randomUUID(); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(1); - Mockito.when(schoolReportsRepository.deleteSchoolReports(List.of("12345678"), "reportTypeARC".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.deleteNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.archiveSchoolReports(List.of("12345678"), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(1); Integer count = commonService.archiveSchoolReports(1L, List.of("12345678"), "reportType".toUpperCase()); assertThat(count).isNotNull().isEqualTo(1); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(0); - Mockito.when(schoolReportsRepository.deleteSchoolReports(List.of("12345678"), "reportTypeARC".toUpperCase())).thenReturn(0); + Mockito.when(schoolReportsRepository.deleteNotMatchingSchoolReports(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.deleteSchoolReports("reportTypeARC".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.deleteNotMatchingSchoolReports(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); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(0); - Mockito.when(schoolReportsRepository.deleteSchoolReports("reportTypeARC".toUpperCase())).thenReturn(0); + Mockito.when(schoolReportsRepository.deleteNotMatchingSchoolReports(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()); assertThat(count).isNotNull().isEqualTo(0); @@ -1833,8 +1834,9 @@ public void testArchiveSchoolReportsEmpty() { @Test public void testDeleteSchoolReports() { + UUID schoolReportGuid = UUID.randomUUID(); Mockito.when(schoolReportsRepository.countBySchoolOfRecordsAndReportType(List.of("12345678"), "reportType".toUpperCase())).thenReturn(1); - Mockito.when(schoolReportsRepository.deleteSchoolReports(List.of("12345678"), "reportTypeARC".toUpperCase())).thenReturn(1); + Mockito.when(schoolReportsRepository.deleteNotMatchingSchoolReports(List.of(schoolReportGuid), "reportTypeARC".toUpperCase())).thenReturn(1); Mockito.when(schoolReportsRepository.archiveSchoolReports(List.of("12345678"), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(1); Integer count = commonService.archiveSchoolReports(1L, List.of("12345678"), "reportType".toUpperCase()); assertThat(count).isNotNull().isEqualTo(1);