diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs index 4136b689..8890e353 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/TransactionDomainServiceTests.cs @@ -102,6 +102,8 @@ public async Task TransactionDomainService_ProcessLogonTransaction_DeviceNeedsAd result.Data.MerchantId.ShouldBe(TestData.MerchantId); result.Data.ResponseCode.ShouldBe("0001"); result.Data.TransactionId.ShouldBe(TestData.TransactionId); + this.AggregateService.Verify(t => t.GetLatest(TestData.MerchantId, It.IsAny()), Times.Once); + this.AggregateService.Verify(t => t.Save(It.IsAny(), It.IsAny()), Times.Once); } [Fact] diff --git a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs index 927eadb8..caf6cd3a 100644 --- a/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/TransactionDomainService.cs @@ -107,23 +107,9 @@ public async Task> ProcessLogonTransacti return ResultHelpers.CreateFailure(stateResult); Result validationResult = await this.TransactionValidationService.ValidateLogonTransaction(command.EstateId, command.MerchantId, command.DeviceIdentifier, cancellationToken); - - if (validationResult.IsSuccess && (validationResult.Data.ResponseCode == TransactionResponseCode.Success || validationResult.Data.ResponseCode == TransactionResponseCode.SuccessNeedToAddDevice)) { - if (validationResult.Data.ResponseCode == TransactionResponseCode.SuccessNeedToAddDevice) { - await this.AddDeviceToMerchant(command.MerchantId, command.DeviceIdentifier, cancellationToken); - } - - // Record the successful validation - stateResult = transactionAggregate.AuthoriseTransactionLocally(TransactionHelpers.GenerateAuthCode(), validationResult.Data.ResponseCode, validationResult.Data.ResponseMessage); - if (stateResult.IsFailed) - return ResultHelpers.CreateFailure(stateResult); - } - else { - // Record the failure - stateResult = transactionAggregate.DeclineTransactionLocally(validationResult.Data.ResponseCode, validationResult.Data.ResponseMessage); - if (stateResult.IsFailed) - return ResultHelpers.CreateFailure(stateResult); - } + stateResult = await this.RecordLogonTransactionValidation(command, transactionAggregate, validationResult, cancellationToken); + if (stateResult.IsFailed) + return ResultHelpers.CreateFailure(stateResult); stateResult = transactionAggregate.CompleteTransaction(); if (stateResult.IsFailed) @@ -136,13 +122,7 @@ public async Task> ProcessLogonTransacti Result saveResult = await this.AggregateService.Save(transactionAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult); - return Result.Success(new ProcessLogonTransactionResponse { - ResponseMessage = transactionAggregate.ResponseMessage, - ResponseCode = transactionAggregate.ResponseCode, - EstateId = command.EstateId, - MerchantId = command.MerchantId, - TransactionId = command.TransactionId - }); + return Result.Success(this.CreateLogonTransactionResponse(command, transactionAggregate)); } catch (Exception ex) { return Result.Failure(ex.GetExceptionMessages()); @@ -427,6 +407,36 @@ private async Task AddDeviceToMerchant(Guid merchantId, return await this.AggregateService.Save(merchantAggregate.Data, cancellationToken); } + private ProcessLogonTransactionResponse CreateLogonTransactionResponse(TransactionCommands.ProcessLogonTransactionCommand command, + TransactionAggregate transactionAggregate) { + return new ProcessLogonTransactionResponse { + ResponseMessage = transactionAggregate.ResponseMessage, + ResponseCode = transactionAggregate.ResponseCode, + EstateId = command.EstateId, + MerchantId = command.MerchantId, + TransactionId = command.TransactionId + }; + } + + private async Task RecordLogonTransactionValidation(TransactionCommands.ProcessLogonTransactionCommand command, + TransactionAggregate transactionAggregate, + Result validationResult, + CancellationToken cancellationToken) { + if (validationResult.IsSuccess && this.IsSuccessfulLogonValidation(validationResult.Data.ResponseCode)) { + if (validationResult.Data.ResponseCode == TransactionResponseCode.SuccessNeedToAddDevice) { + await this.AddDeviceToMerchant(command.MerchantId, command.DeviceIdentifier, cancellationToken); + } + + return transactionAggregate.AuthoriseTransactionLocally(TransactionHelpers.GenerateAuthCode(), validationResult.Data.ResponseCode, validationResult.Data.ResponseMessage); + } + + return transactionAggregate.DeclineTransactionLocally(validationResult.Data.ResponseCode, validationResult.Data.ResponseMessage); + } + + private Boolean IsSuccessfulLogonValidation(TransactionResponseCode responseCode) { + return responseCode == TransactionResponseCode.Success || responseCode == TransactionResponseCode.SuccessNeedToAddDevice; + } + private async Task> GetMerchant(Guid merchantId,