diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java index 3503b4236be83bffa281342e3d73a454e54f8d73..80a8f851bbe3f57114a0b012315145ba11051cec 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditional.java @@ -15,6 +15,7 @@ import biz.nynja.account.models.AuthenticationProvider; import biz.nynja.account.models.ContactInfo; import biz.nynja.account.models.PendingAccountByAuthenticationProvider; import biz.nynja.account.models.Profile; +import biz.nynja.account.models.PendingAccount; @Repository public interface AccountRepositoryAdditional { @@ -44,4 +45,6 @@ public interface AccountRepositoryAdditional { boolean deleteContactInfo(UUID accountId, ContactInfo contactInfo); boolean editContactInfo(UUID accountId, ContactInfo oldContactInfo, ContactInfo editedContactInfo); + + PendingAccount savePendingAccount(PendingAccount updatedPendingAccount); } diff --git a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java index 38f4bff208bfe3027a2be163255b75c1f537ca8c..2497cbbcb7ea2d936ebe9d216d86bf5730e7214d 100644 --- a/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java +++ b/src/main/java/biz/nynja/account/repositories/AccountRepositoryAdditionalImpl.java @@ -97,17 +97,11 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio PendingAccount pendingAccount = pendingAccountRepository .findByAccountId(UUID.fromString(request.getAccountId())); if (pendingAccount == null) { - logger.info("Existing pending account with the provided id was not found."); - logger.debug("Existing pending account with the provided id {} was not found.", request.getAccountId()); + logger.info("Existing pending account with the provided id was not found or creation timeout expired."); + logger.debug("Existing pending account with the provided id {} was not found or creation timeout expired.", request.getAccountId()); return null; } Long timeCreated = Instant.now().toEpochMilli(); - Long checkMinutes = timeCreated - pendingAccount.getCreationTimestamp(); - if (checkMinutes > COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES * 60 * 1000) { - logger.info("Account creation timeout expired."); - pendingAccountRepository.deleteById(UUID.fromString(request.getAccountId())); - return null; - } WriteResult wr = null; try { newAccountInsert(batchOperations, request, pendingAccount, timeCreated); @@ -654,4 +648,19 @@ public class AccountRepositoryAdditionalImpl implements AccountRepositoryAdditio } return false; } + + public PendingAccount savePendingAccount(PendingAccount pendingAccount) { + //for saving back compatibility. need refactoring in the future + if (!createSavePendingAccount(pendingAccount)) { + throw new RuntimeException("Exception for save save pending account id=" + pendingAccount.getAccountId()); + } + return pendingAccount; + } + + private boolean createSavePendingAccount(PendingAccount pendingAccount) { + String cql = "INSERT INTO pendingAccount (accountId, profileId, authenticationProvider, authenticationProviderType, creationTimestamp)" + + " VALUES (?, ?, ? ,?, ?) " + + "USING TTL " + COMPLETE_PENDING_ACCOUNT_TIMEOUT_IN_MINUTES * 60 + ";"; + return cassandraTemplate.getCqlOperations().execute(cql, pendingAccount.getAccountId(), pendingAccount.getProfileId(), pendingAccount.getAuthenticationProvider(), pendingAccount.getAuthenticationProviderType(), pendingAccount.getCreationTimestamp()); + } } diff --git a/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java b/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java index 4d3c6f1eec8766ea9cef3ffbe029d9eb2970eb3e..f7d2c3c2b297bd93dbf9002196e080e2d5985cc4 100644 --- a/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java +++ b/src/main/java/biz/nynja/account/repositories/PendingAccountRepository.java @@ -16,4 +16,7 @@ public interface PendingAccountRepository extends CassandraRepository S save(S s); } diff --git a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java index 645d3bc66b32172f9b60987c6e2e1857ad8989dc..227c4cefd16aaedcf86cc48620f4de0514cf777b 100644 --- a/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java +++ b/src/main/java/biz/nynja/account/services/decomposition/AccountCreator.java @@ -80,13 +80,19 @@ public class AccountCreator { pendingAccount.setProfileId(UUID.randomUUID()); pendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount savedPendingAccount = pendingAccountRepository.save(pendingAccount); - logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount); - response = CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(savedPendingAccount.toProto()) - .build(); - logger.info("Pending account created successfully."); - logger.debug("Pending account: \"{}\" created successfully.", response); - return response; + try{ + PendingAccount savedPendingAccount = accountRepositoryAdditional.savePendingAccount(pendingAccount); + logger.debug("Pending account \"{}\" saved into the DB", savedPendingAccount); + response = CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(savedPendingAccount.toProto()) + .build(); + logger.info("Pending account created successfully."); + logger.debug("Pending account: \"{}\" created successfully.", response); + return response; + }catch (Exception e){ + logger.error(e.getMessage()); + return CreatePendingAccountResponse.newBuilder().setError(ErrorResponse.newBuilder().setCause(Cause.INTERNAL_SERVER_ERROR)) + .build(); + } } private CreatePendingAccountResponse updateAndValidatePendingAccountCreation(CreatePendingAccountRequest request) { @@ -105,7 +111,7 @@ public class AccountCreator { .setAuthenticationProviderType(foundExistingPendingAccount.getAuthenticationProviderType()); updatedPendingAccount.setCreationTimestamp(Instant.now().toEpochMilli()); - PendingAccount updatePendingAccount = pendingAccountRepository.save(updatedPendingAccount); + PendingAccount updatePendingAccount = accountRepositoryAdditional.savePendingAccount(updatedPendingAccount); return CreatePendingAccountResponse.newBuilder().setPendingAccountDetails(updatePendingAccount.toProto()) .build(); } diff --git a/src/test/java/biz/nynja/account/services/AccountServiceTests.java b/src/test/java/biz/nynja/account/services/AccountServiceTests.java index 8f02eedbce7a8319e013955a51a27f3f83224579..c3048e668765585bed47f76db790ef0ea35f6087 100644 --- a/src/test/java/biz/nynja/account/services/AccountServiceTests.java +++ b/src/test/java/biz/nynja/account/services/AccountServiceTests.java @@ -461,7 +461,7 @@ public class AccountServiceTests extends GrpcServerTestBase { AuthenticationProvider.createAuthenticationProviderFromStrings(request.getAuthenticationType().name(), request.getAuthenticationProvider()))) .willReturn(existingPendingAccountByAuthenticationProvider); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(existingPendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(existingPendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -500,7 +500,7 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testCreatePendingAccountOK() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL).setAuthenticationProvider(Util.EMAIL).build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -514,7 +514,7 @@ public class AccountServiceTests extends GrpcServerTestBase { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.EMAIL) .setAuthenticationProvider("invalid.E-mail1.@domain_test.com1").build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request); @@ -527,7 +527,7 @@ public class AccountServiceTests extends GrpcServerTestBase { public void testCreatePendingAccountInvalidPhone() { final CreatePendingAccountRequest request = CreatePendingAccountRequest.newBuilder() .setAuthenticationType(AuthenticationType.PHONE).setAuthenticationProvider("BG:084365:5555").build(); - given(pendingAccountRepository.save(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); + given(accountRepositoryAdditional.savePendingAccount(Mockito.any(PendingAccount.class))).willReturn(pendingAccount); final AccountServiceGrpc.AccountServiceBlockingStub accountServiceBlockingStub = AccountServiceGrpc .newBlockingStub(Optional.ofNullable(channel).orElse(inProcChannel)); final CreatePendingAccountResponse reply = accountServiceBlockingStub.createPendingAccount(request);