Skip to content

Commit 60d260d

Browse files
committed
reprocess safeguards
1 parent 18e5e11 commit 60d260d

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
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);

0 commit comments

Comments
 (0)