diff --git a/TransactionProcessor.Aggregates.Tests/SettlementAggregateTests.cs b/TransactionProcessor.Aggregates.Tests/SettlementAggregateTests.cs index 28f52777..725b4620 100644 --- a/TransactionProcessor.Aggregates.Tests/SettlementAggregateTests.cs +++ b/TransactionProcessor.Aggregates.Tests/SettlementAggregateTests.cs @@ -1,4 +1,5 @@ using Shouldly; +using SimpleResults; using TransactionProcessor.Testing; namespace TransactionProcessor.Aggregates.Tests @@ -17,7 +18,9 @@ public void SettlementAggregate_CanBeCreated_IsCreated() public void PendingSettlementAggregate_Create_AggregateIsCreated() { SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); - aggregate.Create(TestData.EstateId,TestData.MerchantId, TestData.SettlementDate); + Result result = aggregate.Create(TestData.EstateId,TestData.MerchantId, TestData.SettlementDate); + result.IsSuccess.ShouldBeTrue(); + aggregate.EstateId.ShouldBe(TestData.EstateId); aggregate.SettlementDate.ShouldBe(TestData.SettlementDate.Date); aggregate.IsCreated.ShouldBeTrue(); @@ -30,10 +33,8 @@ public void SettlementAggregate_Create_AlreadyCreated_ErrorThrown() SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); - Should.Throw(() => - { - aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); - }); + Result result = aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); + result.IsSuccess.ShouldBeTrue(); } [Fact] @@ -84,16 +85,12 @@ public void SettlementAggregate_AddFee_TwoFeesAdded_SameFeeIdDifferentTransactio } [Fact] - public void SettlementAggregate_AddFee_AggregateNotCreated_ErrorThrown() - { + public void SettlementAggregate_AddFee_AggregateNotCreated_ErrorThrown() { SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); - Should.Throw(() => - { - aggregate.AddFee(TestData.MerchantId, - TestData.TransactionId, - TestData.CalculatedFeeMerchantFee()); - }); + Result result = aggregate.AddFee(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee()); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); } [Fact] @@ -129,10 +126,41 @@ public void SettlementAggregate_AddFee_InvalidFeeType_ErrorThrown() { SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); - Should.Throw(() => - { - aggregate.AddFee(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeServiceProviderFee()); - }); + Result result = aggregate.AddFee(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeServiceProviderFee()); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); + } + + [Fact] + public void SettlementAggregate_AddFee_InvalidMerchantId_ErrorThrown() { + SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); + aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); + + Result result = aggregate.AddFee(Guid.Empty, TestData.TransactionId, TestData.CalculatedFeeMerchantFee()); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); + } + + [Fact] + public void SettlementAggregate_AddFee_InvalidTransactionId_ErrorThrown() + { + SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); + aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); + + Result result = aggregate.AddFee(TestData.MerchantId, Guid.Empty, TestData.CalculatedFeeMerchantFee()); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); + } + + [Fact] + public void SettlementAggregate_AddFee_InvalidFee_ErrorThrown() + { + SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); + aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); + + Result result = aggregate.AddFee(TestData.MerchantId, TestData.TransactionId, null); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); } [Fact] @@ -144,7 +172,8 @@ public void SettlementAggregate_MarkFeeAsSettled_FeeIsSettledAndSettlementComple aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(1); - aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + Result result = aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + result.IsSuccess.ShouldBeTrue(); aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(0); aggregate.GetNumberOfFeesSettled().ShouldBe(1); @@ -161,7 +190,8 @@ public void SettlementAggregate_MarkFeeAsSettled_FeeIsSettled_SettlementNotCompl aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(2); - aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + Result result = aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + result.IsSuccess.ShouldBeTrue(); aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(1); aggregate.GetNumberOfFeesSettled().ShouldBe(1); @@ -174,7 +204,8 @@ public void SettlementAggregate_MarkFeeAsSettled_PendingFeeNotFound_NoErrorThrow SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); - aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + Result result = aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + result.IsSuccess.ShouldBeTrue(); aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(0); aggregate.GetNumberOfFeesSettled().ShouldBe(0); @@ -189,7 +220,8 @@ public void SettlementAggregate_MarkFeeAsSettled_FeeAlreadySettled_NoErrorThrown aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(1); - aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + Result result = aggregate.MarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId, TestData.SettlementDate); + result.IsSuccess.ShouldBeTrue(); aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(0); aggregate.GetNumberOfFeesSettled().ShouldBe(1); @@ -206,7 +238,8 @@ public void SettlementAggregate_ImmediatelyMarkFeeAsSettled_FeeIsSettledAndSettl aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(1); - aggregate.ImmediatelyMarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId); + Result result = aggregate.ImmediatelyMarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId); + result.IsSuccess.ShouldBeTrue(); aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(0); aggregate.GetNumberOfFeesSettled().ShouldBe(1); @@ -222,8 +255,9 @@ public void SettlementAggregate_ImmediatelyMarkFeeAsSettled_FeeIsAlreadySettled( aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(1); - aggregate.ImmediatelyMarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId); - + Result result = aggregate.ImmediatelyMarkFeeAsSettled(TestData.MerchantId, TestData.TransactionId, TestData.CalculatedFeeMerchantFee().FeeId); + result.IsSuccess.ShouldBeTrue(); + aggregate.GetNumberOfFeesPendingSettlement().ShouldBe(0); aggregate.GetNumberOfFeesSettled().ShouldBe(1); aggregate.SettlementComplete.ShouldBeFalse(); @@ -234,7 +268,8 @@ public void SettlementAggregate_StartProcessing_ProcessingStarted() { SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); - aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime); + Result result = aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime); + result.IsSuccess.ShouldBeTrue(); aggregate.ProcessingStarted.ShouldBeTrue(); aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTime); @@ -245,10 +280,12 @@ public void SettlementAggregate_StartProcessing_CalledTwice_ProcessingStarted(){ SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate); aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime); - aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTimeSecondCall); + Result result = aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTimeSecondCall); - aggregate.ProcessingStarted.ShouldBeTrue(); - aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTimeSecondCall); + result.IsSuccess.ShouldBeTrue(); + + aggregate.ProcessingStarted.ShouldBeTrue(); + aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTime); } [Fact] @@ -256,9 +293,9 @@ public void SettlementAggregate_StartProcessing_SettlementNotCreated_ErrorThron( { SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); - Should.Throw(() => { - aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime); - }); + Result result = aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); } [Fact] @@ -285,13 +322,12 @@ public void SettlementAggregate_ManuallyComplete_CalledTwice_SettlementCompleted } [Fact] - public void SettlementAggregate_ManuallyComplete_SettlementNotCreated_ErrorThron() - { + public void SettlementAggregate_ManuallyComplete_SettlementNotCreated_ErrorThron() { SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId); - Should.Throw(() => { - aggregate.ManuallyComplete(); - }); + Result result = aggregate.ManuallyComplete(); + result.IsFailed.ShouldBeTrue(); + result.Status.ShouldBe(ResultStatus.Invalid); } [Fact] diff --git a/TransactionProcessor.Aggregates/SettlementAggregate.cs b/TransactionProcessor.Aggregates/SettlementAggregate.cs index 561a82f4..d64ba48e 100644 --- a/TransactionProcessor.Aggregates/SettlementAggregate.cs +++ b/TransactionProcessor.Aggregates/SettlementAggregate.cs @@ -1,4 +1,5 @@ -using TransactionProcessor.DomainEvents; +using SimpleResults; +using TransactionProcessor.DomainEvents; using TransactionProcessor.Models; using TransactionProcessor.Models.Contract; @@ -13,56 +14,65 @@ namespace TransactionProcessor.Aggregates using Shared.General; public static class SettlementAggregateExtensions{ - public static void StartProcessing(this SettlementAggregate aggregate, DateTime dateTime){ + public static Result StartProcessing(this SettlementAggregate aggregate, DateTime dateTime){ - aggregate.CheckHasBeenCreated(); + Result result = aggregate.CheckHasBeenCreated(); + if (result.IsFailed) + return result; + if (aggregate.ProcessingStarted) + return Result.Success(); SettlementDomainEvents.SettlementProcessingStartedEvent startedEvent = new SettlementDomainEvents.SettlementProcessingStartedEvent(aggregate.AggregateId, aggregate.EstateId, aggregate.MerchantId, dateTime); aggregate.ApplyAndAppend(startedEvent); + + return Result.Success(); } - public static void ManuallyComplete(this SettlementAggregate aggregate){ - - aggregate.CheckHasBeenCreated(); + public static Result ManuallyComplete(this SettlementAggregate aggregate){ if (aggregate.SettlementComplete) - return; + return Result.Success(); + + Result result = aggregate.CheckHasBeenCreated(); + if (result.IsFailed) + return result; SettlementDomainEvents.SettlementCompletedEvent pendingSettlementCompletedEvent = new SettlementDomainEvents.SettlementCompletedEvent(aggregate.AggregateId, aggregate.EstateId, aggregate.MerchantId); aggregate.ApplyAndAppend(pendingSettlementCompletedEvent); + + return Result.Success(); } - public static void MarkFeeAsSettled(this SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId, DateTime settledDate) + public static Result MarkFeeAsSettled(this SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId, DateTime settledDate) { - (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) pendingFee = SettlementAggregateExtensions.GetPendingFee(aggregate, merchantId, transactionId, feeId); + (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) pendingFee = GetPendingFee(aggregate, merchantId, transactionId, feeId); - (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) settledFee = SettlementAggregateExtensions.GetSettledFee(aggregate, merchantId, transactionId, feeId); + (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) settledFee = GetSettledFee(aggregate, merchantId, transactionId, feeId); - if (settledFee != default((Guid, Guid, CalculatedFee))) - { + if (settledFee != default((Guid, Guid, CalculatedFee))) { // Fee already settled.... - return; + return Result.Success(); } - if (pendingFee == default((Guid, Guid, CalculatedFee))) - { + if (pendingFee == default((Guid, Guid, CalculatedFee))) { // Fee not found.... - return; + return Result.Success(); } - SettlementDomainEvents.MerchantFeeSettledEvent merchantFeeSettledEvent = SettlementAggregateExtensions.CreateMerchantFeeSettledEvent(aggregate, pendingFee,settledDate); + SettlementDomainEvents.MerchantFeeSettledEvent merchantFeeSettledEvent = CreateMerchantFeeSettledEvent(aggregate, pendingFee,settledDate); aggregate.ApplyAndAppend(merchantFeeSettledEvent); if (aggregate.CalculatedFeesPendingSettlement.Any() == false) { // Settlement is completed - SettlementDomainEvents.SettlementCompletedEvent pendingSettlementCompletedEvent = new SettlementDomainEvents.SettlementCompletedEvent(aggregate.AggregateId, aggregate.EstateId, aggregate.MerchantId); + SettlementDomainEvents.SettlementCompletedEvent pendingSettlementCompletedEvent = new(aggregate.AggregateId, aggregate.EstateId, aggregate.MerchantId); aggregate.ApplyAndAppend(pendingSettlementCompletedEvent); } + return Result.Success(); } private static (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) GetSettledFee(SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId){ @@ -86,27 +96,27 @@ private static SettlementDomainEvents.MerchantFeeSettledEvent CreateMerchantFeeS return merchantFeeSettledEvent; } - public static void ImmediatelyMarkFeeAsSettled(this SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId) + public static Result ImmediatelyMarkFeeAsSettled(this SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId) { (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) pendingFee = SettlementAggregateExtensions.GetPendingFee(aggregate, merchantId, transactionId, feeId); (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) settledFee = SettlementAggregateExtensions.GetSettledFee(aggregate, merchantId, transactionId, feeId); - if (settledFee != default((Guid, Guid, CalculatedFee))) - { + if (settledFee != default((Guid, Guid, CalculatedFee))) { // Fee already settled.... - return; + return Result.Success(); } - if (pendingFee == default((Guid, Guid, CalculatedFee))) - { + if (pendingFee == default((Guid, Guid, CalculatedFee))) { // Fee not found.... - return; + return Result.Success(); } - SettlementDomainEvents.MerchantFeeSettledEvent merchantFeeSettledEvent = SettlementAggregateExtensions.CreateMerchantFeeSettledEvent(aggregate, pendingFee,DateTime.Now); + SettlementDomainEvents.MerchantFeeSettledEvent merchantFeeSettledEvent = CreateMerchantFeeSettledEvent(aggregate, pendingFee,DateTime.Now); aggregate.ApplyAndAppend(merchantFeeSettledEvent); + + return Result.Success(); } private static (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) GetPendingFee(SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId){ @@ -115,18 +125,25 @@ private static (Guid transactionId, Guid merchantId, CalculatedFee calculatedFee return pendingFee; } - public static void AddFee(this SettlementAggregate aggregate, + public static Result AddFee(this SettlementAggregate aggregate, Guid merchantId, Guid transactionId, CalculatedFee calculatedFee) { - Guard.ThrowIfInvalidGuid(merchantId, nameof(merchantId)); - Guard.ThrowIfInvalidGuid(transactionId, nameof(merchantId)); - Guard.ThrowIfNull(calculatedFee, nameof(calculatedFee)); + if (merchantId == Guid.Empty) + return Result.Invalid("Merchant Id cannot be an empty Guid"); + if (transactionId == Guid.Empty) { + return Result.Invalid("Merchant Id cannot be an empty Guid"); + } + if (calculatedFee == null) + return Result.Invalid("Calculated Fee cannot be null"); + + Result result = aggregate.CheckHasBeenCreated(); + if (result.IsFailed) + return result; - aggregate.CheckHasBeenCreated(); if (aggregate.HasFeeAlreadyBeenAdded(transactionId, calculatedFee)) - return; + return Result.Success(); DomainEvent @event = null; if (calculatedFee.FeeType == FeeType.Merchant) @@ -144,10 +161,12 @@ public static void AddFee(this SettlementAggregate aggregate, } else { - throw new InvalidOperationException("Unsupported Fee Type"); + return Result.Invalid("Unsupported Fee Type"); } aggregate.ApplyAndAppend(@event); + + return Result.Success(); } public static List<(Guid transactionId, Guid merchantId, CalculatedFee calculatedFee)> GetFeesToBeSettled(this SettlementAggregate aggregate) @@ -155,15 +174,17 @@ public static void AddFee(this SettlementAggregate aggregate, return aggregate.CalculatedFeesPendingSettlement; } - public static void Create(this SettlementAggregate aggregate, Guid estateId,Guid merchantId, + public static Result Create(this SettlementAggregate aggregate, Guid estateId,Guid merchantId, DateTime settlementDate) { - aggregate.CheckHasNotAlreadyBeenCreated(); + if (aggregate.IsCreated) + return Result.Success(); - SettlementDomainEvents.SettlementCreatedForDateEvent pendingSettlementCreatedForDateEvent = - new SettlementDomainEvents.SettlementCreatedForDateEvent(aggregate.AggregateId, estateId,merchantId, settlementDate.Date); + SettlementDomainEvents.SettlementCreatedForDateEvent pendingSettlementCreatedForDateEvent = new(aggregate.AggregateId, estateId, merchantId, settlementDate.Date); aggregate.ApplyAndAppend(pendingSettlementCreatedForDateEvent); + + return Result.Success(); } public static Int32 GetNumberOfFeesPendingSettlement(this SettlementAggregate aggregate) @@ -182,20 +203,12 @@ private static Boolean HasFeeAlreadyBeenAdded(this SettlementAggregate aggregate aggregate.SettledCalculatedFees.Any(c => c.calculatedFee.FeeId == calculatedFee.FeeId && c.transactionId == transactionId); } - private static void CheckHasBeenCreated(this SettlementAggregate aggregate) + private static Result CheckHasBeenCreated(this SettlementAggregate aggregate) { - if (aggregate.IsCreated == false) - { - throw new InvalidOperationException($"Pending Settlement not created for this date {aggregate.SettlementDate}"); - } - } - - private static void CheckHasNotAlreadyBeenCreated(this SettlementAggregate aggregate) - { - if (aggregate.IsCreated) - { - throw new InvalidOperationException($"Pending Settlement already created for this date {aggregate.SettlementDate}"); + if (aggregate.IsCreated == false) { + return Result.Invalid($"Pending Settlement not created for this date {aggregate.SettlementDate}"); } + return Result.Success(); } public static void PlayEvent(this SettlementAggregate aggregate, SettlementDomainEvents.MerchantFeeSettledEvent domainEvent) diff --git a/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs b/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs index 44ee5b05..392d1544 100644 --- a/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs @@ -77,7 +77,9 @@ public async Task> ProcessSettlement(SettlementCommands.ProcessSett if (feesToBeSettled.Any()) { // Record the process call - settlementAggregate.StartProcessing(DateTime.Now); + Result stateResult = settlementAggregate.StartProcessing(DateTime.Now); + if (stateResult.IsFailed) + return ResultHelpers.CreateFailure(stateResult); settlementSaveResult = await this.AggregateService.Save(settlementAggregate, cancellationToken); } @@ -136,7 +138,9 @@ public async Task AddMerchantFeePendingSettlement(SettlementCommands.Add if (settlementAggregate.IsCreated == false) { Logger.LogInformation("In ApplySettlementUpdates - aggregate not created"); - settlementAggregate.Create(command.EstateId, command.MerchantId, command.SettlementDueDate.Date); + Result createResult = settlementAggregate.Create(command.EstateId, command.MerchantId, command.SettlementDueDate.Date); + if (createResult.IsFailed) + return ResultHelpers.CreateFailure(createResult); } // Create Calculated Fee from the domain event @@ -151,7 +155,9 @@ public async Task AddMerchantFeePendingSettlement(SettlementCommands.Add SettlementDueDate = command.SettlementDueDate }; Logger.LogInformation("In ApplySettlementUpdates - about to add fee"); - settlementAggregate.AddFee(command.MerchantId, command.TransactionId, calculatedFee); + Result stateResult = settlementAggregate.AddFee(command.MerchantId, command.TransactionId, calculatedFee); + if (stateResult.IsFailed) + return ResultHelpers.CreateFailure(stateResult); Result saveResult = await this.AggregateService.Save(settlementAggregate, cancellationToken); if (saveResult.IsFailed) @@ -183,14 +189,18 @@ public async Task AddSettledFeeToSettlement(SettlementCommands.AddSettle return ResultHelpers.CreateFailure(getMerchantResult); MerchantAggregate merchant = getMerchantResult.Data; - + Result stateResult = Result.Failure(); if (merchant.SettlementSchedule == SettlementSchedule.Immediate) { - settlementAggregate.ImmediatelyMarkFeeAsSettled(command.MerchantId, command.TransactionId, command.FeeId); + stateResult = settlementAggregate.ImmediatelyMarkFeeAsSettled(command.MerchantId, command.TransactionId, command.FeeId); } else { - settlementAggregate.MarkFeeAsSettled(command.MerchantId, command.TransactionId, command.FeeId, command.SettledDate.Date); + stateResult = settlementAggregate.MarkFeeAsSettled(command.MerchantId, command.TransactionId, command.FeeId, command.SettledDate.Date); } + if (stateResult.IsFailed) + return stateResult; + + Result saveResult = await this.AggregateService.Save(settlementAggregate, cancellationToken); if (saveResult.IsFailed) return ResultHelpers.CreateFailure(saveResult);