diff --git a/TransactionProcessor.Aggregates.Tests/MerchantStatementForDateAggregateTests.cs b/TransactionProcessor.Aggregates.Tests/MerchantStatementForDateAggregateTests.cs index 2bb2b272..3fa82221 100644 --- a/TransactionProcessor.Aggregates.Tests/MerchantStatementForDateAggregateTests.cs +++ b/TransactionProcessor.Aggregates.Tests/MerchantStatementForDateAggregateTests.cs @@ -9,6 +9,11 @@ namespace TransactionProcessor.Aggregates.Tests; public class MerchantStatementForDateAggregateTests { + private const Int32 TransactionLineType = 1; + private const Int32 SettledFeeLineType = 2; + private const Int32 DepositLineType = 3; + private const Int32 WithdrawalLineType = 4; + [Fact] public void MerchantStatementForDateAggregate_CanBeCreated_IsCreated() { @@ -237,6 +242,49 @@ public void MerchantStatementForDateAggregate_AddWithdrawalToStatement_Withdrawa statementLines.Count.ShouldBe(1); } + [Fact] + public void MerchantStatementForDateAggregate_GetStatement_IncludeStatementLines_AddsAllLineTypes() + { + Guid settledFeeEventId = Guid.NewGuid(); + Guid depositEventId = Guid.NewGuid(); + Guid withdrawalEventId = Guid.NewGuid(); + MerchantStatementForDateAggregate merchantStatementForDateAggregate = MerchantStatementForDateAggregate.Create(TestData.MerchantStatementForDateId1); + merchantStatementForDateAggregate.AddTransactionToStatement(TestData.MerchantStatementId, TestData.StatementDate, TestData.EventId1, TestData.EstateId, TestData.MerchantId, TestData.Transaction1); + merchantStatementForDateAggregate.AddSettledFeeToStatement(TestData.MerchantStatementId, TestData.StatementDate, settledFeeEventId, TestData.EstateId, TestData.MerchantId, TestData.SettledFee1); + merchantStatementForDateAggregate.AddDepositToStatement(TestData.MerchantStatementId, + TestData.StatementDate, + depositEventId, + TestData.EstateId, + TestData.MerchantId, new Deposit + { + DepositDateTime = TestData.DepositDateTime, + Amount = TestData.DepositAmount.Value, + DepositId = TestData.DepositId, + Reference = TestData.DepositReference, + Source = MerchantDepositSource.Manual + }); + merchantStatementForDateAggregate.AddWithdrawalToStatement(TestData.MerchantStatementId, + TestData.StatementDate, + withdrawalEventId, + TestData.EstateId, + TestData.MerchantId, new Withdrawal + { + WithdrawalDateTime = TestData.WithdrawalDateTime, + Amount = TestData.WithdrawalAmount.Value, + WithdrawalId = TestData.WithdrawalId + }); + + MerchantStatementForDate merchantStatementForDate = merchantStatementForDateAggregate.GetStatement(true); + List? statementLines = merchantStatementForDate.GetStatementLines(); + + statementLines.ShouldNotBeNull(); + statementLines.Count.ShouldBe(4); + statementLines.Count(line => line.LineType == TransactionLineType && line.Amount == TestData.Transaction1.Amount && line.DateTime == TestData.Transaction1.DateTime).ShouldBe(1); + statementLines.Count(line => line.LineType == SettledFeeLineType && line.Amount == TestData.SettledFee1.Amount && line.DateTime == TestData.SettledFee1.DateTime).ShouldBe(1); + statementLines.Count(line => line.LineType == DepositLineType && line.Amount == TestData.DepositAmount.Value && line.DateTime == TestData.DepositDateTime).ShouldBe(1); + statementLines.Count(line => line.LineType == WithdrawalLineType && line.Amount == TestData.WithdrawalAmount.Value && line.DateTime == TestData.WithdrawalDateTime).ShouldBe(1); + } + [Theory] [InlineData("", null, null)] [InlineData(null, "", null)] @@ -275,4 +323,4 @@ public void MerchantStatementForDateAggregate_AddWithdrawalToStatement_InvalidDa result.IsFailed.ShouldBeTrue(); result.Status.ShouldBe(ResultStatus.Invalid); } -} \ No newline at end of file +} diff --git a/TransactionProcessor.Aggregates/MerchantStatementForDateAggregate.cs b/TransactionProcessor.Aggregates/MerchantStatementForDateAggregate.cs index fd540647..a0e3f5fd 100644 --- a/TransactionProcessor.Aggregates/MerchantStatementForDateAggregate.cs +++ b/TransactionProcessor.Aggregates/MerchantStatementForDateAggregate.cs @@ -13,6 +13,11 @@ namespace TransactionProcessor.Aggregates; public static class MerchantStatementForDateAggregateExtensions { + private const Int32 TransactionLineType = 1; + private const Int32 SettledFeeLineType = 2; + private const Int32 DepositLineType = 3; + private const Int32 WithdrawalLineType = 4; + public static Result AddSettledFeeToStatement(this MerchantStatementForDateAggregate aggregate, Guid merchantStatementId, DateTime statementDate, @@ -176,53 +181,36 @@ public static MerchantStatementForDate GetStatement(this MerchantStatementForDat if (includeStatementLines) { - foreach (Transaction transaction in aggregate.Transactions) - { - merchantStatement.AddStatementLine(new MerchantStatementLine - { - Amount = transaction.Amount, - DateTime = transaction.DateTime, - Description = string.Empty, - LineType = 1 // Transaction - }); - } - - foreach (SettledFee settledFee in aggregate.SettledFees) - { - merchantStatement.AddStatementLine(new MerchantStatementLine - { - Amount = settledFee.Amount, - DateTime = settledFee.DateTime, - Description = string.Empty, - LineType = 2 // Settled Fee - }); - } - - foreach (Deposit deposit in aggregate.Deposits) - { - merchantStatement.AddStatementLine(new MerchantStatementLine - { - Amount = deposit.Amount, - DateTime = deposit.DepositDateTime, - Description = string.Empty, - LineType = 3 // Deposit - }); - } - - foreach (Withdrawal withdrawal in aggregate.Withdrawals) - { - merchantStatement.AddStatementLine(new MerchantStatementLine - { - Amount = withdrawal.Amount, - DateTime = withdrawal.WithdrawalDateTime, - Description = string.Empty, - LineType = 4 // Withdrawal - }); - } + merchantStatement.AddStatementLines(aggregate.Transactions, transaction => transaction.Amount, transaction => transaction.DateTime, TransactionLineType); + merchantStatement.AddStatementLines(aggregate.SettledFees, settledFee => settledFee.Amount, settledFee => settledFee.DateTime, SettledFeeLineType); + merchantStatement.AddStatementLines(aggregate.Deposits, deposit => deposit.Amount, deposit => deposit.DepositDateTime, DepositLineType); + merchantStatement.AddStatementLines(aggregate.Withdrawals, withdrawal => withdrawal.Amount, withdrawal => withdrawal.WithdrawalDateTime, WithdrawalLineType); } return merchantStatement; } + + private static void AddStatementLines(this MerchantStatementForDate merchantStatement, + IEnumerable source, + Func amountSelector, + Func dateTimeSelector, + Int32 lineType) + { + foreach (T item in source) + { + merchantStatement.AddStatementLine(CreateStatementLine(amountSelector(item), dateTimeSelector(item), lineType)); + } + } + + private static MerchantStatementLine CreateStatementLine(Decimal amount, + DateTime dateTime, + Int32 lineType) => new() + { + Amount = amount, + DateTime = dateTime, + Description = string.Empty, + LineType = lineType + }; } public record MerchantStatementForDateAggregate : Aggregate @@ -283,4 +271,4 @@ protected override Object GetMetadata() } #endregion -} \ No newline at end of file +}