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 dd2f48c8..6fa340a0 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 @@ -65,7 +65,7 @@ public ResponseEntity getStudentReport(@PathVariable String reportTypeC public ResponseEntity> saveStudentReport(@RequestBody GradStudentReports gradStudentReports,@RequestParam(value = "isGraduated", required = false, defaultValue = "false") boolean isGraduated) { logger.debug("Save student Grad Report for Student ID: {}",gradStudentReports.getStudentID()); validation.requiredField(gradStudentReports.getStudentID(), "Student ID"); - return response.UPDATED(commonService.saveGradReports(gradStudentReports,isGraduated)); + return response.UPDATED(commonService.saveGradStudentReports(gradStudentReports,isGraduated)); } @GetMapping(EducGradReportApiConstants.STUDENT_REPORT) @@ -80,6 +80,29 @@ public ResponseEntity getStudentReportByType( return commonService.getStudentReportByType(UUID.fromString(studentID),reportType,documentStatusCode); } + @DeleteMapping(EducGradReportApiConstants.STUDENT_REPORT_BY_STUDENTID) + @PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT_REPORTS) + @Operation(summary = "Read Student Reports by Student ID and Report Type", description = "Read Student Reports by Student ID and Report Type", tags = { "Reports" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity deleteStudentReportByType( + @RequestParam(value = "reportType") String reportType, + @PathVariable UUID studentID) { + logger.debug("getStudentReportByType : "); + return response.GET(commonService.deleteStudentReports(studentID, reportType)); + } + + @PostMapping(EducGradReportApiConstants.STUDENT_REPORTS) + @PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT_REPORTS) + @Operation(summary = "Read Student Reports by Student ID and Report Type", description = "Read Student Reports by Student ID and Report Type", tags = { "Reports" }) + @ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")}) + public ResponseEntity processStudentReports( + @RequestParam(value = "reportTypeCode") String reportTypeCode, + @RequestParam(value = "actionType") String actionType, + @RequestBody List studentIDs) { + logger.debug("processStudentReports : "); + return response.GET(commonService.processStudentReports(studentIDs, reportTypeCode, actionType)); + } + @GetMapping(EducGradReportApiConstants.STUDENT_CERTIFICATES) @PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT_CERTIFICATES) @Operation(summary = "Read Student Certificates by Student ID", description = "Read Student Certificates by Student ID", tags = { "Reports" }) diff --git a/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/GradStudentReportsRepository.java b/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/GradStudentReportsRepository.java index 7a5e1402..034c2bea 100644 --- a/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/GradStudentReportsRepository.java +++ b/api/src/main/java/ca/bc/gov/educ/api/grad/report/repository/GradStudentReportsRepository.java @@ -18,7 +18,9 @@ public interface GradStudentReportsRepository extends JpaRepository existsByReportTypeCode(String reportType); - long deleteByStudentID(UUID studentID); + long deleteByStudentIDInAndGradReportTypeCode(List studentIDs, String gradReportTypeCode); + + long deleteByStudentIDAndGradReportTypeCode(UUID studentID, String gradReportTypeCode); List findByStudentID(UUID studentID); 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 1064aaca..574694cb 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 @@ -10,6 +10,7 @@ import ca.bc.gov.educ.api.grad.report.util.ThreadLocalStateUtil; import jakarta.transaction.Transactional; import org.apache.commons.codec.binary.Base64; +import org.apache.commons.lang3.ObjectUtils; import org.apache.commons.lang3.SerializationUtils; import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.tuple.Pair; @@ -88,7 +89,7 @@ public class CommonService extends BaseService { private static final List SCCP_CERT_TYPES = Arrays.asList("SC", "SCF", "SCI"); @Transactional - public GradStudentReports saveGradReports(GradStudentReports gradStudentReports, boolean isGraduated) { + public GradStudentReports saveGradStudentReports(GradStudentReports gradStudentReports, boolean isGraduated) { GradStudentReportsEntity toBeSaved = gradStudentReportsTransformer.transformToEntity(gradStudentReports); Optional existingEntity = gradStudentReportsRepository.findByStudentIDAndGradReportTypeCodeAndDocumentStatusCodeNot(gradStudentReports.getStudentID(), gradStudentReports.getGradReportTypeCode(), "ARCH"); if (existingEntity.isPresent()) { @@ -319,6 +320,35 @@ public int deleteAllStudentAchievement(UUID studentID) { } + @Transactional + public long processStudentReports(List studentIDs, String reportType, String actionType) { + String reportCode = StringUtils.replace(reportType, "TVRRUN", "ACHV"); + if(StringUtils.containsIgnoreCase(actionType, "DELETE")) { + return deleteStudentReports(studentIDs, reportCode); + } + long reportsCount = 0L; + for(UUID uuid: studentIDs) { + Optional existingEntity = gradStudentReportsRepository.findByStudentIDAndGradReportTypeCode(uuid, reportType); + if(existingEntity.isPresent()) { + GradStudentReportsEntity reportsEntity = existingEntity.get(); + reportsEntity.setReportUpdateDate(new Date()); + gradStudentReportsRepository.save(reportsEntity); + reportsCount ++; + } + } + return reportsCount; + } + + @Transactional + public long deleteStudentReports(List studentIDs, String reportType) { + return gradStudentReportsRepository.deleteByStudentIDInAndGradReportTypeCode(studentIDs, ObjectUtils.defaultIfNull(reportType, "ACHV").toUpperCase()); + } + + @Transactional + public long deleteStudentReports(UUID studentID, String reportType) { + return gradStudentReportsRepository.deleteByStudentIDAndGradReportTypeCode(studentID, ObjectUtils.defaultIfNull(reportType, "ACHV").toUpperCase()); + } + public List getAllStudentReportList(UUID studentID) { List reportList = gradStudentReportsTransformer.transformToDTO(gradStudentReportsRepository.findByStudentID(studentID)); reportList.forEach(rep -> { 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 468d4237..d5707ea2 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 @@ -42,6 +42,8 @@ private EducGradReportApiConstants(){} public static final String UPDATE_SCHOOL_REPORTS = "/updateschoolreport"; public static final String STUDENT_REPORT = "/studentreport"; + public static final String STUDENT_REPORT_BY_STUDENTID = "/studentreport/{studentID}"; + public static final String STUDENT_REPORTS = "/studentreports"; public static final String SCHOOL_REPORT = "/schoolreport"; public static final String STUDENT_CERTIFICATE = "/studentcertificate"; public static final String STUDENT_CERTIFICATES = "/studentcertificates"; 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 c98cc1ea..766248d6 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,22 @@ public void testGetStudentCertificate() { Mockito.verify(commonService).getStudentCertificate(certificateTypeCode); } + @Test + public void testProcessStudentReports() { + final UUID studentGuid = UUID.randomUUID(); + Mockito.when(commonService.processStudentReports(List.of(studentGuid), "TVRRUN", "TVRDELETE")).thenReturn(1L); + commonController.processStudentReports("TVRRUN", "TVRDELETE", List.of(studentGuid)); + Mockito.verify(commonService).processStudentReports(List.of(studentGuid), "TVRRUN", "TVRDELETE"); + } + + @Test + public void testDeleteStudentReportByType() { + final UUID studentGuid = UUID.randomUUID(); + Mockito.when(commonService.deleteStudentReports(studentGuid, "TVRRUN")).thenReturn(1L); + commonController.deleteStudentReportByType("TVRRUN", studentGuid); + Mockito.verify(commonService).deleteStudentReports(studentGuid, "TVRRUN"); + } + @Test public void testGetStudentCertificateByGuid() { final UUID studentGuid = UUID.randomUUID(); @@ -107,9 +123,9 @@ public void testSaveStudentReport() { gradStudentReport.setReport("TEST Report Body"); gradStudentReport.setDocumentStatusCode("IP"); - Mockito.when(commonService.saveGradReports(gradStudentReport,isGraduated)).thenReturn(gradStudentReport); + Mockito.when(commonService.saveGradStudentReports(gradStudentReport,isGraduated)).thenReturn(gradStudentReport); commonController.saveStudentReport(gradStudentReport,isGraduated); - Mockito.verify(commonService).saveGradReports(gradStudentReport,isGraduated); + Mockito.verify(commonService).saveGradStudentReports(gradStudentReport,isGraduated); } @Test 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 93f923fc..75f4d963 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 @@ -222,7 +222,7 @@ public void testSaveGradReports_thenReturnCreateSuccess() { when(this.gradStudentReportsRepository.findByStudentIDAndGradReportTypeCodeAndDocumentStatusCodeNot(studentID, reportTypeCode,documentStatusCode)).thenReturn(optionalEmpty); when(this.gradStudentReportsRepository.save(any(GradStudentReportsEntity.class))).thenReturn(gradStudentReportEntity); - var result = commonService.saveGradReports(gradStudentReport,isGraduated); + var result = commonService.saveGradStudentReports(gradStudentReport,isGraduated); assertThat(result).isNotNull(); assertThat(result.getStudentID()).isEqualTo(studentID); @@ -258,7 +258,7 @@ public void testSaveGradReportsWithExistingOne_thenReturnUpdateSuccess() { when(this.gradStudentReportsRepository.findByStudentIDAndGradReportTypeCodeAndDocumentStatusCodeNot(studentID, reportTypeCode,"ARCH")).thenReturn(optional); when(this.gradStudentReportsRepository.save(any(GradStudentReportsEntity.class))).thenReturn(gradStudentReportEntity); - var result = commonService.saveGradReports(gradStudentReport,isGraduated); + var result = commonService.saveGradStudentReports(gradStudentReport,isGraduated); assertThat(result).isNotNull(); assertThat(result.getStudentID()).isEqualTo(studentID); @@ -294,7 +294,7 @@ public void testSaveGradReportsWithExistingOne_whenReportClobIsChanged_thenRetur when(this.gradStudentReportsRepository.findByStudentIDAndGradReportTypeCodeAndDocumentStatusCodeNot(studentID, reportTypeCode,"ARCH")).thenReturn(optional); when(this.gradStudentReportsRepository.save(any(GradStudentReportsEntity.class))).thenReturn(gradStudentReportEntity); - var result = commonService.saveGradReports(gradStudentReport,isGraduated); + var result = commonService.saveGradStudentReports(gradStudentReport,isGraduated); assertThat(result).isNotNull(); assertThat(result.getStudentID()).isEqualTo(studentID); @@ -330,6 +330,51 @@ public void testGetStudentReportByType() { } + @Test + public void testDeleteStudentReports() { + // ID + final UUID studentID = UUID.randomUUID(); + final String reportTypeCode = "TEST"; + + when(gradStudentReportsRepository.deleteByStudentIDAndGradReportTypeCode(studentID, reportTypeCode)).thenReturn(1L); + var result = commonService.deleteStudentReports(studentID, reportTypeCode); + assertThat(result).isEqualTo(1); + + when(gradStudentReportsRepository.deleteByStudentIDInAndGradReportTypeCode(List.of(studentID), reportTypeCode)).thenReturn(1L); + result = commonService.deleteStudentReports(studentID, reportTypeCode); + assertThat(result).isEqualTo(1); + + } + + @Test + public void testProcessStudentReports() { + // ID + final UUID studentID = UUID.randomUUID(); + final String reportTypeCode = "TVRRUN"; + String actionType = "TVRDELETE"; + + when(gradStudentReportsRepository.deleteByStudentIDInAndGradReportTypeCode(List.of(studentID), reportTypeCode)).thenReturn(0L); + var result = commonService.processStudentReports(List.of(studentID), reportTypeCode, actionType); + assertThat(result).isZero(); + + final UUID reportID = UUID.randomUUID(); + final String pen = "123456789"; + + final GradStudentReportsEntity gradStudentReport = new GradStudentReportsEntity(); + gradStudentReport.setId(reportID); + gradStudentReport.setGradReportTypeCode(reportTypeCode); + gradStudentReport.setPen(pen); + gradStudentReport.setStudentID(studentID); + gradStudentReport.setReport("TEST Report Body"); + actionType = "TVRCREATE"; + + when(gradStudentReportsRepository.findByStudentIDAndGradReportTypeCode(studentID, reportTypeCode)).thenReturn(Optional.of(gradStudentReport)); + when(gradStudentReportsRepository.save(gradStudentReport)).thenReturn(gradStudentReport); + result = commonService.processStudentReports(List.of(studentID), reportTypeCode, actionType); + assertThat(result).isEqualTo(1); + + } + @Test public void testGetStudentCertificateByType() { // UUID