diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index bcfdf0460259a6fbae48a2c0c36a920750fa2a45..77a674543332f038036dca81517365834ea52450 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -226,18 +226,23 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio WriteResult wr = null; try { deleteAccountData(batchOperations, existingAccount); - deleteProfileByAuthenticationProvider(batchOperations, existingAccount.getProfileId(), - AuthenticationProvider.createAuthenticationProviderFromStringsWithDefaultSearchableOption( - existingAccount.getAuthenticationProviderType(), - existingAccount.getAuthenticationProvider())); if (alsoDeleteProfile) { - if (existingProfile.getAuthenticationProviders() != null) { + if (existingProfile.getAuthenticationProviders() != null + && existingProfile.getAuthenticationProviders().size() > 0) { deleteAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), existingProfile.getAuthenticationProviders()); } deleteProfileData(batchOperations, existingProfile); } else { + if (existingAccount.getAuthenticationProviderType() != null + && existingAccount.getAuthenticationProvider() != null) { + deleteProfileByAuthenticationProvider(batchOperations, existingAccount.getProfileId(), + AuthenticationProvider.createAuthenticationProviderFromStringsWithDefaultSearchableOption( + existingAccount.getAuthenticationProviderType(), + existingAccount.getAuthenticationProvider())); + } if (!removeCreationProvider(batchOperations, existingAccount, existingProfile)) { + logger.error("Error deleting account {}", existingAccount.getAccountId()); return false; } } @@ -266,14 +271,22 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio // update authentication providers of the profile by removing the one of the deleted account (if not // already manually removed by the user) Long timeUpdated = Instant.now().toEpochMilli(); - if (existingProfile.getAuthenticationProviders() != null) { + if (existingProfile.getAuthenticationProviders() != null + && existingProfile.getAuthenticationProviders().size() > 0) { Set existingAuthenticationProvidersSet = new HashSet( existingProfile.getAuthenticationProviders()); + if (existingAccount.getAuthenticationProviderType() == null + && existingAccount.getAuthenticationProvider() == null) { + // The creation provider is already deleted + return true; + } Optional existingCreationProviderToDelete = AuthenticationProvider .foundExistingAuthenticationProviderByTypeAndValue(existingAccount.getAuthenticationProviderType(), existingAccount.getAuthenticationProvider(), existingAuthenticationProvidersSet); if (!existingCreationProviderToDelete.isPresent()) { - logger.error("Error removing creation provider from account {}.", existingAccount.getAccountId()); + logger.error( + "The creation provider for account {} was not found in the list of the profile's authentication providers.", + existingAccount.getAccountId()); return false; } @@ -284,6 +297,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio if (existingAuthenticationProvidersSet.size() > 0) { updateAuthProvidersInProfileWhenDeletingAccount(batchOperations, existingProfile, existingAuthenticationProvidersSet, timeUpdated); + return true; } else { logger.error( "Error deleting account. At least one authentication provider should exist in profile: {}.", @@ -291,8 +305,11 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } } + logger.error("Error removing creation provider from account {}.", existingAccount.getAccountId()); + return false; } - return true; + logger.error("No authentication providers found for profile {}", existingProfile.getProfileId()); + return false; } private void updateAccountData(CassandraBatchOperations batchOps, UpdateAccountRequest request, @@ -416,7 +433,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio WriteResult wr = null; try { deleteProfileAccountsWhenDeletingProfile(profileId, batchOperations, existingAccountsForProfile); - if (existingProfile.getAuthenticationProviders() != null) { + if (existingProfile.getAuthenticationProviders() != null + && existingProfile.getAuthenticationProviders().size() > 0) { deleteAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), existingProfile.getAuthenticationProviders()); } @@ -438,10 +456,6 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio for (AccountByProfileId accountByProfileId : existingAccountsForProfile) { Account existingAccount = accountRepository.findByAccountId(accountByProfileId.getAccountId()); deleteAccountData(batchOperations, existingAccount); - deleteProfileByAuthenticationProvider(batchOperations, profileId, - AuthenticationProvider.createAuthenticationProviderFromStringsWithDefaultSearchableOption( - existingAccount.getAuthenticationProviderType(), - existingAccount.getAuthenticationProvider())); } }