use of ca.bc.gov.educ.penreg.api.mappers.v1.ArchiveAndReturnSagaResponseMapper in project EDUC-PEN-REG-BATCH-API by bcgov.
the class PenRequestBatchSagaController method processBatchRequest.
private ResponseEntity<List<ArchiveAndReturnSagaResponse>> processBatchRequest(final SagaEnum sagaName, final PenRequestBatchArchiveAndReturnAllSagaData penRequestBatchArchiveAndReturnAllSagaData) {
final var penRequestBatchIDs = penRequestBatchArchiveAndReturnAllSagaData.getPenRequestBatchArchiveAndReturnSagaData().stream().map(PenRequestBatchArchiveAndReturnSagaData::getPenRequestBatchID).collect(Collectors.toList());
final var sagaInProgress = !this.getSagaService().findAllByPenRequestBatchIDInAndStatusIn(penRequestBatchIDs, this.getStatusesFilter()).isEmpty();
if (sagaInProgress) {
return ResponseEntity.status(HttpStatus.CONFLICT).build();
}
try {
final var updateUser = penRequestBatchArchiveAndReturnAllSagaData.getUpdateUser();
final var payloads = penRequestBatchArchiveAndReturnAllSagaData.getPenRequestBatchArchiveAndReturnSagaData().stream().map(sagaData -> {
sagaData.setUpdateUser(updateUser);
try {
val payload = JsonUtil.getJsonStringFromObject(sagaData);
return Pair.of(sagaData.getPenRequestBatchID(), payload);
} catch (final JsonProcessingException e) {
throw new InvalidParameterException(e.getMessage());
}
}).collect(Collectors.toList());
final var sagas = this.getOrchestratorMap().get(sagaName.toString()).saveMultipleSagas(payloads, penRequestBatchArchiveAndReturnAllSagaData.getCreateUser());
for (val saga : sagas) {
this.getOrchestratorMap().get(sagaName.toString()).startSaga(saga);
}
return ResponseEntity.ok(sagas.stream().map(archiveAndReturnSagaResponseMapper::toStruct).collect(Collectors.toList()));
} catch (final Exception e) {
throw new SagaRuntimeException(e.getMessage());
}
}
Aggregations