Skip to content

Commit

Permalink
Merge pull request #434 from bcgov/feature/softDelete
Browse files Browse the repository at this point in the history
Cleanup for save history
  • Loading branch information
alexmcdermid authored Feb 6, 2024
2 parents df88d13 + f605ded commit 8e168d7
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void processStudentRecord(final Event event, final SdcSagaEntity saga, fi
saga.setStatus(IN_PROGRESS.toString());
this.getSagaService().updateAttachedSagaWithEvents(saga, eventStates);

this.sdcSchoolCollectionStudentService.processSagaRecord(UUID.fromString(sdcStudentSagaData.getSdcSchoolCollectionStudent().getSdcSchoolCollectionStudentID()), sdcStudentSagaData.getSchool(), sdcStudentSagaData.getCollectionTypeCode());
this.sdcSchoolCollectionStudentService.processSagaStudentRecord(UUID.fromString(sdcStudentSagaData.getSdcSchoolCollectionStudent().getSdcSchoolCollectionStudentID()), sdcStudentSagaData.getSchool());

final Event.EventBuilder eventBuilder = Event.builder();
eventBuilder.sagaId(saga.getSagaId()).eventType(PROCESS_SDC_STUDENT);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package ca.bc.gov.educ.studentdatacollection.api.service.v1;

import ca.bc.gov.educ.studentdatacollection.api.calculator.FteCalculatorChainProcessor;
import ca.bc.gov.educ.studentdatacollection.api.calculator.FteCalculatorUtils;
import ca.bc.gov.educ.studentdatacollection.api.constants.EventOutcome;
import ca.bc.gov.educ.studentdatacollection.api.constants.EventType;
import ca.bc.gov.educ.studentdatacollection.api.constants.StudentValidationIssueSeverityCode;
Expand Down Expand Up @@ -114,22 +113,32 @@ public SdcSchoolCollectionStudentEntity updateAndValidateSdcSchoolCollectionStud
log.debug("SdcSchoolCollectionStudent was not saved to the database because it has errors :: {}", processedSdcSchoolCollectionStudent);
return processedSdcSchoolCollectionStudent;
} else {
var entity = this.sdcSchoolCollectionStudentRepository.save(processedSdcSchoolCollectionStudent);
this.sdcSchoolCollectionStudentHistoryService.createSDCSchoolStudentHistory(entity, processedSdcSchoolCollectionStudent.getUpdateUser());
return entity;
return saveSdcStudentWithHistory(processedSdcSchoolCollectionStudent);
}
} else {
throw new EntityNotFoundException(SdcSchoolCollectionStudentEntity.class, "SdcSchoolCollectionStudentEntity", studentEntity.getSdcSchoolCollectionStudentID().toString());
}
}

public SdcSchoolCollectionStudentEntity saveSdcStudentWithHistory(SdcSchoolCollectionStudentEntity studentEntity) {
var entity = this.sdcSchoolCollectionStudentRepository.save(studentEntity);
this.sdcSchoolCollectionStudentHistoryService.createSDCSchoolStudentHistory(entity, studentEntity.getUpdateUser());
return entity;
}

public List<SdcSchoolCollectionStudentEntity> saveAllSdcStudentWithHistory(List<SdcSchoolCollectionStudentEntity> studentEntities) {
var entities = this.sdcSchoolCollectionStudentRepository.saveAll(studentEntities);
entities.forEach(entity -> this.sdcSchoolCollectionStudentHistoryService.createSDCSchoolStudentHistory(entity, entity.getUpdateUser()));

return entities;
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
public void processSagaRecord(final UUID sdcSchoolCollectionStudentID, School school, String collectionTypeCode) {
public void processSagaStudentRecord(final UUID sdcSchoolCollectionStudentID, School school) {
var currentStudentEntity = this.sdcSchoolCollectionStudentRepository.findById(sdcSchoolCollectionStudentID);

if(currentStudentEntity.isPresent()) {
var entity = this.sdcSchoolCollectionStudentRepository.save(processStudentRecord(school, currentStudentEntity.get()));
this.sdcSchoolCollectionStudentHistoryService.createSDCSchoolStudentHistory(entity, entity.getUpdateUser());
saveSdcStudentWithHistory(processStudentRecord(school, currentStudentEntity.get()));
} else {
throw new EntityNotFoundException(SdcSchoolCollectionStudentEntity.class, "SdcSchoolCollectionStudentEntity", sdcSchoolCollectionStudentID.toString());
}
Expand Down Expand Up @@ -324,7 +333,7 @@ public SdcSchoolCollectionStudentEntity softDeleteSdcSchoolCollectionStudent(UUI
this.sdcStudentValidationErrorRepository.deleteSdcStudentValidationErrors(student.getSdcSchoolCollectionStudentID());
student.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.DELETED.toString());

return sdcSchoolCollectionStudentRepository.save(student);
return saveSdcStudentWithHistory(student);
}

@Transactional(propagation = Propagation.REQUIRES_NEW)
Expand All @@ -336,7 +345,7 @@ public List<SdcSchoolCollectionStudentEntity> softDeleteSdcSchoolCollectionStude
student.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.DELETED.toString());
});

return sdcSchoolCollectionStudentRepository.saveAll(sdcSchoolCollectionStudentEntities);
return saveAllSdcStudentWithHistory(sdcSchoolCollectionStudentEntities);
}

public SdcStudentEll createOrReturnSdcStudentEll(SdcStudentEll studentEll) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@
import ca.bc.gov.educ.studentdatacollection.api.constants.v1.SdcSchoolStudentStatus;
import ca.bc.gov.educ.studentdatacollection.api.exception.EntityNotFoundException;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentHistoryEntity;
import ca.bc.gov.educ.studentdatacollection.api.model.v1.SdcSchoolCollectionStudentValidationIssueEntity;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionStudentRepository;
import ca.bc.gov.educ.studentdatacollection.api.repository.v1.SdcSchoolCollectionStudentValidationIssueRepository;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.FteCalculationResult;
import ca.bc.gov.educ.studentdatacollection.api.struct.v1.SdcSchoolCollectionStudentHistory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.InjectMocks;
Expand All @@ -31,6 +33,9 @@ class SdcSchoolCollectionStudentServiceTest {
@InjectMocks
private SdcSchoolCollectionStudentService sdcSchoolCollectionStudentService;

@Mock
private SdcSchoolCollectionStudentHistoryService sdcSchoolCollectionStudentHistoryService;

@Mock
SdcSchoolCollectionStudentValidationIssueRepository sdcSchoolCollectionStudentValidationIssueRepository;

Expand Down Expand Up @@ -243,6 +248,8 @@ void testSoftDeleteSdcSchoolCollectionStudents_WhenStudentExists_SavesEntity() {

SdcSchoolCollectionStudentValidationIssueEntity mockValidationError = new SdcSchoolCollectionStudentValidationIssueEntity();
when(sdcSchoolCollectionStudentValidationIssueRepository.findById(any())).thenReturn(Optional.of(mockValidationError));
SdcSchoolCollectionStudentHistoryEntity studentHistoryEntity = new SdcSchoolCollectionStudentHistoryEntity();
when(sdcSchoolCollectionStudentHistoryService.createSDCSchoolStudentHistory(any(), any())).thenReturn(studentHistoryEntity);

// When
sdcSchoolCollectionStudentService.softDeleteSdcSchoolCollectionStudent(sdcSchoolCollectionStudentID);
Expand Down

0 comments on commit 8e168d7

Please sign in to comment.