From 223e649e889c292e4e7dcda0c8f35317500294db Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Tue, 18 Sep 2018 16:53:33 +0300 Subject: [PATCH 1/4] NY-3568: Endpoint for Delete Account - added implementation for deleting account; - renamed some existing functions: use better names for existing functions invoked when deleting account's data; Signed-off-by: Stanimir Penkov --- .../AccountRepositoryAdditional.java | 2 + .../AccountRepositoryAdditionalImpl.java | 155 +++++++++++++++--- .../account/services/AccountServiceImpl.java | 28 ++++ 3 files changed, 162 insertions(+), 23 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java index 8ab93a2..941d786 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java @@ -24,6 +24,8 @@ public interface AccountRepositoryAdditional { Profile updateProfile(UpdateProfileRequest request); + boolean deleteAccount(UUID accountId); + boolean foundExistingNotOwnUsername(UUID accountId, String username); PendingAccountByAuthenticationProvider findSameAuthenticationProviderInPendingAccount(AuthenticationProvider authProvider); diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index c1b1f0e..c190367 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -25,6 +25,7 @@ import biz.nynja.account.grpc.UpdateProfileRequest; import biz.nynja.account.models.Account; import biz.nynja.account.models.AccountByAuthenticationProvider; import biz.nynja.account.models.AccountByCommunicationProvider; +import biz.nynja.account.models.AccountByProfileId; import biz.nynja.account.models.AccountByUsername; import biz.nynja.account.models.AuthenticationProvider; import biz.nynja.account.models.PendingAccount; @@ -55,6 +56,9 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Autowired AccountByAuthenticationProviderRepository accountByAuthenticationProviderRepository; + @Autowired + AccountByProfileIdRepository accountByProfileIdRepository; + @Autowired ProfileByAuthenticationProviderRepository profileByAuthenticationProviderRepository; @@ -198,7 +202,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio updateProfileData(batchOperations, request, existingProfile, timeUpdated); insertNewAuthenticationProvidersToProfile(batchOperations, existingProfile.getProfileId(), newAuthenticationProvidersSet); - deleteOldAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), + deleteAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), oldAuthenticationProvidersSet); wr = batchOperations.execute(); } catch (IllegalArgumentException | IllegalStateException e) { @@ -261,7 +265,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio try { updateAccountData(batchOperations, request, existingAccount, timeUpdated); insertNewCommunicationProvidersToAccount(batchOperations, request, newCommunicationProvidersSet); - deleteOldCommunicationProvidersFromAccount(batchOperations, existingAccount, oldCommunicationProvidersSet); + deleteCommunicationProvidersFromAccount(batchOperations, existingAccount, oldCommunicationProvidersSet); updateSameCommunicationProvidersFromAccount(batchOperations, request, sameCommunicationProvidersSet); wr = batchOperations.execute(); } catch (IllegalArgumentException | IllegalStateException e) { @@ -280,6 +284,92 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return null; } + public boolean deleteAccount(UUID accountId) { + CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); + Account existingAccount = accountRepository.findByAccountId(accountId); + if (existingAccount == null) { + logger.info("Error deleting account. Existing account with the provided id was not found."); + logger.debug("Error deleting account. Existing account with the provided id {} was not found.", accountId); + return false; + } + Profile existingProfile = profileRepository.findByProfileId(existingAccount.getProfileId()); + if (existingProfile == null) { + logger.info("Error deleting account. Corresponding profile was not found."); + logger.debug("Error deleting account. Corresponding profile with the provided id {} was not found.", + existingAccount.getProfileId()); + return false; + } + List existingAccountsForProfile = accountByProfileIdRepository + .findAllByProfileId(existingAccount.getProfileId()); + if (existingAccountsForProfile == null) { + logger.info("Error deleting account. Existing accounts for the given profile were not found."); + logger.debug("Error deleting account. Existing accounts for the given profile id {} were not found.", + existingAccount.getProfileId()); + return false; + } + boolean alsoDeleteProfile = false; + // check for last account in the profile + if (existingAccountsForProfile.size() == 1) { + alsoDeleteProfile = true; + } + + Set existingCommunicationProvidersSet = new HashSet(); + if (existingAccount.getCommunicationProviders() != null) { + existingCommunicationProvidersSet = new HashSet( + existingAccount.getCommunicationProviders()); + } + WriteResult wr = null; + try { + deleteAccountData(batchOperations, existingAccount); + deleteCommunicationProvidersFromAccount(batchOperations, existingAccount, + existingCommunicationProvidersSet); + deleteProfileByAuthenticationProvider(batchOperations, existingAccount.getProfileId(), + AuthenticationProvider.createAuthenticationProviderFromStrings( + existingAccount.getAuthenticationProviderType(), + existingAccount.getAuthenticationProvider())); + if (alsoDeleteProfile) { + deleteProfileData(batchOperations, existingProfile); + } else { + // update authentication providers of the profile by removing the one of the deleted account (if not + // already manually removed by the user) + Long timeUpdated = new Date().getTime(); + if (existingProfile.getAuthenticationProviders() != null) { + Set existingAuthenticationProvidersSet = new HashSet( + existingProfile.getAuthenticationProviders()); + boolean removedAuthProvider = existingAuthenticationProvidersSet.remove(AuthenticationProvider + .createAuthenticationProviderFromStrings(existingAccount.getAuthenticationProviderType(), + existingAccount.getAuthenticationProvider())); + if (removedAuthProvider) { + // at least one authentication provider should exist in the profile + if (existingAuthenticationProvidersSet.size() > 0) { + updateAuthProvidersInProfileWhenDeletingAccount(batchOperations, existingProfile, + existingAuthenticationProvidersSet, timeUpdated); + } else { + logger.info( + "Error deleting account. At least one authentication provider should exist in profile."); + logger.debug( + "Error deleting account. At least one authentication provider should exist in profile: {}.", + existingAccount.getProfileId()); + return false; + } + } + } + } + wr = batchOperations.execute(); + } catch (IllegalArgumentException | IllegalStateException e) { + logger.info("Exception while deleting account."); + logger.debug("Exception while deleting account: {} ...", e.getMessage()); + return false; + } + if (wr != null) { + boolean applied = wr.wasApplied(); + if (applied) { + return true; + } + } + return false; + } + private void updateAccountData(CassandraBatchOperations batchOps, UpdateAccountRequest request, Account existingAccount, Long lastUpdateTimestamp) { Account updatedAccount = existingAccount; @@ -328,6 +418,25 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio batchOps.update(updatedProfile); } + private void updateAuthProvidersInProfileWhenDeletingAccount(CassandraBatchOperations batchOps, Profile existingProfile, Set authProvidersToUpdate, Long lastUpdateTimestamp) { + Profile updatedProfile = existingProfile; + if (authProvidersToUpdate != null) { + updatedProfile.setAuthenticationProviders(authProvidersToUpdate); + updatedProfile.setLastUpdateTimestamp(lastUpdateTimestamp); + batchOps.update(updatedProfile); + } + } + + private void deleteAccountData(CassandraBatchOperations batchOps, Account existingAccountToDelete) { + Account accountToDelete = existingAccountToDelete; + batchOps.delete(accountToDelete); + } + + private void deleteProfileData(CassandraBatchOperations batchOps, Profile existingProfileToDelete) { + Profile profileToDelete = existingProfileToDelete; + batchOps.delete(profileToDelete); + } + private void insertNewCommunicationProvidersToAccount(CassandraBatchOperations batchOps, UpdateAccountRequest request, Set newCommunicationProvidersSet) { for (AuthenticationProvider commProvider : newCommunicationProvidersSet) { @@ -335,10 +444,10 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } } - private void deleteOldCommunicationProvidersFromAccount(CassandraBatchOperations batchOps, Account existingAccount, - Set oldCommunicationProvidersSet) { - for (AuthenticationProvider commProvider : oldCommunicationProvidersSet) { - deleteOldAccountByCommunicationProvider(batchOps, existingAccount, commProvider); + private void deleteCommunicationProvidersFromAccount(CassandraBatchOperations batchOps, Account existingAccount, + Set communicationProvidersSetToDelete) { + for (AuthenticationProvider commProvider : communicationProvidersSetToDelete) { + deleteAccountByCommunicationProvider(batchOps, existingAccount, commProvider); } } @@ -365,20 +474,20 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio batchOps.insert(newProfileByAuthenticationProvider); } - private void deleteOldAuthenticationProvidersFromProfile(CassandraBatchOperations batchOps, UUID profileId, + private void deleteAuthenticationProvidersFromProfile(CassandraBatchOperations batchOps, UUID profileId, Set oldAuthenticationProvidersSet) { for (AuthenticationProvider authProvider : oldAuthenticationProvidersSet) { - deleteOldProfileByAuthenticationProvider(batchOps, profileId, authProvider); + deleteProfileByAuthenticationProvider(batchOps, profileId, authProvider); } } - private void deleteOldProfileByAuthenticationProvider(CassandraBatchOperations batchOps, UUID profileId, + private void deleteProfileByAuthenticationProvider(CassandraBatchOperations batchOps, UUID profileId, AuthenticationProvider authProvider) { - ProfileByAuthenticationProvider oldProfileByAuthenticationProvider = new ProfileByAuthenticationProvider(); - oldProfileByAuthenticationProvider.setAuthenticationProvider(authProvider.getValue()); - oldProfileByAuthenticationProvider.setAuthenticationProviderType(authProvider.getType()); - oldProfileByAuthenticationProvider.setProfileId(profileId); - batchOps.delete(oldProfileByAuthenticationProvider); + ProfileByAuthenticationProvider profileByAuthenticationProviderToDelete = new ProfileByAuthenticationProvider(); + profileByAuthenticationProviderToDelete.setAuthenticationProvider(authProvider.getValue()); + profileByAuthenticationProviderToDelete.setAuthenticationProviderType(authProvider.getType()); + profileByAuthenticationProviderToDelete.setProfileId(profileId); + batchOps.delete(profileByAuthenticationProviderToDelete); } private void insertNewAccountByCommunicationProvider(CassandraBatchOperations batchOps, @@ -393,16 +502,16 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio batchOps.insert(newAccountByCommunicationProvider); } - private void deleteOldAccountByCommunicationProvider(CassandraBatchOperations batchOps, Account existingAccount, + private void deleteAccountByCommunicationProvider(CassandraBatchOperations batchOps, Account existingAccount, AuthenticationProvider authProvider) { - AccountByCommunicationProvider oldAccountByCommunicationProvider = new AccountByCommunicationProvider(); - oldAccountByCommunicationProvider.setCommunicationProvider(authProvider.getValue()); - oldAccountByCommunicationProvider.setCommunicationProviderType(authProvider.getType()); - oldAccountByCommunicationProvider.setAccountId(existingAccount.getAccountId()); - oldAccountByCommunicationProvider.setAccountName(existingAccount.getAccountName()); - oldAccountByCommunicationProvider.setFirstName(existingAccount.getFirstName()); - oldAccountByCommunicationProvider.setAvatar(existingAccount.getAvatar()); - batchOps.delete(oldAccountByCommunicationProvider); + AccountByCommunicationProvider accountByCommunicationProviderToDelete = new AccountByCommunicationProvider(); + accountByCommunicationProviderToDelete.setCommunicationProvider(authProvider.getValue()); + accountByCommunicationProviderToDelete.setCommunicationProviderType(authProvider.getType()); + accountByCommunicationProviderToDelete.setAccountId(existingAccount.getAccountId()); + accountByCommunicationProviderToDelete.setAccountName(existingAccount.getAccountName()); + accountByCommunicationProviderToDelete.setFirstName(existingAccount.getFirstName()); + accountByCommunicationProviderToDelete.setAvatar(existingAccount.getAvatar()); + batchOps.delete(accountByCommunicationProviderToDelete); } private void updateSameAccountByCommunicationProvider(CassandraBatchOperations batchOps, diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index 7aede54..293b8b5 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -26,6 +26,8 @@ import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreatePendingAccountRequest; import biz.nynja.account.grpc.CreatePendingAccountResponse; +import biz.nynja.account.grpc.DeleteAccountRequest; +import biz.nynja.account.grpc.DeletionResponse; import biz.nynja.account.grpc.ErrorResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.models.Account; @@ -413,4 +415,30 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas logger.info("Account updated successfully."); return; } + + @Override + public void deleteAccount(DeleteAccountRequest request, StreamObserver responseObserver) { + logger.info("Deleting account..."); + logger.debug("Deleting account...: {}", request); + + if ((request.getAccountId() == null) || (request.getAccountId().isEmpty())) { + responseObserver.onNext(DeletionResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_ACCOUNT_ID)).build()); + responseObserver.onCompleted(); + return; + } + + boolean wasAccountDeleted = accountRepositoryAdditional.deleteAccount(UUID.fromString(request.getAccountId())); + if (wasAccountDeleted) { + responseObserver.onNext(DeletionResponse.newBuilder() + .setDeletionDetails("The account was successfully deleted.").build()); + responseObserver.onCompleted(); + return; + } + + responseObserver.onNext(DeletionResponse.newBuilder() + .setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_DELETING_ACCOUNT)).build()); + responseObserver.onCompleted(); + return; + } } \ No newline at end of file -- GitLab From 3a253c7a307e8d61e536d20033e019339fa3c7a8 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Wed, 19 Sep 2018 14:15:49 +0300 Subject: [PATCH 2/4] NY-3568: Endpoint for Delete Account - also delete records from the table for the profile's auth providers; - use better name for the parameter for auth providers. Signed-off-by: Stanimir Penkov --- .../repositories/AccountRepositoryAdditionalImpl.java | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index c190367..6b95063 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -328,6 +328,10 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio existingAccount.getAuthenticationProviderType(), existingAccount.getAuthenticationProvider())); if (alsoDeleteProfile) { + if (existingProfile.getAuthenticationProviders() != null) { + deleteAuthenticationProvidersFromProfile(batchOperations, existingProfile.getProfileId(), + existingProfile.getAuthenticationProviders()); + } deleteProfileData(batchOperations, existingProfile); } else { // update authentication providers of the profile by removing the one of the deleted account (if not @@ -475,8 +479,8 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } private void deleteAuthenticationProvidersFromProfile(CassandraBatchOperations batchOps, UUID profileId, - Set oldAuthenticationProvidersSet) { - for (AuthenticationProvider authProvider : oldAuthenticationProvidersSet) { + Set authenticationProvidersSetToDelete) { + for (AuthenticationProvider authProvider : authenticationProvidersSetToDelete) { deleteProfileByAuthenticationProvider(batchOps, profileId, authProvider); } } -- GitLab From 2a7f2a4d5ccfb3f50f2ea9ff37cc03d9a7dfeaa2 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Wed, 19 Sep 2018 16:37:15 +0300 Subject: [PATCH 3/4] NY-3568: Code Review Feedback Signed-off-by: Stanimir Penkov --- .../AccountRepositoryAdditionalImpl.java | 83 +++++++++++-------- 1 file changed, 47 insertions(+), 36 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 6b95063..5b0e674 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -287,23 +287,14 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio public boolean deleteAccount(UUID accountId) { CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); Account existingAccount = accountRepository.findByAccountId(accountId); - if (existingAccount == null) { - logger.info("Error deleting account. Existing account with the provided id was not found."); - logger.debug("Error deleting account. Existing account with the provided id {} was not found.", accountId); + if (!existingAccountAndProfileToDelete(accountId)) { return false; } Profile existingProfile = profileRepository.findByProfileId(existingAccount.getProfileId()); - if (existingProfile == null) { - logger.info("Error deleting account. Corresponding profile was not found."); - logger.debug("Error deleting account. Corresponding profile with the provided id {} was not found.", - existingAccount.getProfileId()); - return false; - } List existingAccountsForProfile = accountByProfileIdRepository .findAllByProfileId(existingAccount.getProfileId()); if (existingAccountsForProfile == null) { - logger.info("Error deleting account. Existing accounts for the given profile were not found."); - logger.debug("Error deleting account. Existing accounts for the given profile id {} were not found.", + logger.error("Error deleting account. Existing accounts for the given profile id {} were not found.", existingAccount.getProfileId()); return false; } @@ -334,35 +325,13 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } deleteProfileData(batchOperations, existingProfile); } else { - // update authentication providers of the profile by removing the one of the deleted account (if not - // already manually removed by the user) - Long timeUpdated = new Date().getTime(); - if (existingProfile.getAuthenticationProviders() != null) { - Set existingAuthenticationProvidersSet = new HashSet( - existingProfile.getAuthenticationProviders()); - boolean removedAuthProvider = existingAuthenticationProvidersSet.remove(AuthenticationProvider - .createAuthenticationProviderFromStrings(existingAccount.getAuthenticationProviderType(), - existingAccount.getAuthenticationProvider())); - if (removedAuthProvider) { - // at least one authentication provider should exist in the profile - if (existingAuthenticationProvidersSet.size() > 0) { - updateAuthProvidersInProfileWhenDeletingAccount(batchOperations, existingProfile, - existingAuthenticationProvidersSet, timeUpdated); - } else { - logger.info( - "Error deleting account. At least one authentication provider should exist in profile."); - logger.debug( - "Error deleting account. At least one authentication provider should exist in profile: {}.", - existingAccount.getProfileId()); - return false; - } - } + if (!removeDeletedAccountCreationProviderFromProfile(batchOperations, existingAccount, existingProfile)) { + return false; } } wr = batchOperations.execute(); } catch (IllegalArgumentException | IllegalStateException e) { - logger.info("Exception while deleting account."); - logger.debug("Exception while deleting account: {} ...", e.getMessage()); + logger.error("Exception while deleting account: {}.", e.getMessage()); return false; } if (wr != null) { @@ -374,6 +343,33 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } + private boolean removeDeletedAccountCreationProviderFromProfile(CassandraBatchOperations batchOperations, Account existingAccount, + Profile existingProfile) { + // update authentication providers of the profile by removing the one of the deleted account (if not + // already manually removed by the user) + Long timeUpdated = new Date().getTime(); + if (existingProfile.getAuthenticationProviders() != null) { + Set existingAuthenticationProvidersSet = new HashSet( + existingProfile.getAuthenticationProviders()); + boolean removedAuthProvider = existingAuthenticationProvidersSet.remove(AuthenticationProvider + .createAuthenticationProviderFromStrings(existingAccount.getAuthenticationProviderType(), + existingAccount.getAuthenticationProvider())); + if (removedAuthProvider) { + // at least one authentication provider should exist in the profile + if (existingAuthenticationProvidersSet.size() > 0) { + updateAuthProvidersInProfileWhenDeletingAccount(batchOperations, existingProfile, + existingAuthenticationProvidersSet, timeUpdated); + } else { + logger.error( + "Error deleting account. At least one authentication provider should exist in profile: {}.", + existingAccount.getProfileId()); + return false; + } + } + } + return true; + } + private void updateAccountData(CassandraBatchOperations batchOps, UpdateAccountRequest request, Account existingAccount, Long lastUpdateTimestamp) { Account updatedAccount = existingAccount; @@ -431,6 +427,21 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } } + private boolean existingAccountAndProfileToDelete(UUID accountId) { + Account existingAccount = accountRepository.findByAccountId(accountId); + if (existingAccount == null) { + logger.error("Error deleting account. Existing account with the provided id {} was not found.", accountId); + return false; + } + Profile existingProfile = profileRepository.findByProfileId(existingAccount.getProfileId()); + if (existingProfile == null) { + logger.error("Error deleting account. Corresponding profile with the provided id {} was not found.", + existingAccount.getProfileId()); + return false; + } + return true; + } + private void deleteAccountData(CassandraBatchOperations batchOps, Account existingAccountToDelete) { Account accountToDelete = existingAccountToDelete; batchOps.delete(accountToDelete); -- GitLab From 20d0609a1bacc08c857b3c406575ba8cf261ebc6 Mon Sep 17 00:00:00 2001 From: Stanimir Penkov Date: Thu, 20 Sep 2018 16:17:10 +0300 Subject: [PATCH 4/4] NY-3568: Code Review Feedback Signed-off-by: Stanimir Penkov --- .../AccountRepositoryAdditionalImpl.java | 24 ++++++++++++------- .../account/services/AccountServiceImpl.java | 13 +++++----- 2 files changed, 21 insertions(+), 16 deletions(-) diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 5b0e674..17e0556 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -287,7 +287,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio public boolean deleteAccount(UUID accountId) { CassandraBatchOperations batchOperations = cassandraTemplate.batchOps(); Account existingAccount = accountRepository.findByAccountId(accountId); - if (!existingAccountAndProfileToDelete(accountId)) { + if (!doExistAccountAndProfileToDelete(accountId)) { return false; } Profile existingProfile = profileRepository.findByProfileId(existingAccount.getProfileId()); @@ -325,7 +325,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } deleteProfileData(batchOperations, existingProfile); } else { - if (!removeDeletedAccountCreationProviderFromProfile(batchOperations, existingAccount, existingProfile)) { + if (!removeCreationProvider(batchOperations, existingAccount, existingProfile)) { return false; } } @@ -334,16 +334,22 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio logger.error("Exception while deleting account: {}.", e.getMessage()); return false; } - if (wr != null) { - boolean applied = wr.wasApplied(); - if (applied) { - return true; - } + if (wr != null && wr.wasApplied()) { + return true; } return false; } - private boolean removeDeletedAccountCreationProviderFromProfile(CassandraBatchOperations batchOperations, Account existingAccount, + /** + * The method removes the deleted account's creation provider from the list of auth providers of the profile (if not + * already manually removed by the user). + * + * @param batchOperations + * @param existingAccount + * @param existingProfile + * @return true or false according to the result of removing the account's creation provider + */ + private boolean removeCreationProvider(CassandraBatchOperations batchOperations, Account existingAccount, Profile existingProfile) { // update authentication providers of the profile by removing the one of the deleted account (if not // already manually removed by the user) @@ -427,7 +433,7 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } } - private boolean existingAccountAndProfileToDelete(UUID accountId) { + private boolean doExistAccountAndProfileToDelete(UUID accountId) { Account existingAccount = accountRepository.findByAccountId(accountId); if (existingAccount == null) { logger.error("Error deleting account. Existing account with the provided id {} was not found.", accountId); diff --git a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java index 293b8b5..c3cb47e 100644 --- a/src/main/java/biz/nynja/account/services/AccountServiceImpl.java +++ b/src/main/java/biz/nynja/account/services/AccountServiceImpl.java @@ -27,7 +27,7 @@ import biz.nynja.account.grpc.CreateAccountRequest; import biz.nynja.account.grpc.CreatePendingAccountRequest; import biz.nynja.account.grpc.CreatePendingAccountResponse; import biz.nynja.account.grpc.DeleteAccountRequest; -import biz.nynja.account.grpc.DeletionResponse; +import biz.nynja.account.grpc.StatusResponse; import biz.nynja.account.grpc.ErrorResponse; import biz.nynja.account.grpc.ErrorResponse.Cause; import biz.nynja.account.models.Account; @@ -417,12 +417,11 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas } @Override - public void deleteAccount(DeleteAccountRequest request, StreamObserver responseObserver) { - logger.info("Deleting account..."); + public void deleteAccount(DeleteAccountRequest request, StreamObserver responseObserver) { logger.debug("Deleting account...: {}", request); if ((request.getAccountId() == null) || (request.getAccountId().isEmpty())) { - responseObserver.onNext(DeletionResponse.newBuilder() + responseObserver.onNext(StatusResponse.newBuilder() .setError(ErrorResponse.newBuilder().setCause(Cause.MISSING_ACCOUNT_ID)).build()); responseObserver.onCompleted(); return; @@ -430,13 +429,13 @@ public class AccountServiceImpl extends AccountServiceGrpc.AccountServiceImplBas boolean wasAccountDeleted = accountRepositoryAdditional.deleteAccount(UUID.fromString(request.getAccountId())); if (wasAccountDeleted) { - responseObserver.onNext(DeletionResponse.newBuilder() - .setDeletionDetails("The account was successfully deleted.").build()); + responseObserver.onNext(StatusResponse.newBuilder() + .setStatus("SUCCESS").build()); responseObserver.onCompleted(); return; } - responseObserver.onNext(DeletionResponse.newBuilder() + responseObserver.onNext(StatusResponse.newBuilder() .setError(ErrorResponse.newBuilder().setCause(Cause.ERROR_DELETING_ACCOUNT)).build()); responseObserver.onCompleted(); return; -- GitLab