Skip to content

Commit

Permalink
Merge pull request #1517 from bcgov/fix/snapshotForHistorical
Browse files Browse the repository at this point in the history
Fix for snapshot date
  • Loading branch information
arcshiftsolutions authored Jan 15, 2025
2 parents de83bc5 + 1f0d7c1 commit 6b10cbf
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -190,13 +190,14 @@ public boolean noCoursesForSchoolAgedStudentInLastTwoYears(StudentRuleData stude
var reportedByOnlineOrContEdSchool = StringUtils.equals(school.getFacilityTypeCode(), FacilityTypeCodes.DIST_LEARN.getCode()) || StringUtils.equals(school.getFacilityTypeCode(), FacilityTypeCodes.DISTONLINE.getCode());
var zeroCourses = TransformUtil.parseNumberOfCourses(student.getNumberOfCourses(), student.getSdcSchoolCollection().getSdcSchoolCollectionID()) == 0;
boolean isSchoolAged = Boolean.TRUE.equals(student.getIsSchoolAged());
LocalDate currentCollectionSnapshotDate = studentRuleData.getSdcSchoolCollectionStudentEntity().getSdcSchoolCollection().getCollectionEntity().getSnapshotDate();

if (isSchoolAged && isEightPlusGradeCode && reportedByOnlineOrContEdSchool && zeroCourses) {
if(studentRuleData.getSdcSchoolCollectionStudentEntity().getAssignedStudentId() == null) {
return true;
}
validationRulesService.setupMergedStudentIdValues(studentRuleData);
var lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()), student.getSdcSchoolCollection().getSdcSchoolCollectionID());
var lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()), student.getSdcSchoolCollection().getSdcSchoolCollectionID(), currentCollectionSnapshotDate);
return lastTwoYearsOfCollections.isEmpty() || sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(studentRuleData.getHistoricStudentIds(), lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(), "0") == 0;
}
return false;
Expand All @@ -213,13 +214,14 @@ public boolean noCoursesForAdultStudentInLastTwoYears(StudentRuleData studentRul
var reportedByOnlineSchool = StringUtils.equals(school.getFacilityTypeCode(), FacilityTypeCodes.DIST_LEARN.getCode()) || StringUtils.equals(school.getFacilityTypeCode(), FacilityTypeCodes.DISTONLINE.getCode());
var zeroCourses = TransformUtil.parseNumberOfCourses(student.getNumberOfCourses(), student.getSdcSchoolCollection().getSdcSchoolCollectionID()) == 0;
boolean isAdult = Boolean.TRUE.equals(student.getIsAdult());
LocalDate currentCollectionSnapshotDate = studentRuleData.getSdcSchoolCollectionStudentEntity().getSdcSchoolCollection().getCollectionEntity().getSnapshotDate();

if (isAdult && isAllowedAdultGradeCode && reportedByOnlineSchool && zeroCourses) {
if(studentRuleData.getSdcSchoolCollectionStudentEntity().getAssignedStudentId() == null) {
return true;
}
validationRulesService.setupMergedStudentIdValues(studentRuleData);
var lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()), student.getSdcSchoolCollection().getSdcSchoolCollectionID());
var lastTwoYearsOfCollections = sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(UUID.fromString(school.getSchoolId()), student.getSdcSchoolCollection().getSdcSchoolCollectionID(), currentCollectionSnapshotDate);
return lastTwoYearsOfCollections.isEmpty() || sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(studentRuleData.getHistoricStudentIds(), lastTwoYearsOfCollections.stream().map(SdcSchoolCollectionEntity::getSdcSchoolCollectionID).toList(), "0") == 0;
}
return false;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,9 +39,10 @@ public interface SdcSchoolCollectionRepository extends JpaRepository<SdcSchoolCo
where col.collection_id = ssoc.collection_id
and ssoc.sdc_school_collection_id = :sdcCollectionID
)
AND C.snapshot_date < :snapshotDate
AND ssc.sdc_school_collection_id != :sdcCollectionID"""
, nativeQuery = true)
List<SdcSchoolCollectionEntity> findAllCollectionsForSchoolInLastTwoYears(UUID schoolId, UUID sdcCollectionID);
List<SdcSchoolCollectionEntity> findAllCollectionsForSchoolInLastTwoYears(UUID schoolId, UUID sdcCollectionID, LocalDate snapshotDate);

@Query("""
SELECT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -556,6 +556,7 @@ public UUID createMockHistoricalCollection(int yearsAgo, UUID schoolID, LocalDat
LocalDateTime historicalOpenDate = currentCollectionOpenDate.minusYears(yearsAgo);
CollectionEntity historicalCollectionEntity = createMockCollectionEntity();
historicalCollectionEntity.setOpenDate(historicalOpenDate);
historicalCollectionEntity.setSnapshotDate(LocalDate.from(historicalOpenDate.plusDays(20)));
collectionRepository.save(historicalCollectionEntity);

SdcSchoolCollectionEntity historicalSdcSchoolCollectionEntity = createMockSdcSchoolCollectionEntity(historicalCollectionEntity, schoolID);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -976,11 +976,17 @@ void noCoursesForStudentInLastTwoYears_NotSchoolAged_ShouldReturnFalse() {
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(1));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1000,7 +1006,7 @@ void noCoursesForStudentInLastTwoYears_NotSchoolAged_ShouldReturnFalse() {
studentData.setSdcSchoolCollectionStudentEntity(student);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(lastTwoYearsOfCollections);
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(0L);
Expand All @@ -1026,11 +1032,17 @@ void noCoursesForStudentInLastTwoYears_NoCoursesInLastTwoYears_ShouldReturnTrue(
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(1));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1050,7 +1062,7 @@ void noCoursesForStudentInLastTwoYears_NoCoursesInLastTwoYears_ShouldReturnTrue(
student.setSdcSchoolCollection(schoolCollection1);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(lastTwoYearsOfCollections);
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(0L);
Expand All @@ -1076,11 +1088,17 @@ void noCoursesForStudentInLastTwoYears_OneCourseInLastTwoYears_ReturnsFalse() {
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(1));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1100,7 +1118,7 @@ void noCoursesForStudentInLastTwoYears_OneCourseInLastTwoYears_ReturnsFalse() {
student.setSdcSchoolCollection(schoolCollection1);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(lastTwoYearsOfCollections);
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(1L);
Expand Down Expand Up @@ -1146,11 +1164,17 @@ void noCoursesForStudentInLastTwoYears_NoCollectionsInLastTwoYears_ReturnsTrue()
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(1));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1168,7 +1192,7 @@ void noCoursesForStudentInLastTwoYears_NoCollectionsInLastTwoYears_ReturnsTrue()
student.setSdcSchoolCollection(schoolCollection1);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(List.of());
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(1L);
Expand Down Expand Up @@ -1202,11 +1226,17 @@ void noCoursesForStudentInLastTwoYears_GivenDifferentFacilityTypesAndNumCourses_
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(1));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1226,7 +1256,7 @@ void noCoursesForStudentInLastTwoYears_GivenDifferentFacilityTypesAndNumCourses_
student.setSdcSchoolCollection(schoolCollection1);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(lastTwoYearsOfCollections);
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(0L);
Expand Down Expand Up @@ -1271,11 +1301,17 @@ void noCoursesForStudentInLastTwoYears_GivenAllGrades_ShouldReturnTrue(String en
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1295,7 +1331,7 @@ void noCoursesForStudentInLastTwoYears_GivenAllGrades_ShouldReturnTrue(String en
student.setSdcSchoolCollection(schoolCollection1);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(lastTwoYearsOfCollections);
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(0L);
Expand All @@ -1320,11 +1356,17 @@ void noCoursesForStudentInLastTwoYears_NumberOfCoursesNull_ReturnsTrue() {
schoolTombstone.setFacilityTypeCode(facilityTypeCode);

SdcSchoolCollectionEntity schoolCollection1 = new SdcSchoolCollectionEntity();
var mockCollection1 = createMockCollectionEntity();
mockCollection1.setSnapshotDate(LocalDate.now().minusYears(1));
schoolCollection1.setCollectionEntity(mockCollection1);
schoolCollection1.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection1.setSchoolID(schoolId);
schoolCollection1.setCreateDate(studentCreateDate.minusYears(1)); // One year ago

SdcSchoolCollectionEntity schoolCollection2 = new SdcSchoolCollectionEntity();
var mockCollection2 = createMockCollectionEntity();
mockCollection2.setSnapshotDate(LocalDate.now().minusYears(2));
schoolCollection1.setCollectionEntity(mockCollection2);
schoolCollection2.setSdcSchoolCollectionID(UUID.randomUUID());
schoolCollection2.setSchoolID(schoolId);
schoolCollection2.setCreateDate(studentCreateDate.minusYears(2)); // Two years ago
Expand All @@ -1343,7 +1385,7 @@ void noCoursesForStudentInLastTwoYears_NumberOfCoursesNull_ReturnsTrue() {
student.setSdcSchoolCollection(schoolCollection1);
studentData.setHistoricStudentIds(List.of(UUID.fromString(getStudentMergeResult().getStudentID()), student.getAssignedStudentId()));

when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any()))
when(sdcSchoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(any(UUID.class), any(), any()))
.thenReturn(lastTwoYearsOfCollections);
when(sdcSchoolCollectionStudentRepository.countByAssignedStudentIdInAndSdcSchoolCollection_SdcSchoolCollectionIDInAndNumberOfCoursesGreaterThan(anyList(), anyList(), any(String.class)))
.thenReturn(0L);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ void testGetPreviousCollectionID_givenNoCollections_shouldReturnNull() {
schoolCollectionEntity.setCreateDate(createDate);
schoolCollectionEntity.setCollectionEntity(collectionEntity);

when(schoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(eq(schoolId), any()))
when(schoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(eq(schoolId), any(), any()))
.thenReturn(new ArrayList<>());

// When
Expand All @@ -195,7 +195,7 @@ void testGetPreviousCollectionIDJuly_givenNoCollections_shouldReturnNull() {
schoolCollectionEntity.setCreateDate(createDate);
schoolCollectionEntity.setCollectionEntity(collectionEntity);

when(schoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(eq(schoolId), any()))
when(schoolCollectionRepository.findAllCollectionsForSchoolInLastTwoYears(eq(schoolId), any(), any()))
.thenReturn(new ArrayList<>());

// When
Expand Down

0 comments on commit 6b10cbf

Please sign in to comment.