From f605ded29d5c1b82c0a055ce5441c86534794d55 Mon Sep 17 00:00:00 2001 From: Marco Villeneuve Date: Tue, 6 Feb 2024 11:27:20 -0800 Subject: [PATCH] Cleanup for save with history --- .../SdcStudentProcessingOrchestrator.java | 2 +- .../v1/SdcSchoolCollectionStudentService.java | 27 ++++++++++++------- ...SdcSchoolCollectionStudentServiceTest.java | 7 +++++ 3 files changed, 26 insertions(+), 10 deletions(-) diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/orchestrator/SdcStudentProcessingOrchestrator.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/orchestrator/SdcStudentProcessingOrchestrator.java index c615bf11f..14bc82e40 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/orchestrator/SdcStudentProcessingOrchestrator.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/orchestrator/SdcStudentProcessingOrchestrator.java @@ -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); diff --git a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentService.java b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentService.java index bf1d5d71e..29f17539d 100644 --- a/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentService.java +++ b/api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentService.java @@ -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; @@ -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 saveAllSdcStudentWithHistory(List 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()); } @@ -321,7 +330,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) @@ -333,7 +342,7 @@ public List softDeleteSdcSchoolCollectionStude student.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.DELETED.toString()); }); - return sdcSchoolCollectionStudentRepository.saveAll(sdcSchoolCollectionStudentEntities); + return saveAllSdcStudentWithHistory(sdcSchoolCollectionStudentEntities); } public SdcStudentEll createOrReturnSdcStudentEll(SdcStudentEll studentEll) { diff --git a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentServiceTest.java b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentServiceTest.java index 8bf712fbd..840f2ac3a 100644 --- a/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentServiceTest.java +++ b/api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionStudentServiceTest.java @@ -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; @@ -31,6 +33,9 @@ class SdcSchoolCollectionStudentServiceTest { @InjectMocks private SdcSchoolCollectionStudentService sdcSchoolCollectionStudentService; + @Mock + private SdcSchoolCollectionStudentHistoryService sdcSchoolCollectionStudentHistoryService; + @Mock SdcSchoolCollectionStudentValidationIssueRepository sdcSchoolCollectionStudentValidationIssueRepository; @@ -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);