diff --git a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantStatementDomainEventHandlerTests.cs b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantStatementDomainEventHandlerTests.cs index 198f8a2..86b1ec7 100644 --- a/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantStatementDomainEventHandlerTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/MerchantStatementDomainEventHandlerTests.cs @@ -55,6 +55,37 @@ public async Task MerchantStatementDomainEventHandler_Handle_TransactionHasBeenC result.IsSuccess.ShouldBeTrue(); } + [Fact] + public async Task MerchantStatementDomainEventHandler_Handle_TransactionHasBeenCompletedEvent_WrongExpectedRetried_EventIsHandled() + { + List errors = new() { "WrongExpectedVersion" }; + + this.Mediator.SetupSequence(m => m.Send(It.IsAny>(), It.IsAny())) + .ReturnsAsync(Result.Failure(errors)) + .ReturnsAsync(Result.Success()); + + Result result = await this.EventHandler.Handle(TestData.DomainEvents.TransactionHasBeenCompletedEvent, CancellationToken.None); + result.IsSuccess.ShouldBeTrue(); + this.Mediator.Verify(m => m.Send(It.IsAny>(), It.IsAny()), Times.Exactly(2)); + } + + [Fact] + public async Task MerchantStatementDomainEventHandler_Handle_TransactionHasBeenCompletedEvent_WrongExpectedRetried_AllRetriesFailed() + { + List errors = new() { "WrongExpectedVersion" }; + this.Mediator.SetupSequence(m => m.Send(It.IsAny>(), It.IsAny())) + .ReturnsAsync(Result.Failure(errors)) + .ReturnsAsync(Result.Failure(errors)) + .ReturnsAsync(Result.Failure(errors)) + .ReturnsAsync(Result.Failure(errors)) + .ReturnsAsync(Result.Failure(errors)) + .ReturnsAsync(Result.Failure(errors)); + + Result result = await this.EventHandler.Handle(TestData.DomainEvents.TransactionHasBeenCompletedEvent, CancellationToken.None); + result.IsFailed.ShouldBeTrue(); + this.Mediator.Verify(m => m.Send(It.IsAny>(), It.IsAny()), Times.Exactly(6)); + } + [Fact] public async Task MerchantStatementDomainEventHandler_Handle_MerchantFeeSettledEvent_EventIsHandled() { diff --git a/TransactionProcessor.BusinessLogic/EventHandling/MerchantStatementDomainEventHandler.cs b/TransactionProcessor.BusinessLogic/EventHandling/MerchantStatementDomainEventHandler.cs index d8ad31b..aa8759d 100644 --- a/TransactionProcessor.BusinessLogic/EventHandling/MerchantStatementDomainEventHandler.cs +++ b/TransactionProcessor.BusinessLogic/EventHandling/MerchantStatementDomainEventHandler.cs @@ -109,11 +109,24 @@ private async Task HandleSpecificDomainEvent(MerchantStatementDomainEven private async Task HandleSpecificDomainEvent(TransactionDomainEvents.TransactionHasBeenCompletedEvent domainEvent, CancellationToken cancellationToken) { - MerchantStatementCommands.AddTransactionToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.CompletedDateTime, - Money.Create(domainEvent.TransactionAmount.GetValueOrDefault(0)), domainEvent.IsAuthorised, domainEvent.TransactionId); + IAsyncPolicy retryPolicy = PolicyFactory.CreatePolicy(policyTag: "MerchantStatementDomainEventHandler - HandleSpecificDomainEvent"); - Result result = await this.Mediator.Send(command, cancellationToken); - return result; + try + { + return await PolicyFactory.ExecuteWithPolicyAsync(async () => + { + MerchantStatementCommands.AddTransactionToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.CompletedDateTime, + Money.Create(domainEvent.TransactionAmount.GetValueOrDefault(0)), domainEvent.IsAuthorised, domainEvent.TransactionId); + + Result result = await this.Mediator.Send(command, cancellationToken); + return result; + + }, retryPolicy, "MerchantStatementDomainEventHandler - HandleSpecificDomainEvent"); + } + catch (Exception ex) + { + return Result.Failure(ex.GetExceptionMessages()); + } } private async Task HandleSpecificDomainEvent(MerchantDomainEvents.AutomaticDepositMadeEvent domainEvent, @@ -163,14 +176,6 @@ private async Task HandleSpecificDomainEvent(SettlementDomainEvents.Merc { return Result.Failure(ex.GetExceptionMessages()); } - //MerchantStatementCommands.AddSettledFeeToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.FeeCalculatedDateTime, - // PositiveMoney.Create(Money.Create(domainEvent.CalculatedValue)), domainEvent.TransactionId, domainEvent.FeeId); - - ////return await this.Mediator.Send(command, cancellationToken); - //Result result = await this.Mediator.Send(command, cancellationToken); - //return result; - - } #endregion