Skip to content

Commit

Permalink
Merge pull request #270 from bcgov/develop/alex-GRAD2-2817
Browse files Browse the repository at this point in the history
GRAD2-2817
  • Loading branch information
arybakov-cgi authored Jul 10, 2024
2 parents 1b18f6b + aa504ea commit 4cf8297
Show file tree
Hide file tree
Showing 6 changed files with 126 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public ResponseEntity<Boolean> getStudentReport(@PathVariable String reportTypeC
public ResponseEntity<ApiResponseModel<GradStudentReports>> 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)
Expand All @@ -80,6 +80,29 @@ public ResponseEntity<InputStreamResource> 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<Long> 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<Long> processStudentReports(
@RequestParam(value = "reportTypeCode") String reportTypeCode,
@RequestParam(value = "actionType") String actionType,
@RequestBody List<UUID> 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" })
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ public interface GradStudentReportsRepository extends JpaRepository<GradStudentR
@Query("select c from GradStudentReportsEntity c where c.gradReportTypeCode=:reportType")
List<GradStudentReportsEntity> existsByReportTypeCode(String reportType);

long deleteByStudentID(UUID studentID);
long deleteByStudentIDInAndGradReportTypeCode(List<UUID> studentIDs, String gradReportTypeCode);

long deleteByStudentIDAndGradReportTypeCode(UUID studentID, String gradReportTypeCode);

List<GradStudentReportsEntity> findByStudentID(UUID studentID);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -88,7 +89,7 @@ public class CommonService extends BaseService {
private static final List<String> 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<GradStudentReportsEntity> existingEntity = gradStudentReportsRepository.findByStudentIDAndGradReportTypeCodeAndDocumentStatusCodeNot(gradStudentReports.getStudentID(), gradStudentReports.getGradReportTypeCode(), "ARCH");
if (existingEntity.isPresent()) {
Expand Down Expand Up @@ -319,6 +320,35 @@ public int deleteAllStudentAchievement(UUID studentID) {

}

@Transactional
public long processStudentReports(List<UUID> 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<GradStudentReportsEntity> 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<UUID> 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<GradStudentReports> getAllStudentReportList(UUID studentID) {
List<GradStudentReports> reportList = gradStudentReportsTransformer.transformToDTO(gradStudentReportsRepository.findByStudentID(studentID));
reportList.forEach(rep -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 4cf8297

Please sign in to comment.