Skip to content

Commit

Permalink
GRAD2-2817
Browse files Browse the repository at this point in the history
Fix for duplicated archive school reports
  • Loading branch information
arybakov-cgi committed Sep 6, 2024
1 parent a777986 commit a44f90d
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@ public interface SchoolReportsRepository extends JpaRepository<SchoolReportsEnti
@Query("select count(*) from SchoolReportsLightEntity c where c.reportTypeCode=:reportType")
Integer countByReportType(String reportType);

@Query("select c.id from SchoolReportsLightEntity c where c.schoolOfRecord IN (:schoolOfRecords) and c.reportTypeCode=:reportType")
List<UUID> getReportGuidsBySchoolOfRecordsAndReportType(List<String> schoolOfRecords, String reportType);

@Query("select c.id from SchoolReportsLightEntity c where c.reportTypeCode=:reportType")
List<UUID> 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<String> schoolOfRecords, String reportTypeFrom, String reportTypeTo, long batchId);
Expand All @@ -39,11 +45,7 @@ public interface SchoolReportsRepository extends JpaRepository<SchoolReportsEnti
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<String> 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<UUID> schoolReportGuids, String archivedReportType);

}
Original file line number Diff line number Diff line change
Expand Up @@ -894,17 +894,19 @@ public Integer archiveSchoolReports(long batchId, List<String> schoolOfRecords,
Integer originalReportsCount = 0;
String archivedReportType = StringUtils.appendIfMissing(reportType, "ARC", "ARC");
if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) {
List<UUID> 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<UUID> 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);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand Down

0 comments on commit a44f90d

Please sign in to comment.