Skip to content

Commit

Permalink
Merge pull request #308 from bcgov/feature/GRAD2-3037
Browse files Browse the repository at this point in the history
GRAD2-3037 - updates for report count endpoint and StudentSearchReque…
  • Loading branch information
infstar authored Jan 16, 2025
2 parents 2df1998 + 6f8f596 commit 637753e
Show file tree
Hide file tree
Showing 8 changed files with 143 additions and 122 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.UUID;

@RestController("commonControllerV2")
@RequestMapping(EducGradReportApiConstants.GRAD_REPORT_API_V2_ROOT_MAPPING)
Expand All @@ -28,7 +29,6 @@ public class CommonController {

private static final Logger logger = LoggerFactory.getLogger(CommonController.class);

private static final String BEARER = "Bearer ";
final CommonService commonService;

final GradValidation validation;
Expand All @@ -47,25 +47,35 @@ public CommonController(CommonService commonService, GradValidation validation,
@Operation(summary = "Read All Student Transcripts/Certificates for User Req Distribution", description = "Read All Student Credentials for Distribution", tags = { "Certificates" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<List<StudentCredentialDistribution>> getStudentCredentialsForUserRequestDisRun(
@PathVariable String credentialType, @RequestBody StudentSearchRequest studentSearchRequest,
@RequestHeader(name="Authorization") String accessToken) {
@PathVariable String credentialType, @RequestBody StudentSearchRequest studentSearchRequest) {
logger.debug("getStudentCredentialsForUserRequestDisRun : ");
boolean isPenNumberSearch = studentSearchRequest.getPens() != null && !studentSearchRequest.getPens().isEmpty()
&& !studentSearchRequest.getPens().stream().filter(StringUtils::isNotBlank).toList().isEmpty();
boolean onlyWithNullDistributionDate = !isPenNumberSearch && studentSearchRequest.getGradDateFrom() == null && studentSearchRequest.getGradDateTo() == null && !StringUtils.equalsAnyIgnoreCase(credentialType, "OT", "RT");
return response.GET(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,studentSearchRequest,onlyWithNullDistributionDate,accessToken.replace(BEARER, "")));
return response.GET(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,studentSearchRequest,onlyWithNullDistributionDate));
}

@PostMapping(EducGradReportApiConstants.USER_REQUEST_DIS_RUN_WITH_NULL_DISTRIBUTION_DATE)
@PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT_CERTIFICATES)
@Operation(summary = "Read All Student Transcripts/Certificates with Null Distribution Date for User Req Distribution", description = "Read All Student Credentials with Null Distribution Date for Distribution", tags = { "Certificates" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<List<StudentCredentialDistribution>> getStudentCredentialsForUserRequestDisRunWithNullDistributionDate(
@PathVariable String credentialType, @RequestBody StudentSearchRequest studentSearchRequest,
@RequestHeader(name="Authorization") String accessToken) {
@PathVariable String credentialType, @RequestBody StudentSearchRequest studentSearchRequest) {
logger.debug("getStudentCredentialsForUserRequestDisRunWithNullDistributionDate : ");
boolean isPenNumberSearch = studentSearchRequest.getPens()!= null && !studentSearchRequest.getPens().isEmpty()
&& !studentSearchRequest.getPens().stream().filter(StringUtils::isNotBlank).toList().isEmpty();
return response.GET(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,studentSearchRequest,!isPenNumberSearch,accessToken.replace(BEARER, "")));
return response.GET(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,studentSearchRequest,!isPenNumberSearch));
}

@PostMapping (EducGradReportApiConstants.REPORT_COUNT)
@PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT_REPORTS)
@Operation(summary = "Get Reports Count by id and status", description = "Get Students Count by id and status", tags = { "Business" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<Integer> getReportsCount(@RequestParam String reportType, @RequestBody List<UUID> reportContainerIds) {
if(StringUtils.containsAnyIgnoreCase(reportType, "ACHV")) {
return response.GET(commonService.countByStudentGuidsAndReportType(reportContainerIds, reportType));
} else {
return response.GET(commonService.countBySchoolOfRecordsAndReportType(reportContainerIds, reportType));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

@Data
public class StudentSearchRequest implements Serializable {
private List<UUID> schoolOfRecordIds = new ArrayList<>();
private List<String> districts = new ArrayList<>();
private List<UUID> schoolIds = new ArrayList<>();
private List<UUID> districtIds = new ArrayList<>();
private List<String> schoolCategoryCodes = new ArrayList<>();
private List<String> pens = new ArrayList<>();
private List<String> programs = new ArrayList<>();
Expand All @@ -37,8 +37,8 @@ public class StudentSearchRequest implements Serializable {

@JsonIgnore
public boolean isEmpty() {
return (schoolOfRecordIds == null || schoolOfRecordIds.isEmpty()) &&
(districts == null || districts.isEmpty()) &&
return (schoolIds == null || schoolIds.isEmpty()) &&
(districtIds == null || districtIds.isEmpty()) &&
(schoolCategoryCodes == null || schoolCategoryCodes.isEmpty()) &&
(pens == null || pens.isEmpty()) &&
(studentIDs == null || studentIDs.isEmpty());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,12 @@

@Repository("schoolReportsRepositoryV2")
public interface SchoolReportRepository extends JpaRepository<SchoolReportEntity, UUID>, JpaSpecificationExecutor<SchoolReportEntity> {

List<SchoolReportEntity> deleteAllByReportTypeCode(String reportTypeCode);

Optional<SchoolReportEntity> findBySchoolOfRecordIdAndReportTypeCode(UUID schoolOfRecordId, String reportTypeCode);

Integer countBySchoolOfRecordIdInAndReportTypeCode(List<UUID> schoolOfRecordIds, String reportType);

Integer countByReportTypeCode(String reportType);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,11 @@
import ca.bc.gov.educ.api.grad.report.model.dto.StudentCredentialDistribution;
import ca.bc.gov.educ.api.grad.report.model.dto.v2.StudentSearchRequest;
import ca.bc.gov.educ.api.grad.report.repository.*;
import ca.bc.gov.educ.api.grad.report.repository.v2.SchoolReportRepository;
import ca.bc.gov.educ.api.grad.report.service.BaseService;
import ca.bc.gov.educ.api.grad.report.util.EducGradReportApiConstants;
import ca.bc.gov.educ.api.grad.report.util.ThreadLocalStateUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.reactive.function.BodyInserters;

import java.util.*;

Expand All @@ -19,22 +17,28 @@ public class CommonService extends BaseService {

final GradStudentCertificatesRepository gradStudentCertificatesRepository;
final GradStudentTranscriptsRepository gradStudentTranscriptsRepository;
final GradStudentReportsRepository gradStudentReportsRepository;
final SchoolReportRepository schoolReportRepository;
final RESTService restService;

@Autowired
public CommonService(GradStudentCertificatesRepository gradStudentCertificatesRepository, GradStudentTranscriptsRepository gradStudentTranscriptsRepository) {
public CommonService(GradStudentCertificatesRepository gradStudentCertificatesRepository, GradStudentTranscriptsRepository gradStudentTranscriptsRepository, GradStudentReportsRepository gradStudentReportsRepository, SchoolReportRepository schoolReportRepository, RESTService restService) {
this.gradStudentCertificatesRepository = gradStudentCertificatesRepository;
this.gradStudentTranscriptsRepository = gradStudentTranscriptsRepository;
this.gradStudentReportsRepository = gradStudentReportsRepository;
this.schoolReportRepository = schoolReportRepository;
this.restService = restService;
}


public List<StudentCredentialDistribution> getStudentCredentialsForUserRequestDisRun(String credentialType, StudentSearchRequest studentSearchRequest, boolean onlyWithNullDistributionDate, String accessToken) {
public List<StudentCredentialDistribution> getStudentCredentialsForUserRequestDisRun(String credentialType, StudentSearchRequest studentSearchRequest, boolean onlyWithNullDistributionDate) {
List<StudentCredentialDistribution> scdList = new ArrayList<>();
if(StringUtils.isBlank(studentSearchRequest.getActivityCode())) {
studentSearchRequest.setActivityCode("USERDIST" + StringUtils.upperCase(credentialType));
}
List<UUID> studentIDs = studentSearchRequest.getStudentIDs();
if(studentIDs == null || studentIDs.isEmpty()) {
studentIDs = getStudentsForSpecialGradRun(studentSearchRequest, accessToken);
studentIDs = getStudentsForSpecialGradRun(studentSearchRequest);
}
if (!studentIDs.isEmpty()) {
int partitionSize = 1000;
Expand Down Expand Up @@ -87,19 +91,31 @@ private void processTranscript(List<List<UUID>> partitions, StudentSearchRequest
}
}

public List<UUID> getStudentsForSpecialGradRun(StudentSearchRequest req, String accessToken) {
GraduationStudentRecordSearchResult res = this.webClient.post()
.uri(constants.getGradStudentApiStudentForSpcGradListUrl())
.headers(h -> {
h.setBearerAuth(accessToken);
h.set(EducGradReportApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
})
.body(BodyInserters.fromValue(req))
.retrieve()
.bodyToMono(GraduationStudentRecordSearchResult.class)
.block();
public List<UUID> getStudentsForSpecialGradRun(StudentSearchRequest req) {
GraduationStudentRecordSearchResult res = this.restService.post(constants.getGradStudentApiStudentForSpcGradListUrl(), req, GraduationStudentRecordSearchResult.class);
if (res != null && !res.getStudentIDs().isEmpty())
return res.getStudentIDs();
return new ArrayList<>();
}

public Integer countByStudentGuidsAndReportType(List<UUID> studentGuidsString, String reportType) {
Integer reportsCount = 0;
if(studentGuidsString != null && !studentGuidsString.isEmpty()) {
List<UUID> studentGuids = new ArrayList<>(studentGuidsString);
reportsCount += gradStudentReportsRepository.countByStudentGuidsAndReportType(studentGuids, reportType);
} else {
reportsCount += gradStudentReportsRepository.countByReportType(reportType);
}
return reportsCount;
}

public Integer countBySchoolOfRecordsAndReportType(List<UUID> schoolOfRecords, String reportType) {
Integer reportsCount = 0;
if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) {
reportsCount += schoolReportRepository.countBySchoolOfRecordIdInAndReportTypeCode(schoolOfRecords, reportType);
} else {
reportsCount += schoolReportRepository.countByReportTypeCode(reportType);
}
return reportsCount;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
import ca.bc.gov.educ.api.grad.report.repository.v2.DistrictReportLightRepository;
import ca.bc.gov.educ.api.grad.report.repository.v2.DistrictReportRepository;
import ca.bc.gov.educ.api.grad.report.service.BaseService;
import ca.bc.gov.educ.api.grad.report.service.CommonService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -31,15 +30,13 @@ public class DistrictReportService extends BaseService {
DistrictReportRepository districtReportsRepository;
DistrictReportLightRepository districtReportLightRepository;
DistrictReportTransformer districtReportTransformer;
CommonService commonService;
InstituteService instituteService;

private static final Logger logger = LoggerFactory.getLogger(DistrictReportService.class);

@Autowired
public DistrictReportService(DistrictReportRepository districtReportsRepository, CommonService commonService, DistrictReportLightRepository districtReportLightRepository, InstituteService instituteService, DistrictReportTransformer districtReportTransformer) {
public DistrictReportService(DistrictReportRepository districtReportsRepository, DistrictReportLightRepository districtReportLightRepository, InstituteService instituteService, DistrictReportTransformer districtReportTransformer) {
this.districtReportsRepository = districtReportsRepository;
this.commonService = commonService;
this.districtReportLightRepository = districtReportLightRepository;
this.instituteService = instituteService;
this.districtReportTransformer = districtReportTransformer;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
import ca.bc.gov.educ.api.grad.report.repository.v2.SchoolReportLightRepository;
import ca.bc.gov.educ.api.grad.report.repository.v2.SchoolReportRepository;
import ca.bc.gov.educ.api.grad.report.service.BaseService;
import ca.bc.gov.educ.api.grad.report.service.CommonService;
import org.apache.commons.codec.binary.Base64;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
Expand All @@ -32,16 +31,14 @@ public class SchoolReportService extends BaseService {
SchoolReportRepository schoolReportsRepository;
SchoolReportLightRepository schoolReportsLightRepository;
SchoolReportTransformer schoolReportsTransformer;
CommonService commonService;
InstituteService instituteService;

private static final Logger logger = LoggerFactory.getLogger(SchoolReportService.class);

@Autowired
public SchoolReportService(SchoolReportRepository schoolReportsRepository, SchoolReportTransformer schoolReportsTransformer, CommonService commonService, SchoolReportLightRepository schoolReportsLightRepository, InstituteService instituteService) {
public SchoolReportService(SchoolReportRepository schoolReportsRepository, SchoolReportTransformer schoolReportsTransformer, SchoolReportLightRepository schoolReportsLightRepository, InstituteService instituteService) {
this.schoolReportsRepository = schoolReportsRepository;
this.schoolReportsTransformer = schoolReportsTransformer;
this.commonService = commonService;
this.schoolReportsLightRepository = schoolReportsLightRepository;
this.instituteService = instituteService;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,9 @@ public void testGetAllStudentCredentialDistributionList() {
final StudentCredentialDistribution cred = new StudentCredentialDistribution(UUID.randomUUID(),"BC2018-IND",studentID,"YED4","COMPL", new Date());
list.add(cred);

Mockito.when(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,req,false,"accessToken")).thenReturn(list);
commonController.getStudentCredentialsForUserRequestDisRun(credentialType,req,"accessToken");
Mockito.verify(commonService).getStudentCredentialsForUserRequestDisRun(credentialType,req,false,"accessToken");
Mockito.when(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,req,false)).thenReturn(list);
commonController.getStudentCredentialsForUserRequestDisRun(credentialType,req);
Mockito.verify(commonService).getStudentCredentialsForUserRequestDisRun(credentialType,req,false);
}

@Test
Expand All @@ -59,15 +59,37 @@ public void testGetAllStudentCredentialDistributionListWithNullDistributionDate(
final UUID studentID = UUID.randomUUID();
final String credentialType = "E";
final StudentSearchRequest req = new StudentSearchRequest();
req.setDistricts(List.of("005"));
req.setDistrictIds(List.of(UUID.randomUUID()));

// Student Certificate Types
final List<StudentCredentialDistribution> list = new ArrayList<>();
final StudentCredentialDistribution cred = new StudentCredentialDistribution(UUID.randomUUID(),"BC2018-IND",studentID,"YED4","COMPL", new Date());
list.add(cred);

Mockito.when(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,req,true,"accessToken")).thenReturn(list);
commonController.getStudentCredentialsForUserRequestDisRunWithNullDistributionDate(credentialType,req,"accessToken");
Mockito.verify(commonService).getStudentCredentialsForUserRequestDisRun(credentialType,req,true,"accessToken");
Mockito.when(commonService.getStudentCredentialsForUserRequestDisRun(credentialType,req,true)).thenReturn(list);
commonController.getStudentCredentialsForUserRequestDisRunWithNullDistributionDate(credentialType,req);
Mockito.verify(commonService).getStudentCredentialsForUserRequestDisRun(credentialType,req,true);
}

@Test
public void testGetReportsCount_givenACHV() {
final String reportType = "ACHV";
final List<UUID> reportContainerIds = List.of(UUID.randomUUID(), UUID.randomUUID());
final Integer expectedCount = 5;

Mockito.when(commonService.countByStudentGuidsAndReportType(reportContainerIds, reportType)).thenReturn(expectedCount);
commonController.getReportsCount(reportType, reportContainerIds);
Mockito.verify(commonService).countByStudentGuidsAndReportType(reportContainerIds, reportType);
}

@Test
public void testGetReportsCount_givenNotACHV() {
final String reportType = "TEST";
final List<UUID> reportContainerIds = List.of(UUID.randomUUID(), UUID.randomUUID());
final Integer expectedCount = 5;

Mockito.when(commonService.countBySchoolOfRecordsAndReportType(reportContainerIds, reportType)).thenReturn(expectedCount);
commonController.getReportsCount(reportType, reportContainerIds);
Mockito.verify(commonService).countBySchoolOfRecordsAndReportType(reportContainerIds, reportType);
}
}
Loading

0 comments on commit 637753e

Please sign in to comment.