Skip to content

Commit

Permalink
Fix username for student history
Browse files Browse the repository at this point in the history
  • Loading branch information
arybakov-cgi committed Aug 20, 2024
1 parent c731c23 commit ee4c49f
Show file tree
Hide file tree
Showing 12 changed files with 190 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
package ca.bc.gov.educ.api.gradstudent.controller;

import java.util.List;

import ca.bc.gov.educ.api.gradstudent.model.dto.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import ca.bc.gov.educ.api.gradstudent.service.GradStudentService;
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants;
import ca.bc.gov.educ.api.gradstudent.util.PermissionsConstants;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

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

@RestController
@CrossOrigin
Expand All @@ -26,7 +27,8 @@ public class GradStudentController {

@SuppressWarnings("unused")
private static final Logger logger = LoggerFactory.getLogger(GradStudentController.class);

private static final String BEARER = "Bearer ";

private final GradStudentService gradStudentService;

public GradStudentController(GradStudentService gradStudentService) {
Expand Down Expand Up @@ -116,4 +118,12 @@ public GraduationStudentRecordDistribution getGradStudentByStudentIDFromGRAD(@Pa
public Student addNewPenFromStudentAPI(@Validated @RequestBody StudentCreate student, @RequestHeader(name="Authorization") String accessToken) {
return gradStudentService.addNewPenFromStudentAPI(student, accessToken.replaceAll("Bearer ", ""));
}

@PostMapping (EducGradStudentApiConstants.GRAD_STUDENT_BY_SEARCH_CRITERIAS)
@PreAuthorize(PermissionsConstants.READ_GRADUATION_STUDENT)
@Operation(summary = "Find Students by StudentSearchRequest criteria", description = "Find Students by StudentSearchRequest criteria", tags = { "Search Student Records" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public List<UUID> searchGraduationStudentRecords(@RequestBody StudentSearchRequest searchRequest) {
return gradStudentService.getStudentIDsBySearchCriteriaOrAll(searchRequest);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -359,9 +359,9 @@ public ResponseEntity<GraduationStudentRecord> saveStudentGradStatusDistribution
@PreAuthorize(PermissionsConstants.UPDATE_GRADUATION_STUDENT)
@Operation(summary = "Save Student Grad Status by Student ID for projected run", description = "Save Student Grad Status by Student ID for projected run", tags = { "Student Graduation Status" })
@ApiResponses(value = {@ApiResponse(responseCode = "200", description = "OK")})
public ResponseEntity<ApiResponseModel<Integer>> updateStudentGradHistoryStatusDistributionRun(@PathVariable Long batchID, @RequestParam(required = false) String userName, @RequestParam(required = false) String activityCode) {
public ResponseEntity<ApiResponseModel<Integer>> updateStudentGradHistoryStatusDistributionRun(@PathVariable Long batchID, @RequestParam(required = false) String userName, @RequestParam(required = false) String activityCode, @RequestBody List<UUID> studentGuids) {
logger.debug("Save Distribution student Grad history for Student ID");
return response.UPDATED(historyService.updateStudentRecordHistoryDistributionRun(batchID, userName, activityCode));
return response.UPDATED(historyService.updateStudentRecordHistoryDistributionRun(batchID, userName, activityCode, studentGuids));
}

@GetMapping (EducGradStudentApiConstants.STUDENT_LIST_FOR_SCHOOL_REPORT)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,15 @@ public interface GraduationStudentRecordRepository extends JpaRepository<Graduat
@Query("select c.studentID from GraduationStudentRecordEntity c where c.schoolOfRecord IN (:schoolOfRecords) and c.studentStatus=:studentStatus")
List<UUID> findBySchoolOfRecordInAndStudentStatus(List<String> schoolOfRecords, String studentStatus);

@Query("select c.studentID from GraduationStudentRecordEntity c where c.schoolOfRecord IN (:schoolOfRecords)")
List<UUID> findBySchoolOfRecordIn(List<String> schoolOfRecords);

@Query("select c.studentID from GraduationStudentRecordEntity c where c.studentStatus=:studentStatus")
List<UUID> findByStudentStatus(String studentStatus);

@Query("select c.studentID from GraduationStudentRecordEntity c")
List<UUID> findAllStudentGuids();

List<GraduationStudentRecordView> findBySchoolOfRecordAndStudentStatusAndStudentGradeIn(String schoolOfRecord, String studentStatus, List<String> studentGrade);

@Query("select count(*) from GraduationStudentRecordEntity c where c.schoolOfRecord=:schoolOfRecord and c.studentStatus='CUR' and (c.studentGrade='AD' or c.studentGrade='12')")
Expand All @@ -61,6 +67,9 @@ public interface GraduationStudentRecordRepository extends JpaRepository<Graduat
@Query("select count(*) from GraduationStudentRecordEntity c where c.schoolOfRecord IN (:schoolOfRecords) and c.studentStatus=:studentStatus")
Long countBySchoolOfRecordsAndStudentStatus(List<String> schoolOfRecords, String studentStatus);

@Query("select count(*) from GraduationStudentRecordEntity c where c.schoolOfRecord IN (:schoolOfRecords)")
Long countBySchoolOfRecords(List<String> schoolOfRecords);

@Query("select count(*) from GraduationStudentRecordEntity c where c.studentStatus=:studentStatus")
Long countByStudentStatus(String studentStatus);

Expand Down Expand Up @@ -96,8 +105,8 @@ void updateStudentGuidPenXrefRecord(
long countStudentGuidPenXrefRecord(@Param("studentGuid") UUID studentGuid);

@Query(value="select STUDENT_GUID from STUDENT_GUID_PEN_XREF \n"
+ "where STUDENT_PEN = :pen", nativeQuery = true)
byte[] findStudentID(@Param("pen") String pen);
+ "where STUDENT_PEN in (:pens)", nativeQuery = true)
List<UUID> findStudentIDsByPenIn(@Param("pens") List<String> pens);

@Query("select c.studentID from GraduationStudentRecordEntity c where c.studentStatus = :statusCode and c.studentID in :studentIDList")
List<UUID> filterGivenStudentsByStatusCode(@Param("studentIDList") List<UUID> studentIDs, @Param("statusCode") String statusCode);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public GradOnlyStudentSearch getGRADStudents(StudentSearchRequest studentSearchR
h.set(EducGradStudentApiConstants.CORRELATION_ID, ThreadLocalStateUtil.getCorrelationID());
})
.retrieve().bodyToMono(new ParameterizedTypeReference<RestResponsePage<Student>>() {}).block();
List<Student> studentList = response != null ?response.getContent():new ArrayList<>();
List<Student> studentList = response != null ? response.getContent() : new ArrayList<>();
if (!studentList.isEmpty()) {
studentList.forEach(st -> {
GradSearchStudent gradStu = populateGradSearchStudent(st, accessToken);
Expand Down Expand Up @@ -405,4 +405,19 @@ public List<UUID> getStudentIDsByStatusCode(List<UUID> studentIDs, String status
}
return results;
}

public List<UUID> getStudentIDsBySearchCriteriaOrAll(StudentSearchRequest searchRequest) {
ArrayList<UUID> result = new ArrayList<>();
result.addAll(searchRequest.getStudentIDs());
if(searchRequest.getPens() != null && !searchRequest.getPens().isEmpty()) {
result.addAll(graduationStatusRepository.findStudentIDsByPenIn(searchRequest.getPens()));
}
if(searchRequest.getSchoolOfRecords() != null && !searchRequest.getSchoolOfRecords().isEmpty()) {
result.addAll(graduationStatusRepository.findBySchoolOfRecordIn(searchRequest.getSchoolOfRecords()));
}
if(result.isEmpty()) {
result.addAll(graduationStatusRepository.findAllStudentGuids());
}
return result;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -1385,10 +1385,12 @@ public Integer countStudentsForAmalgamatedSchoolReport(String schoolOfRecord) {
}

public Long countBySchoolOfRecordsAndStudentStatus(List<String> schoolOfRecords, String studentStatus) {
if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) {
return graduationStatusRepository.countBySchoolOfRecordsAndStudentStatus(schoolOfRecords, StringUtils.defaultString(studentStatus, "CUR"));
if(schoolOfRecords != null && !schoolOfRecords.isEmpty() && StringUtils.isNotBlank(studentStatus) && !StringUtils.equalsAnyIgnoreCase(studentStatus, "null")) {
return graduationStatusRepository.countBySchoolOfRecordsAndStudentStatus(schoolOfRecords, StringUtils.upperCase(studentStatus));
} else if(StringUtils.isNotBlank(studentStatus) && !StringUtils.equalsAnyIgnoreCase(studentStatus, "null")) {
return graduationStatusRepository.countByStudentStatus(StringUtils.upperCase(studentStatus));
} else {
return graduationStatusRepository.countByStudentStatus(StringUtils.defaultString(studentStatus, "CUR"));
return countBySchoolOfRecords(schoolOfRecords);
}
}

Expand Down Expand Up @@ -1448,6 +1450,14 @@ private void saveBatchFlagsOfGraduationStudentRecord(GraduationStudentRecordEnti
}
}

private Long countBySchoolOfRecords(List<String> schoolOfRecords) {
if(schoolOfRecords != null && !schoolOfRecords.isEmpty()) {
return graduationStatusRepository.countBySchoolOfRecords(schoolOfRecords);
} else {
return graduationStatusRepository.count();
}
}

private void resetBatchFlags(GraduationStudentRecordEntity gradEntity, boolean projectedRun) {
String flag = null;
if (gradEntity.getProgram().equalsIgnoreCase("SCCP") && EducGradStudentApiUtils.isDateInFuture(gradEntity.getProgramCompletionDate())) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import ca.bc.gov.educ.api.gradstudent.model.transformer.GraduationStudentRecordHistoryTransformer;
import ca.bc.gov.educ.api.gradstudent.model.transformer.StudentOptionalProgramHistoryTransformer;
import ca.bc.gov.educ.api.gradstudent.repository.GraduationStudentRecordHistoryRepository;
import ca.bc.gov.educ.api.gradstudent.repository.GraduationStudentRecordRepository;
import ca.bc.gov.educ.api.gradstudent.repository.HistoryActivityRepository;
import ca.bc.gov.educ.api.gradstudent.repository.StudentOptionalProgramHistoryRepository;
import ca.bc.gov.educ.api.gradstudent.util.EducGradStudentApiConstants;
Expand Down Expand Up @@ -38,6 +39,7 @@ public class HistoryService {
final
WebClient webClient;

final GraduationStudentRecordRepository graduationStatusRepository;
final GraduationStudentRecordHistoryRepository graduationStudentRecordHistoryRepository;
final GraduationStudentRecordHistoryTransformer graduationStudentRecordHistoryTransformer;
final StudentOptionalProgramHistoryRepository studentOptionalProgramHistoryRepository;
Expand All @@ -46,13 +48,14 @@ public class HistoryService {
final EducGradStudentApiConstants constants;

@Autowired
public HistoryService(WebClient webClient, GraduationStudentRecordHistoryRepository graduationStudentRecordHistoryRepository, GraduationStudentRecordHistoryTransformer graduationStudentRecordHistoryTransformer, StudentOptionalProgramHistoryRepository studentOptionalProgramHistoryRepository, StudentOptionalProgramHistoryTransformer studentOptionalProgramHistoryTransformer, EducGradStudentApiConstants constants, HistoryActivityRepository historyActivityRepository) {
public HistoryService(WebClient webClient, GraduationStudentRecordHistoryRepository graduationStudentRecordHistoryRepository, GraduationStudentRecordHistoryTransformer graduationStudentRecordHistoryTransformer, StudentOptionalProgramHistoryRepository studentOptionalProgramHistoryRepository, StudentOptionalProgramHistoryTransformer studentOptionalProgramHistoryTransformer, EducGradStudentApiConstants constants, HistoryActivityRepository historyActivityRepository, GraduationStudentRecordRepository graduationStatusRepository) {
this.webClient = webClient;
this.graduationStudentRecordHistoryRepository = graduationStudentRecordHistoryRepository;
this.graduationStudentRecordHistoryTransformer = graduationStudentRecordHistoryTransformer;
this.studentOptionalProgramHistoryRepository = studentOptionalProgramHistoryRepository;
this.studentOptionalProgramHistoryTransformer = studentOptionalProgramHistoryTransformer;
this.historyActivityRepository = historyActivityRepository;
this.graduationStatusRepository = graduationStatusRepository;
this.constants = constants;
}

Expand Down Expand Up @@ -160,13 +163,30 @@ public Page<GraduationStudentRecordHistoryEntity> getStudentHistoryByBatchID(Lon
}

@Transactional
public Integer updateStudentRecordHistoryDistributionRun(Long batchId, String updateUser, String activityCode) {
public Integer updateStudentRecordHistoryDistributionRun(Long batchId, String updateUser, String activityCode, List<UUID> studentGuids) {
Integer result = 0;
LocalDateTime updateDate = LocalDateTime.now();
if(StringUtils.isBlank(activityCode) || StringUtils.equalsIgnoreCase(activityCode, "null")) {
return graduationStudentRecordHistoryRepository.updateGradStudentUpdateUser(batchId, updateUser, updateDate);
if(studentGuids != null && !studentGuids.isEmpty()) {
Page<GraduationStudentRecordHistoryEntity> recordHistoryEntityPage = graduationStudentRecordHistoryRepository.findByBatchId(batchId, PageRequest.of(0, Integer.SIZE));
if(recordHistoryEntityPage == null || recordHistoryEntityPage.isEmpty()) {
for (UUID studentGuid : studentGuids) {
GraduationStudentRecordEntity entity = graduationStatusRepository.findByStudentID(studentGuid);
if(entity != null) {
GraduationStudentRecordEntity toBeSaved = new GraduationStudentRecordEntity();
BeanUtils.copyProperties(entity, toBeSaved);
toBeSaved.setUpdateDate(updateDate);
toBeSaved.setUpdateUser(updateUser);
createStudentHistory(toBeSaved, activityCode);
result ++;
}
}
}
} else if(StringUtils.isBlank(activityCode) || StringUtils.equalsIgnoreCase(activityCode, "null")) {
result = graduationStudentRecordHistoryRepository.updateGradStudentUpdateUser(batchId, updateUser, updateDate);
} else {
return graduationStudentRecordHistoryRepository.updateGradStudentUpdateUser(batchId, activityCode, updateUser, updateDate);
result = graduationStudentRecordHistoryRepository.updateGradStudentUpdateUser(batchId, activityCode, updateUser, updateDate);
}
return result;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public class EducGradStudentApiConstants {
public static final String GRAD_STUDENT_DEMOG_BY_PEN = "/demog/pen/{pen}";
public static final String GRAD_STUDENT_BY_STUDENT_ID_STUDENT_API = "/stdid/{studentID}";
public static final String GRAD_STUDENT_BY_STUDENT_ID_GRAD="/grad/{studentID}";
public static final String GRAD_STUDENT_BY_SEARCH_CRITERIAS = "/gradstudentbysearchcriteria";
public static final String GRAD_STUDENT_BY_LAST_NAME = "/gradstudent";
public static final String GRAD_STUDENT_BY_FIRST_NAME = "/studentsearchfirstname";
public static final String GRAD_STUDENT_BY_MULTIPLE_STUDENTIDS = "/multistudentids";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package ca.bc.gov.educ.api.gradstudent.controller;

import ca.bc.gov.educ.api.gradstudent.model.dto.*;
import ca.bc.gov.educ.api.gradstudent.model.dto.GradSearchStudent;
import ca.bc.gov.educ.api.gradstudent.model.dto.StudentCreate;
import ca.bc.gov.educ.api.gradstudent.model.dto.StudentSearch;
import ca.bc.gov.educ.api.gradstudent.model.dto.StudentSearchRequest;
import ca.bc.gov.educ.api.gradstudent.service.GradStudentService;
import org.junit.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -12,6 +15,7 @@
import org.mockito.junit.jupiter.MockitoExtension;

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

import static org.junit.jupiter.api.Assertions.assertEquals;
Expand Down Expand Up @@ -134,4 +138,12 @@ public void testAddNewPenFromStudentAPI() {
Mockito.verify(gradStudentService).addNewPenFromStudentAPI(student, "accessToken");
}

@Test
public void testSearchGraduationStudentRecords() {
StudentSearchRequest searchRequest = StudentSearchRequest.builder().schoolOfRecords(List.of("12345678")).build();
Mockito.when(gradStudentService.getStudentIDsBySearchCriteriaOrAll(searchRequest)).thenReturn(List.of(UUID.randomUUID()));
gradStudentController.searchGraduationStudentRecords(searchRequest);
Mockito.verify(gradStudentService).getStudentIDsBySearchCriteriaOrAll(searchRequest);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -558,6 +558,13 @@ public void testGetStudentsForYearlyRun() {
Mockito.verify(graduationStatusService).getStudentsForYearlyDistribution();
}

@Test
public void testUpdateStudentGradHistoryStatusDistributionRun() {
Mockito.when(historyService.updateStudentRecordHistoryDistributionRun(1L, "USER", "activityCode", List.of())).thenReturn(1);
graduationStatusController.updateStudentGradHistoryStatusDistributionRun(1L, "USER", "activityCode", List.of());
Mockito.verify(historyService).updateStudentRecordHistoryDistributionRun(1L, "USER", "activityCode", List.of());
}

@Test
public void testGetStudentsForSchoolReport() {
// ID
Expand Down Expand Up @@ -610,6 +617,18 @@ public void testGetStudentsCount() {
Mockito.when(graduationStatusService.countBySchoolOfRecordsAndStudentStatus(List.of(mincode), "CUR")).thenReturn(1L);
graduationStatusController.getStudentsCount("CUR", List.of(mincode));
Mockito.verify(graduationStatusService).countBySchoolOfRecordsAndStudentStatus(List.of(mincode), "CUR");

Mockito.when(graduationStatusService.countBySchoolOfRecordsAndStudentStatus(List.of(), "CUR")).thenReturn(1L);
graduationStatusController.getStudentsCount("CUR", List.of());
Mockito.verify(graduationStatusService).countBySchoolOfRecordsAndStudentStatus(List.of(), "CUR");

Mockito.when(graduationStatusService.countBySchoolOfRecordsAndStudentStatus(List.of(mincode), null)).thenReturn(1L);
graduationStatusController.getStudentsCount(null, List.of(mincode));
Mockito.verify(graduationStatusService).countBySchoolOfRecordsAndStudentStatus(List.of(mincode), null);

Mockito.when(graduationStatusService.countBySchoolOfRecordsAndStudentStatus(List.of(), null)).thenReturn(1L);
graduationStatusController.getStudentsCount(null, List.of());
Mockito.verify(graduationStatusService).countBySchoolOfRecordsAndStudentStatus(List.of(), null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -829,7 +829,14 @@ public void testGetStudentIDsByStatusCode() {
assertThat(result.get(0)).isEqualTo(studentID1);
}

@Test
public void testGetStudentIDsBySearchCriterias() {
StudentSearchRequest searchRequest = StudentSearchRequest.builder().schoolOfRecords(List.of("12345678")).build();
when(graduationStatusRepository.findBySchoolOfRecordIn(List.of("12345678"))).thenReturn(List.of(UUID.randomUUID()));
List<UUID> results = gradStudentService.getStudentIDsBySearchCriteriaOrAll(searchRequest);

assertThat(results).isNotEmpty();
}

@SneakyThrows
protected Object createDataObjectFromJson(String jsonPath, Class<?> clazz) {
Expand Down
Loading

0 comments on commit ee4c49f

Please sign in to comment.