Skip to content

Commit

Permalink
Merge pull request #229 from bcgov/fix/EDX-2070
Browse files Browse the repository at this point in the history
EDX-2070: Move School number fix
  • Loading branch information
arcshiftsolutions authored Nov 30, 2023
2 parents 6549abb + f8ce5c0 commit b14aed6
Show file tree
Hide file tree
Showing 3 changed files with 417 additions and 72 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package ca.bc.gov.educ.api.institute.service.v1;

import ca.bc.gov.educ.api.institute.constants.v1.Constants;
import ca.bc.gov.educ.api.institute.exception.ConflictFoundException;
import ca.bc.gov.educ.api.institute.exception.EntityNotFoundException;
import ca.bc.gov.educ.api.institute.mapper.v1.NoteMapper;
Expand Down Expand Up @@ -115,24 +116,14 @@ private SchoolEntity createSchoolHelper(School school) {
UUID.fromString(school.getDistrictId()));
if (district.isPresent()) {
schoolEntity.setDistrictEntity(district.get());
}

if (district.isPresent()) {
if (school.getSchoolNumber() != null) {
List<SchoolEntity> schools = schoolRepository.findBySchoolNumberAndDistrictID(
school.getSchoolNumber(), UUID.fromString(school.getDistrictId()));
if (!schools.isEmpty()) {
throw new ConflictFoundException("School Number already exists for this district.");
}
schoolEntity.setSchoolNumber(school.getSchoolNumber());
} else {
schoolEntity.setSchoolNumber(
schoolNumberGenerationService.generateSchoolNumber(district.get().getDistrictNumber(),
school.getFacilityTypeCode(), school.getSchoolCategoryCode(),
school.getIndependentAuthorityId()));
}

schoolEntity.setSchoolNumber(schoolNumberGenerationService.generateSchoolNumber(district.get().getDistrictNumber(),
school.getFacilityTypeCode(), school.getSchoolCategoryCode(),
school.getIndependentAuthorityId()));
} else {
throw new EntityNotFoundException(DistrictTombstoneEntity.class, school.getDistrictId(), school.getSchoolId());
}

