diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 265d52f32610bf2e2ddf2c7c8bbf9ab2132c536c..5820442e6e9d68f6ce5a70b88a10a352118932c3 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -32,6 +32,7 @@ import biz.nynja.account.components.StatementsPool; import biz.nynja.account.components.Validator; import biz.nynja.account.grpc.AccessStatus; import biz.nynja.account.grpc.CompletePendingAccountCreationRequest; +import biz.nynja.account.grpc.ContactType; import biz.nynja.account.grpc.UpdateAccountRequest; import biz.nynja.account.models.Account; import biz.nynja.account.models.AccountByAuthenticationProvider; @@ -564,6 +565,10 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Override public boolean addContactInfo(UUID accountId, ContactInfo contactInfo) { + if (contactInfo == null) { + logger.error("Error adding contact info to account {}. contactInfo is not set.", accountId); + return false; + } Account accountToUpdate = accountRepository.findByAccountId(accountId); if (accountToUpdate == null) { logger.error("Existing account with the provided id {} was not found.", accountId); @@ -573,10 +578,16 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio if (contactsInfoSet == null) { contactsInfoSet = new HashSet(); } - if (contactInfo != null) { + + if (foundExistingContactInfo(contactInfo, contactsInfoSet)) { + logger.error("Error adding contact info to account {}. {}:{} already exists.", accountId, + contactInfo.getType(), contactInfo.getValue()); + return false; + } else { + setEmptyLabelExceptPhone(contactInfo); if (!contactsInfoSet.add(contactInfo)) { - logger.error("Error adding contact info to account {}. {} already exists.", accountId, - contactInfo.toString()); + logger.error("Error adding contact info {}:{} to account {}.", contactInfo.getType(), + contactInfo.getValue(), accountId); return false; } accountToUpdate.setContactsInfo(contactsInfoSet); @@ -590,8 +601,42 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio return false; } + private boolean deletedExistingContactInfo(ContactInfo contactInfo, Set contactsInfoSet) { + if (contactInfo == null || contactsInfoSet == null || contactsInfoSet.size() == 0) { + return false; + } + for (ContactInfo c : contactsInfoSet) { + if (c.getType().equals(contactInfo.getType()) && c.getValue().equals(contactInfo.getValue())) { + return contactsInfoSet.remove(c); + } + } + return false; + } + + private void setEmptyLabelExceptPhone(ContactInfo contactInfo) { + if (!contactInfo.getType().equals(ContactType.PHONE_CONTACT.toString())) { + contactInfo.setLabel(""); + } + } + + private boolean foundExistingContactInfo(ContactInfo contactInfo, Set contactsInfoSet) { + if (contactsInfoSet == null || contactsInfoSet.size() == 0) { + return false; + } + for (ContactInfo c : contactsInfoSet) { + if (c.getType().equals(contactInfo.getType()) && c.getValue().equals(contactInfo.getValue())) { + return true; + } + } + return false; + } + @Override public boolean deleteContactInfo(UUID accountId, ContactInfo contactInfo) { + if (contactInfo == null) { + logger.error("Error removing contact info from account {}. contactInfo is not set.", accountId); + return false; + } Account accountToUpdate = accountRepository.findByAccountId(accountId); if (accountToUpdate == null) { logger.error("Existing account with the provided id {} was not found.", accountId); @@ -602,9 +647,10 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio logger.error("No existing contact info found for account {}.", accountId); return false; } - if (!contactsInfoSet.remove(contactInfo)) { - logger.error("Error removing contact info from account {}. Existing contact info: {} was not found.", - accountId, contactInfo.toString()); + + if (!deletedExistingContactInfo(contactInfo, contactsInfoSet)) { + logger.error("Error removing contact info from account {}. Existing contact info: {}:{} was not found.", + accountId, contactInfo.getType(), contactInfo.getValue()); return false; } accountToUpdate.setContactsInfo(contactsInfoSet); @@ -656,6 +702,11 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio @Override public boolean editContactInfo(UUID accountId, ContactInfo oldContactInfo, ContactInfo editedContactInfo) { + if (oldContactInfo == null || editedContactInfo == null) { + logger.error("Error editing contact info for account {}. oldContactInfo/editedContactInfo is not set.", + accountId); + return false; + } Account accountToUpdate = accountRepository.findByAccountId(accountId); if (accountToUpdate == null) { logger.error("Existing account with the provided id {} was not found.", accountId); @@ -666,16 +717,26 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio logger.error("No existing contact info found for account {}.", accountId); return false; } - if (!contactsInfoSet.remove(oldContactInfo)) { - logger.error("Error removing contact info from account {}. Existing contact info: {} was not found.", - accountId, oldContactInfo.toString()); + + if (!deletedExistingContactInfo(oldContactInfo, contactsInfoSet)) { + logger.error("Error removing contact info from account {}. Existing contact info: {}:{} was not found.", + accountId, oldContactInfo.getType(), oldContactInfo.getValue()); return false; } - if (!contactsInfoSet.add(editedContactInfo)) { - logger.error("Error adding contact info to account {}. {} already exists.", accountId, - editedContactInfo.toString()); + + if (foundExistingContactInfo(editedContactInfo, contactsInfoSet)) { + logger.error("Error adding contact info to account {}. {}:{} already exists.", accountId, + editedContactInfo.getType(), editedContactInfo.getValue()); return false; + } else { + setEmptyLabelExceptPhone(editedContactInfo); + if (!contactsInfoSet.add(editedContactInfo)) { + logger.error("Error adding contact info {}:{} to account {}.", editedContactInfo.getType(), + editedContactInfo.getValue(), accountId); + return false; + } } + accountToUpdate.setContactsInfo(contactsInfoSet); Long timeUpdated = Instant.now().toEpochMilli(); accountToUpdate.setLastUpdateTimestamp(timeUpdated); diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 8b361710451eff3a8934e3cea1cc9c9276dba9b5..654c85f5ba840bd480de7d651f334009381c92bf 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -1255,7 +1255,7 @@ public class AccountServiceTests extends GrpcServerTestBase { .setContactInfo( ContactDetails.newBuilder().setType(ContactType.EMAIL_CONTACT).setValue(Util.EMAIL).build()) .build(); - given(accountRepositoryAdditional.addContactInfo(Mockito.any(UUID.class), Mockito.any(ContactInfo.class))) + given(accountRepositoryAdditional.deleteContactInfo(Mockito.any(UUID.class), Mockito.any(ContactInfo.class))) .willReturn(false); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc