diff --git a/api/src/main/java/ca/bc/gov/educ/api/grad/report/controller/CommonController.java b/api/src/main/java/ca/bc/gov/educ/api/grad/report/controller/CommonController.java index 6fa340a0..7d6bc5e6 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/grad/report/controller/CommonController.java +++ b/api/src/main/java/ca/bc/gov/educ/api/grad/report/controller/CommonController.java @@ -397,5 +397,19 @@ public ResponseEntity getStudentCredentialByType(@PathVaria return commonService.getStudentCredentialByType(UUID.fromString(studentID),type); } + @PostMapping (EducGradReportApiConstants.REPORT_COUNT) + @PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT_REPORTS) + @Operation(summary = "Get Students Count by mincode and status", description = "Get Students Count by mincode and status", tags = { "Business" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity getReportsCount(@RequestParam String reportType, @RequestBody List schoolOfRecords) { + return response.GET(commonService.countBySchoolOfRecordsAndReportType(schoolOfRecords, reportType)); + } + @PostMapping (EducGradReportApiConstants.REPORT_ARCHIVE) + @PreAuthorize(PermissionsConstants.ARCHIVE_SCHOOL_REPORT) + @Operation(summary = "Get Students Count by mincode and status", description = "Get Students Count by mincode and status", tags = { "Business" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity archiveSchoolReports(@RequestParam long batchId, @RequestParam String reportType, @RequestBody List schoolOfRecords) { + return response.GET(commonService.archiveSchoolReports(batchId, schoolOfRecords, reportType)); + } } 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 886cc975..979ad177 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 @@ -2,6 +2,7 @@ import ca.bc.gov.educ.api.grad.report.model.entity.SchoolReportsEntity; import org.springframework.data.jpa.repository.JpaRepository; +import org.springframework.data.jpa.repository.Modifying; import org.springframework.data.jpa.repository.Query; import org.springframework.stereotype.Repository; @@ -23,4 +24,18 @@ public interface SchoolReportsRepository extends JpaRepository findBySchoolOfRecordAndReportTypeCodeOrderBySchoolOfRecord(String schoolOfRecord, String reportTypeCode); + @Query("select count(*) from SchoolReportsLightEntity c where c.schoolOfRecord IN (:schoolOfRecords) and c.reportTypeCode=:reportType") + Integer countBySchoolOfRecordsAndReportType(List schoolOfRecords, String reportType); + + @Query("select count(*) from SchoolReportsLightEntity c where c.reportTypeCode=:reportType") + Integer countByReportType(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); + + @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); + } 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 574694cb..ad246d1e 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 @@ -845,6 +845,32 @@ private synchronized List getReportGradStudentData(String .block(); } + public Integer countBySchoolOfRecordsAndReportType(List schoolOfRecords, String reportType) { + Integer reportsCount = 0; + if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) { + reportsCount += schoolReportsRepository.countBySchoolOfRecordsAndReportType(schoolOfRecords, reportType); + } + return reportsCount; + } + + @Transactional + public Integer archiveSchoolReports(long batchId, List schoolOfRecords, String reportType) { + Integer updatedReportsCount = 0; + Integer deletedReportsCount = 0; + Integer originalReportsCount = 0; + 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); + } + } + return updatedReportsCount; + } + class UUIDPageTask implements Callable { private final PageRequest pageRequest; diff --git a/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/EducGradReportApiConstants.java b/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/EducGradReportApiConstants.java index d5707ea2..c35b0d1a 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/EducGradReportApiConstants.java +++ b/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/EducGradReportApiConstants.java @@ -23,6 +23,9 @@ private EducGradReportApiConstants(){} public static final String API_VERSION = "v1"; public static final String GRAD_REPORT_API_ROOT_MAPPING = "/api/" + API_VERSION + "/graduationreports"; + public static final String REPORT_COUNT = "/count"; + public static final String REPORT_ARCHIVE = "/archive"; + public static final String GET_ALL_CERTIFICATE_TYPE_MAPPING = "/certificatetype"; public static final String GET_ALL_CERTIFICATE_TYPE_BY_CODE_MAPPING = "/certificatetype/{certTypeCode}"; diff --git a/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/PermissionsConstants.java b/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/PermissionsConstants.java index 57a6ee55..6fac89f3 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/PermissionsConstants.java +++ b/api/src/main/java/ca/bc/gov/educ/api/grad/report/util/PermissionsConstants.java @@ -22,6 +22,8 @@ private PermissionsConstants() { public static final String UPDATE_CERTIFICATE_TYPE = _PREFIX + "SCOPE_UPDATE_GRAD_CERTIFICATE_CODE_DATA" + _SUFFIX; public static final String CREATE_CERTIFICATE_TYPE = _PREFIX + "SCOPE_CREATE_GRAD_CERTIFICATE_CODE_DATA" + _SUFFIX; public static final String READ_GRAD_REPORT = _PREFIX + "SCOPE_READ_GRAD_REPORT_CODE_DATA" + _SUFFIX; + + public static final String ARCHIVE_SCHOOL_REPORT = _PREFIX + "SCOPE_ARCHIVE_SCHOOL_REPORT" + _SUFFIX; public static final String DELETE_REPORT_TYPE = _PREFIX + "SCOPE_DELETE_GRAD_REPORT_CODE_DATA" + _SUFFIX; public static final String UPDATE_REPORT_TYPE = _PREFIX + "SCOPE_UPDATE_GRAD_REPORT_CODE_DATA" + _SUFFIX; public static final String CREATE_REPORT_TYPE = _PREFIX + "SCOPE_CREATE_GRAD_REPORT_CODE_DATA" + _SUFFIX; diff --git a/api/src/test/java/ca/bc/gov/educ/api/grad/report/controller/CommonControllerTest.java b/api/src/test/java/ca/bc/gov/educ/api/grad/report/controller/CommonControllerTest.java index 766248d6..c1bed3db 100644 --- a/api/src/test/java/ca/bc/gov/educ/api/grad/report/controller/CommonControllerTest.java +++ b/api/src/test/java/ca/bc/gov/educ/api/grad/report/controller/CommonControllerTest.java @@ -52,6 +52,24 @@ public void testGetStudentCertificate() { Mockito.verify(commonService).getStudentCertificate(certificateTypeCode); } + @Test + public void testGetReportsCount() { + // ID + String mincode = "123456789"; + Mockito.when(commonService.countBySchoolOfRecordsAndReportType(List.of(mincode), "reportType")).thenReturn(1); + commonController.getReportsCount("reportType", List.of(mincode)); + Mockito.verify(commonService).countBySchoolOfRecordsAndReportType(List.of(mincode), "reportType"); + } + + @Test + public void testArchiveSchoolReports() { + // ID + String mincode = "123456789"; + Mockito.when(commonService.archiveSchoolReports(1L, List.of(mincode), "reportType")).thenReturn(1); + commonController.archiveSchoolReports(1L, "reportType", List.of(mincode)); + Mockito.verify(commonService).archiveSchoolReports(1L, List.of(mincode), "reportType"); + } + @Test public void testProcessStudentReports() { final UUID studentGuid = UUID.randomUUID(); 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 75f4d963..3fe83c70 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 @@ -1680,6 +1680,41 @@ public void testCheckStudentCertificateExistsForSCCP_with_SCCP_Certificate() { assertThat(result).isTrue(); } + @Test + 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); + } + + @Test + public void testArchiveSchoolReports() { + Mockito.when(schoolReportsRepository.deleteSchoolReports(List.of("12345678"), "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"); + assertThat(count).isNotNull().isEqualTo(1); + + Mockito.when(schoolReportsRepository.deleteSchoolReports(List.of("12345678"), "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"); + assertThat(count).isNotNull().isEqualTo(0); + } + + @Test + public void testArchiveSchoolReportsEmpty() { + Mockito.when(schoolReportsRepository.archiveSchoolReports(new ArrayList<>(), "reportType".toUpperCase(), "reportTypeARC".toUpperCase(), 1L)).thenReturn(0); + Integer count = commonService.archiveSchoolReports(1L, new ArrayList<>(), "reportType"); + assertThat(count).isNotNull().isEqualTo(0); + } + + @Test + public void testDeleteSchoolReports() { + Mockito.when(schoolReportsRepository.deleteSchoolReports(List.of("12345678"), "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"); + assertThat(count).isNotNull().isEqualTo(1); + } + @Test @SneakyThrows public void testGetSchoolReportGradStudentData() {