schoolEntity.getAddresses().stream().forEach(address -> {
RequestUtil.setAuditColumnsForAddress(address);
TransformUtil.uppercaseFields(address);
Expand Down Expand Up @@ -413,25 +404,14 @@ public void deleteSchoolNote(UUID schoolId, UUID noteId) {
public Pair<MoveSchoolData, InstituteEvent> moveSchool(MoveSchoolData moveSchoolData)
throws JsonProcessingException {

Boolean schoolNumberExistsInDistrict = schoolRepository.existsBySchoolNumberAndDistrictID(
moveSchoolData.getToSchool().getSchoolNumber(),
UUID.fromString(moveSchoolData.getToSchool().getDistrictId()));

if (Boolean.TRUE.equals(schoolNumberExistsInDistrict)) {
log.info("School number {} exists in district {} setting school number to null",
moveSchoolData.getToSchool().getSchoolNumber(),
moveSchoolData.getToSchool().getDistrictId());
moveSchoolData.getToSchool().setSchoolNumber(null);
}

SchoolEntity movedSchool = createSchoolHelper(moveSchoolData.getToSchool());
log.info("School created for move schoolId :: {}", movedSchool.getSchoolId());

val schoolEntityOptional = schoolRepository.findById(
UUID.fromString(moveSchoolData.getFromSchoolId()));
UUID.fromString(moveSchoolData.getFromSchoolId()));
SchoolEntity fromSchoolEntity = schoolEntityOptional.orElseThrow(
() -> new EntityNotFoundException(SchoolEntity.class, FROM_SCHOOL_ID_ATTR,
moveSchoolData.getFromSchoolId()));
() -> new EntityNotFoundException(SchoolEntity.class, FROM_SCHOOL_ID_ATTR,
moveSchoolData.getFromSchoolId()));

SchoolEntity movedSchool = moveSchoolHelper(moveSchoolData.getToSchool(), SchoolMapper.mapper.toStructure(fromSchoolEntity));
log.info("School created for move schoolId :: {}", movedSchool.getSchoolId());

fromSchoolEntity.setClosedDate(LocalDateTime.parse(moveSchoolData.getMoveDate()));
fromSchoolEntity.setUpdateUser(moveSchoolData.getToSchool().getCreateUser());
Expand Down Expand Up @@ -460,4 +440,51 @@ public Pair<MoveSchoolData, InstituteEvent> moveSchool(MoveSchoolData moveSchool
JsonUtil.getJsonStringFromObject(moveSchoolData), MOVE_SCHOOL, SCHOOL_MOVED);
return Pair.of(moveSchoolData, instituteEvent);
}

private SchoolEntity moveSchoolHelper(School school, School existingSchool) {
var schoolEntity = SchoolMapper.mapper.toModel(school);
Optional<DistrictTombstoneEntity> district = districtTombstoneRepository.findById(
UUID.fromString(school.getDistrictId()));
if (district.isPresent()) {
schoolEntity.setDistrictEntity(district.get());

List<SchoolEntity> schools = schoolRepository.findBySchoolNumberAndDistrictID(
school.getSchoolNumber(), UUID.fromString(school.getDistrictId()));

if(school.getSchoolCategoryCode().equals(existingSchool.getSchoolCategoryCode()) && schools.isEmpty()) {
schoolEntity.setSchoolNumber(school.getSchoolNumber());
} else {
schoolEntity.setSchoolNumber(
schoolNumberGenerationService.generateSchoolNumber(district.get().getDistrictNumber(),
school.getFacilityTypeCode(), school.getSchoolCategoryCode(),
school.getIndependentAuthorityId()));
}
} else {
throw new EntityNotFoundException(DistrictTombstoneEntity.class, school.getDistrictId(), school.getSchoolId());
}

schoolEntity.getAddresses().stream().forEach(address -> {
RequestUtil.setAuditColumnsForAddress(address);
TransformUtil.uppercaseFields(address);
address.setSchoolEntity(schoolEntity);
});

schoolEntity.getGrades().stream().forEach(grade -> {
RequestUtil.setAuditColumnsForGrades(grade);
TransformUtil.uppercaseFields(grade);
grade.setSchoolEntity(schoolEntity);
});

schoolEntity.getNeighborhoodLearning().stream().forEach(neighborhoodLearning -> {
RequestUtil.setAuditColumnsForNeighborhoodLearning(neighborhoodLearning);
TransformUtil.uppercaseFields(neighborhoodLearning);
neighborhoodLearning.setSchoolEntity(schoolEntity);
});

TransformUtil.uppercaseFields(schoolEntity);
schoolRepository.save(schoolEntity);
schoolHistoryService.createSchoolHistory(schoolEntity, school.getCreateUser());

return schoolEntity;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -553,34 +553,6 @@ void testCreateSchool_GivenValidPayload_ShouldReturnStatusOK() throws Exception
.andExpect(MockMvcResultMatchers.jsonPath("$.displayNameNoSpecialChars").value(mappedSchool.getDisplayNameNoSpecialChars()));
}

@Test
void testCreateSchoolWithSchoolNumberConflict_GivenValidPayload_ShouldReturnStatusCONFLICT() throws Exception {
final DistrictTombstoneEntity dist = this.districtTombstoneRepository.save(this.createDistrictData());
var existingSchoolEntity = this.createNewSchoolData("99000", "PUBLIC", "DISTONLINE");
existingSchoolEntity.setDistrictEntity(dist);
schoolRepository.save(existingSchoolEntity);

var schoolEntity = this.createNewSchoolData("99000", "PUBLIC", "DISTONLINE");
SchoolMapper map = SchoolMapper.mapper;

School mappedSchool = map.toStructure(schoolEntity);

mappedSchool.setDistrictId(dist.getDistrictId().toString());
mappedSchool.setCreateDate(null);
mappedSchool.setUpdateDate(null);
mappedSchool.setGrades(List.of(createSchoolGrade()));
mappedSchool.setNeighborhoodLearning(List.of(createNeighborhoodLearning()));
mappedSchool.setAddresses(List.of(createSchoolAddress()));

this.mockMvc.perform(post(URL.BASE_URL_SCHOOL)
.contentType(MediaType.APPLICATION_JSON)
.accept(MediaType.APPLICATION_JSON)
.content(asJsonString(mappedSchool))
.with(jwt().jwt((jwt) -> jwt.claim("scope", "WRITE_SCHOOL"))))
.andDo(print())
.andExpect(status().isConflict());
}

@Test
void testCreateSchool_GivenInvalidPayload_ShouldReturnStatusBadRequest() throws Exception {
final var school = this.createSchoolData();
Expand Down
Loading

0 comments on commit b14aed6

Please sign in to comment.