diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index a30e94f24183674a612d2282ae01aee797f923f5..fbaa1ea74dd8b7e71e8e4d809efafd696a32f5f6 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -465,7 +465,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } public Optional deleteProfile(UUID profileId) { - CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); + Transaction sagaTransaction = new SagaTransaction(cassandraTemplate); Profile existingProfile = profileRepository.findByProfileId(profileId); if (existingProfile == null) { logger.error("Error deleting profile. Existing profile with the provided id {} was not found.", profileId); @@ -479,22 +479,32 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return Optional.of(Cause.ERROR_PERMISSION_DENIED); } - WriteResult wr = null; + WriteResult wr; try { - deleteProfileAccountsWhenDeletingProfile(batchOperations, existingAccountsForProfile); + deleteProfileAccountsWhenDeletingProfile(sagaTransaction, existingAccountsForProfile); if (existingProfile.getAuthenticationProviders() != null && existingProfile.getAuthenticationProviders().size() > 0) { - deleteAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), + deleteAuthenticationProvidersFromProfile(sagaTransaction, existingProfile.getProfileId(), existingProfile.getAuthenticationProviders()); } - deleteProfileData(batchOperations, existingProfile); - wr = batchOperations.execute(); + deleteProfileData(sagaTransaction, existingProfile); + wr = sagaTransaction.execute(); + if (!erlangAccountBridge.deleteProfile(existingProfile.getProfileId(), existingAccountsForProfile.stream() + .map(AccountByProfileId::getAccountId).collect(Collectors.toList()))) { + logger.error("Internal error with erlang"); + sagaTransaction.rollBack(); + return Optional.of(Cause.ERROR_DELETING_PROFILE); + } + } catch (StatusRuntimeException e) { + logger.info("Error while lose connection with bridge"); + sagaTransaction.rollBack(); + return Optional.of(Cause.ERROR_DELETING_PROFILE); } catch (IllegalArgumentException | IllegalStateException e) { logger.info("Exception while deleting account."); logger.debug("Exception while deleting account: {} ...", e.getMessage()); return Optional.of(Cause.ERROR_DELETING_PROFILE); } - if (wr != null && wr.wasApplied()) { + if (wr.wasApplied()) { return Optional.empty(); } return Optional.of(Cause.ERROR_DELETING_PROFILE);