Skip to content

Commit 02c65e3

Browse files
Merge pull request #1645 from bcgov/feature/reprocessSafeguards
reprocess safeguards
2 parents 18e5e11 + c785f59 commit 02c65e3

File tree

3 files changed

+60
-9
lines changed

3 files changed

+60
-9
lines changed

api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/repository/v1/SagaRepository.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,10 @@ public interface SagaRepository extends JpaRepository<SdcSagaEntity, UUID>, JpaS
4343
" WHERE saga.STATUS = 'COMPLETED' AND saga.SAGA_NAME = 'STUDENT_DATA_COLLECTION_STUDENT_MIGRATION_SAGA'", nativeQuery = true)
4444
void deleteCompletedMigrationSagas();
4545

46+
@Transactional
47+
@Modifying
48+
@Query(value = "DELETE FROM SDC_SAGA saga " +
49+
" WHERE saga.STATUS = 'COMPLETED' AND saga.SAGA_NAME = 'STUDENT_DATA_COLLECTION_STUDENT_PROCESSING_SAGA'", nativeQuery = true)
50+
void deleteCompletedStudentProcessingSagas();
51+
4652
}

api/src/main/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionService.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,13 +58,14 @@ public class SdcSchoolCollectionService {
5858
private final SdcDistrictCollectionRepository sdcDistrictCollectionRepository;
5959
private final SdcSchoolCollectionStudentValidationIssueRepository sdcSchoolCollectionStudentValidationIssueRepository;
6060
private final SdcSchoolCollectionLightRepository sdcSchoolCollectionLightRepository;
61+
private final SagaRepository sagaRepository;
6162

6263
private static final String INVALID_PAYLOAD_MSG = "Payload contains invalid data.";
6364
private static final String SDC_SCHOOL_COLLECTION_ID_KEY = "sdcSchoolCollectionID";
6465
private final DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyyMMdd");
6566

6667
@Autowired
67-
public SdcSchoolCollectionService(SdcSchoolCollectionRepository sdcSchoolCollectionRepository, SdcSchoolCollectionStudentRepository sdcSchoolCollectionStudentRepository, SdcSchoolCollectionHistoryService sdcSchoolCollectionHistoryService, SdcSchoolCollectionStudentHistoryRepository sdcSchoolCollectionStudentHistoryRepository, SdcDuplicateRepository sdcDuplicateRepository, SdcSchoolCollectionStudentStorageService sdcSchoolCollectionStudentStorageService, SdcSchoolCollectionStudentHistoryService sdcSchoolCollectionStudentHistoryService, CollectionRepository collectionRepository, SdcDistrictCollectionRepository sdcDistrictCollectionRepository, SdcDistrictCollectionService sdcDistrictCollectionService, SdcSchoolCollectionStudentValidationIssueRepository sdcSchoolCollectionStudentValidationIssueRepository, SdcSchoolCollectionLightRepository sdcSchoolCollectionLightRepository) {
68+
public SdcSchoolCollectionService(SdcSchoolCollectionRepository sdcSchoolCollectionRepository, SdcSchoolCollectionStudentRepository sdcSchoolCollectionStudentRepository, SdcSchoolCollectionHistoryService sdcSchoolCollectionHistoryService, SdcSchoolCollectionStudentHistoryRepository sdcSchoolCollectionStudentHistoryRepository, SdcDuplicateRepository sdcDuplicateRepository, SdcSchoolCollectionStudentStorageService sdcSchoolCollectionStudentStorageService, SdcSchoolCollectionStudentHistoryService sdcSchoolCollectionStudentHistoryService, CollectionRepository collectionRepository, SdcDistrictCollectionRepository sdcDistrictCollectionRepository, SdcDistrictCollectionService sdcDistrictCollectionService, SdcSchoolCollectionStudentValidationIssueRepository sdcSchoolCollectionStudentValidationIssueRepository, SdcSchoolCollectionLightRepository sdcSchoolCollectionLightRepository, SagaRepository sagaRepository) {
6869
this.sdcSchoolCollectionRepository = sdcSchoolCollectionRepository;
6970
this.sdcSchoolCollectionStudentRepository = sdcSchoolCollectionStudentRepository;
7071
this.sdcSchoolCollectionHistoryService = sdcSchoolCollectionHistoryService;
@@ -77,6 +78,7 @@ public SdcSchoolCollectionService(SdcSchoolCollectionRepository sdcSchoolCollect
7778
this.sdcDistrictCollectionService = sdcDistrictCollectionService;
7879
this.sdcSchoolCollectionStudentValidationIssueRepository = sdcSchoolCollectionStudentValidationIssueRepository;
7980
this.sdcSchoolCollectionLightRepository = sdcSchoolCollectionLightRepository;
81+
this.sagaRepository = sagaRepository;
8082
}
8183

8284
@Transactional(propagation = Propagation.MANDATORY)
@@ -253,6 +255,17 @@ public SdcSchoolCollectionEntity reprocessSchoolCollection(ReprocessSdcSchoolCol
253255
throw new InvalidPayloadException(error);
254256
}
255257

258+
if(sdcSchoolCollectionEntity.getSDCSchoolStudentEntities().stream().anyMatch(student -> StringUtils.equals(student.getSdcSchoolCollectionStudentStatusCode(), SdcSchoolStudentStatus.LOADED.getCode()))) {
259+
ApiError error = ApiError.builder().timestamp(LocalDateTime.now()).message(INVALID_PAYLOAD_MSG).status(BAD_REQUEST).build();
260+
var validationError = ValidationUtil.createFieldError(SDC_SCHOOL_COLLECTION_ID_KEY, reprocessData.getSdcSchoolCollectionID(), "Cannot reprocess SDC School Collection that has students in loaded status.");
261+
List<FieldError> fieldErrorList = new ArrayList<>();
262+
fieldErrorList.add(validationError);
263+
error.addValidationErrors(fieldErrorList);
264+
throw new InvalidPayloadException(error);
265+
}
266+
267+
sagaRepository.deleteCompletedStudentProcessingSagas();
268+
256269
sdcSchoolCollectionEntity.getSDCSchoolStudentEntities().forEach(sdcSchoolCollectionStudentEntity -> {
257270
if (!StringUtils.equals(sdcSchoolCollectionStudentEntity.getSdcSchoolCollectionStudentStatusCode(), SdcSchoolStudentStatus.DELETED.getCode())) {
258271
TransformUtil.clearCalculatedFields(sdcSchoolCollectionStudentEntity, true);

api/src/test/java/ca/bc/gov/educ/studentdatacollection/api/service/v1/SdcSchoolCollectionServiceTest.java

Lines changed: 40 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ class SdcSchoolCollectionServiceTest {
5454
@Mock
5555
SdcDuplicateRepository sdcDuplicateRepository;
5656

57+
@Mock
58+
SagaRepository sagaRepository;
59+
5760
@Mock
5861
SdcSchoolCollectionStudentStorageService sdcSchoolCollectionStudentStorageService;
5962

@@ -355,18 +358,14 @@ void testReprocessSchoolCollection_indySchool() {
355358
}
356359

357360
@Test
358-
void testReprocessSchoolCollection_withStudents_ShouldProcessNonDeletedStudents() {
361+
void testReprocessSchoolCollection_withStudents_notLoadedStudents_ShouldProcessNonDeletedStudents() {
359362
UUID sdcSchoolCollectionID = UUID.randomUUID();
360363
String updateUser = "TEST_USER";
361364

362365
SdcSchoolCollectionEntity sdcSchoolCollectionEntity = new SdcSchoolCollectionEntity();
363366
sdcSchoolCollectionEntity.setSdcSchoolCollectionID(sdcSchoolCollectionID);
364367
sdcSchoolCollectionEntity.setSdcSchoolCollectionStatusCode(SdcSchoolCollectionStatus.NEW.getCode());
365368

366-
SdcSchoolCollectionStudentEntity loadedStudent = new SdcSchoolCollectionStudentEntity();
367-
loadedStudent.setSdcSchoolCollectionStudentID(UUID.randomUUID());
368-
loadedStudent.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.LOADED.getCode());
369-
370369
SdcSchoolCollectionStudentEntity errorStudent = new SdcSchoolCollectionStudentEntity();
371370
errorStudent.setSdcSchoolCollectionStudentID(UUID.randomUUID());
372371
errorStudent.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.ERROR.getCode());
@@ -376,7 +375,6 @@ void testReprocessSchoolCollection_withStudents_ShouldProcessNonDeletedStudents(
376375
deletedStudent.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.DELETED.getCode());
377376

378377
Set<SdcSchoolCollectionStudentEntity> students = new HashSet<>();
379-
students.add(loadedStudent);
380378
students.add(errorStudent);
381379
students.add(deletedStudent);
382380
sdcSchoolCollectionEntity.setSdcSchoolStudentEntities(students);
@@ -390,9 +388,7 @@ void testReprocessSchoolCollection_withStudents_ShouldProcessNonDeletedStudents(
390388
.build()
391389
);
392390

393-
assertEquals(SdcSchoolStudentStatus.LOADED.getCode(), loadedStudent.getSdcSchoolCollectionStudentStatusCode());
394391
assertEquals(SdcSchoolStudentStatus.LOADED.getCode(), errorStudent.getSdcSchoolCollectionStudentStatusCode());
395-
assertEquals(updateUser, loadedStudent.getUpdateUser());
396392
assertEquals(updateUser, errorStudent.getUpdateUser());
397393

398394
assertEquals(SdcSchoolStudentStatus.DELETED.getCode(), deletedStudent.getSdcSchoolCollectionStudentStatusCode());
@@ -401,6 +397,42 @@ void testReprocessSchoolCollection_withStudents_ShouldProcessNonDeletedStudents(
401397
verify(sdcSchoolCollectionStudentStorageService, times(1)).saveAllSDCStudentsWithHistory(any());
402398
}
403399

400+
@Test
401+
void testReprocessSchoolCollection_withLoadedStudents_ShouldThrowLoadedStudentException() {
402+
UUID sdcSchoolCollectionID = UUID.randomUUID();
403+
404+
SdcSchoolCollectionEntity sdcSchoolCollectionEntity = new SdcSchoolCollectionEntity();
405+
sdcSchoolCollectionEntity.setSdcSchoolCollectionID(sdcSchoolCollectionID);
406+
sdcSchoolCollectionEntity.setSdcSchoolCollectionStatusCode(SdcSchoolCollectionStatus.NEW.getCode());
407+
408+
SdcSchoolCollectionStudentEntity loadedStudent = new SdcSchoolCollectionStudentEntity();
409+
loadedStudent.setSdcSchoolCollectionStudentID(UUID.randomUUID());
410+
loadedStudent.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.LOADED.getCode());
411+
412+
SdcSchoolCollectionStudentEntity errorStudent = new SdcSchoolCollectionStudentEntity();
413+
errorStudent.setSdcSchoolCollectionStudentID(UUID.randomUUID());
414+
errorStudent.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.ERROR.getCode());
415+
416+
SdcSchoolCollectionStudentEntity deletedStudent = new SdcSchoolCollectionStudentEntity();
417+
deletedStudent.setSdcSchoolCollectionStudentID(UUID.randomUUID());
418+
deletedStudent.setSdcSchoolCollectionStudentStatusCode(SdcSchoolStudentStatus.DELETED.getCode());
419+
420+
Set<SdcSchoolCollectionStudentEntity> students = new HashSet<>();
421+
students.add(loadedStudent);
422+
students.add(errorStudent);
423+
students.add(deletedStudent);
424+
sdcSchoolCollectionEntity.setSdcSchoolStudentEntities(students);
425+
426+
when(sdcSchoolCollectionRepository.findById(sdcSchoolCollectionID)).thenReturn(Optional.of(sdcSchoolCollectionEntity));
427+
428+
ReprocessSdcSchoolCollection sdcSchoolCollectionReprocess = ReprocessSdcSchoolCollection.builder()
429+
.sdcSchoolCollectionID(sdcSchoolCollectionID)
430+
.updateUser("USER")
431+
.build();
432+
433+
assertThrows(InvalidPayloadException.class, () -> sdcSchoolCollectionService.reprocessSchoolCollection(sdcSchoolCollectionReprocess));
434+
}
435+
404436
@Test
405437
void testReprocessSchoolCollection_withOnlyDeletedStudents_ShouldNotProcessAnyStudents() {
406438
UUID sdcSchoolCollectionID = UUID.randomUUID();

0 commit comments

Comments
 (0)