From f33a9e7cbfc37aeeb8a80f5ccd29151adb55109a Mon Sep 17 00:00:00 2001 From: StuartFerguson Date: Wed, 24 Dec 2025 08:30:07 +0000 Subject: [PATCH] validate merchant for building statements --- .../MerchantStatementDomainServiceTests.cs | 16 ++++++++++++++++ .../Services/MerchantStatementDomainService.cs | 6 ++++++ .../Services/StatementBuilder.cs | 2 -- 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/TransactionProcessor.BusinessLogic.Tests/Services/MerchantStatementDomainServiceTests.cs b/TransactionProcessor.BusinessLogic.Tests/Services/MerchantStatementDomainServiceTests.cs index 09e56909..3ce2a336 100644 --- a/TransactionProcessor.BusinessLogic.Tests/Services/MerchantStatementDomainServiceTests.cs +++ b/TransactionProcessor.BusinessLogic.Tests/Services/MerchantStatementDomainServiceTests.cs @@ -202,6 +202,22 @@ public async Task MerchantStatementDomainService_BuildStatement_GetMerchantFaile result.IsFailed.ShouldBeTrue(); } + [Fact] + public async Task MerchantStatementDomainService_BuildStatement_MerchantNotCreated_StatementIsNotBuilt() + { + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.GeneratedMerchantStatementAggregate())); + this.AggregateService.Setup(a => a.Save(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success); + + this.AggregateService.Setup(a => a.GetLatest(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.MerchantStatementForDateAggregateWithTransactionAndFee())); + + this.AggregateService.Setup(a => a.Get(It.IsAny(), It.IsAny())).ReturnsAsync(Result.Success(TestData.Aggregates.EmptyMerchantAggregate())); + + this.StatementBuilder.Setup(s => s.GetStatementHtml(It.IsAny(), It.IsAny(), It.IsAny())).ReturnsAsync(""); + + Result result = await this.DomainService.BuildStatement(TestData.Commands.BuildMerchantStatementCommand, CancellationToken.None); + result.IsFailed.ShouldBeTrue(); + } + [Fact] public async Task MerchantStatementDomainService_BuildStatement_SaveFailed_StatementIsNotBuilt() { diff --git a/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs b/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs index 05a395c0..646c8313 100644 --- a/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs +++ b/TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs @@ -262,8 +262,14 @@ public async Task BuildStatement(MerchantStatementCommands.BuildMerchant if (getMerchantResult.IsFailed) return ResultHelpers.CreateFailure(getMerchantResult); + if (getMerchantResult.Data.IsCreated == false) + return Result.Invalid("Merchant must be created to build a statement"); + Merchant merchantModel = getMerchantResult.Data.GetMerchant(); + if (merchantModel.Addresses.Any() == false) + return Result.Invalid("Merchant must have an address to build a statement"); + Result htmlResult = await this.StatementBuilder.GetStatementHtml(merchantStatementAggregate, merchantModel, cancellationToken); if (htmlResult.IsFailed) return ResultHelpers.CreateFailure(htmlResult); diff --git a/TransactionProcessor.BusinessLogic/Services/StatementBuilder.cs b/TransactionProcessor.BusinessLogic/Services/StatementBuilder.cs index 2e35a544..f92151cb 100644 --- a/TransactionProcessor.BusinessLogic/Services/StatementBuilder.cs +++ b/TransactionProcessor.BusinessLogic/Services/StatementBuilder.cs @@ -56,8 +56,6 @@ public async Task> GetStatementHtml(MerchantStatementAggregate st Merchant merchant, CancellationToken cancellationToken) { - // TODO: Check merchant and addresses exist - IDirectoryInfo path = this.FileSystem.Directory.GetParent(Assembly.GetExecutingAssembly().Location); MerchantStatement statementHeader = statementAggregate.GetStatement();