|
| 1 | +using Shared.DomainDrivenDesign.EventSourcing; |
| 2 | +using Shared.EventStore.Aggregate; |
| 3 | +using Shared.General; |
| 4 | +using System; |
| 5 | +using System.Collections.Generic; |
| 6 | +using System.Diagnostics.CodeAnalysis; |
| 7 | +using System.Linq; |
| 8 | +using System.Runtime.CompilerServices; |
| 9 | +using System.Text; |
| 10 | +using System.Threading.Tasks; |
| 11 | +using TransactionProcessor.DomainEvents; |
| 12 | +using TransactionProcessor.Models.Merchant; |
| 13 | + |
| 14 | +namespace TransactionProcessor.Aggregates |
| 15 | +{ |
| 16 | + public static class MerchantBalanceAggregateExtensions |
| 17 | + { |
| 18 | + public static void PlayEvent(this MerchantBalanceAggregate aggregate, MerchantBalanceDomainEvents.AuthorisedTransactionRecordedEvent domainEvent) |
| 19 | + { |
| 20 | + aggregate.Balance -= domainEvent.Amount; |
| 21 | + aggregate.AuthorisedSales = aggregate.AuthorisedSales with { Count = aggregate.AuthorisedSales.Count + 1, Value = aggregate.AuthorisedSales.Value + domainEvent.Amount, LastActivity = domainEvent.DateTime }; |
| 22 | + } |
| 23 | + |
| 24 | + public static void PlayEvent(this MerchantBalanceAggregate aggregate, MerchantBalanceDomainEvents.DeclinedTransactionRecordedEvent domainEvent) |
| 25 | + { |
| 26 | + aggregate.DeclinedSales= aggregate.AuthorisedSales with { Count = aggregate.DeclinedSales.Count + 1, Value = aggregate.DeclinedSales.Value + domainEvent.Amount, LastActivity = domainEvent.DateTime }; |
| 27 | + } |
| 28 | + |
| 29 | + public static void PlayEvent(this MerchantBalanceAggregate aggregate, MerchantBalanceDomainEvents.MerchantDepositRecordedEvent domainEvent) |
| 30 | + { |
| 31 | + aggregate.Balance += domainEvent.Amount; |
| 32 | + aggregate.Deposits = aggregate.Deposits with { Count = aggregate.Deposits.Count + 1, Value = aggregate.Deposits.Value + domainEvent.Amount, LastActivity = domainEvent.DateTime }; |
| 33 | + } |
| 34 | + |
| 35 | + public static void PlayEvent(this MerchantBalanceAggregate aggregate, MerchantBalanceDomainEvents.MerchantWithdrawalRecordedEvent domainEvent) |
| 36 | + { |
| 37 | + aggregate.Balance -= domainEvent.Amount; |
| 38 | + aggregate.Withdrawals = aggregate.Withdrawals with { Count = aggregate.Withdrawals.Count + 1, Value = aggregate.Withdrawals.Value + domainEvent.Amount, LastActivity = domainEvent.DateTime }; |
| 39 | + } |
| 40 | + |
| 41 | + public static void PlayEvent(this MerchantBalanceAggregate aggregate, MerchantBalanceDomainEvents.SettledFeeRecordedEvent domainEvent) |
| 42 | + { |
| 43 | + aggregate.Balance += domainEvent.Amount; |
| 44 | + aggregate.Fees = aggregate.Fees with { Count = aggregate.Fees.Count + 1, Value = aggregate.Fees.Value + domainEvent.Amount, LastActivity = domainEvent.DateTime }; |
| 45 | + } |
| 46 | + |
| 47 | + public static void PlayEvent(this MerchantBalanceAggregate aggregate, MerchantBalanceDomainEvents.MerchantBalanceInitialisedEvent domainEvent) |
| 48 | + { |
| 49 | + aggregate.IsInitialised = true; |
| 50 | + aggregate.EstateId = domainEvent.EstateId; |
| 51 | + aggregate.Balance = 0; |
| 52 | + } |
| 53 | + |
| 54 | + private static void EnsureMerchantHasBeenCreated(this MerchantBalanceAggregate aggregate, |
| 55 | + MerchantAggregate merchantAggregate) { |
| 56 | + if (merchantAggregate.IsCreated == false) { |
| 57 | + throw new InvalidOperationException("Merchant has not been created"); |
| 58 | + } |
| 59 | + } |
| 60 | + |
| 61 | + private static void EnsureMerchantBalanceHasBeenInitialised(this MerchantBalanceAggregate aggregate, |
| 62 | + MerchantAggregate merchantAggregate, |
| 63 | + DateTime dateTime) |
| 64 | + { |
| 65 | + if (aggregate.IsInitialised == false) { |
| 66 | + Merchant merchant = merchantAggregate.GetMerchant(); |
| 67 | + MerchantBalanceDomainEvents.MerchantBalanceInitialisedEvent merchantBalanceInitialisedEvent = new MerchantBalanceDomainEvents.MerchantBalanceInitialisedEvent(merchant.MerchantId, merchant.EstateId,dateTime); |
| 68 | + aggregate.ApplyAndAppend(merchantBalanceInitialisedEvent); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + |
| 73 | + public static void RecordCompletedTransaction(this MerchantBalanceAggregate aggregate, |
| 74 | + MerchantAggregate merchantAggregate, |
| 75 | + Guid transactionId, |
| 76 | + Decimal transactionAmount, |
| 77 | + DateTime transactionDateTime, |
| 78 | + Boolean isAuthorised) { |
| 79 | + aggregate.EnsureMerchantHasBeenCreated(merchantAggregate); |
| 80 | + aggregate.EnsureMerchantBalanceHasBeenInitialised(merchantAggregate, transactionDateTime); |
| 81 | + DomainEvent domainEvent = isAuthorised switch { |
| 82 | + true => new MerchantBalanceDomainEvents.AuthorisedTransactionRecordedEvent(aggregate.AggregateId, aggregate.EstateId, transactionId, transactionAmount, transactionDateTime), |
| 83 | + _ => new MerchantBalanceDomainEvents.DeclinedTransactionRecordedEvent(aggregate.AggregateId, aggregate.EstateId, transactionId, transactionAmount, transactionDateTime) |
| 84 | + }; |
| 85 | + aggregate.ApplyAndAppend(domainEvent); |
| 86 | + } |
| 87 | + |
| 88 | + public static void RecordMerchantDeposit(this MerchantBalanceAggregate aggregate, |
| 89 | + MerchantAggregate merchantAggregate, |
| 90 | + Guid depositId, |
| 91 | + Decimal depositAmount, |
| 92 | + DateTime depositDateTime) { |
| 93 | + aggregate.EnsureMerchantHasBeenCreated(merchantAggregate); |
| 94 | + aggregate.EnsureMerchantBalanceHasBeenInitialised(merchantAggregate, depositDateTime); |
| 95 | + |
| 96 | + MerchantBalanceDomainEvents.MerchantDepositRecordedEvent domainEvent = new MerchantBalanceDomainEvents.MerchantDepositRecordedEvent(aggregate.AggregateId, aggregate.EstateId, depositId, depositAmount, depositDateTime); |
| 97 | + aggregate.ApplyAndAppend(domainEvent); |
| 98 | + } |
| 99 | + |
| 100 | + public static void RecordMerchantWithdrawal(this MerchantBalanceAggregate aggregate, |
| 101 | + MerchantAggregate merchantAggregate, |
| 102 | + Guid withdrawalId, |
| 103 | + Decimal withdrawalAmount, |
| 104 | + DateTime withdrawalDateTime) { |
| 105 | + aggregate.EnsureMerchantHasBeenCreated(merchantAggregate); |
| 106 | + aggregate.EnsureMerchantBalanceHasBeenInitialised(merchantAggregate, withdrawalDateTime); |
| 107 | + |
| 108 | + MerchantBalanceDomainEvents.MerchantWithdrawalRecordedEvent domainEvent = new MerchantBalanceDomainEvents.MerchantWithdrawalRecordedEvent(aggregate.AggregateId, aggregate.EstateId, withdrawalId, withdrawalAmount, withdrawalDateTime); |
| 109 | + aggregate.ApplyAndAppend(domainEvent); |
| 110 | + } |
| 111 | + |
| 112 | + public static void RecordSettledFee(this MerchantBalanceAggregate aggregate, |
| 113 | + MerchantAggregate merchantAggregate, |
| 114 | + Guid feeId, |
| 115 | + Decimal feeAmount, |
| 116 | + DateTime feeDateTime) { |
| 117 | + aggregate.EnsureMerchantHasBeenCreated(merchantAggregate); |
| 118 | + aggregate.EnsureMerchantBalanceHasBeenInitialised(merchantAggregate, feeDateTime); |
| 119 | + |
| 120 | + MerchantBalanceDomainEvents.SettledFeeRecordedEvent domainEvent = new MerchantBalanceDomainEvents.SettledFeeRecordedEvent(aggregate.AggregateId, aggregate.EstateId, feeId, feeAmount, feeDateTime); |
| 121 | + aggregate.ApplyAndAppend(domainEvent); |
| 122 | + } |
| 123 | + } |
| 124 | + |
| 125 | + public record ActivityType(Int32 Count, Decimal Value, DateTime LastActivity); |
| 126 | + |
| 127 | + public record MerchantBalanceAggregate : Aggregate { |
| 128 | + |
| 129 | + public Boolean IsInitialised { get; internal set; } |
| 130 | + public Guid EstateId { get; internal set; } |
| 131 | + public Decimal Balance { get; internal set; } |
| 132 | + public ActivityType Deposits { get; internal set; } |
| 133 | + public ActivityType Withdrawals { get; internal set; } |
| 134 | + public ActivityType AuthorisedSales { get; internal set; } |
| 135 | + public ActivityType DeclinedSales { get; internal set; } |
| 136 | + public ActivityType Fees { get; internal set; } |
| 137 | + |
| 138 | + [ExcludeFromCodeCoverage] |
| 139 | + public MerchantBalanceAggregate() |
| 140 | + { |
| 141 | + // Nothing here |
| 142 | + this.AuthorisedSales = new ActivityType(0, 0, DateTime.MinValue); |
| 143 | + this.DeclinedSales = new ActivityType(0, 0, DateTime.MinValue); |
| 144 | + this.Deposits = new ActivityType(0, 0, DateTime.MinValue); |
| 145 | + this.Withdrawals = new ActivityType(0, 0, DateTime.MinValue); |
| 146 | + this.Fees = new ActivityType(0, 0, DateTime.MinValue); |
| 147 | + } |
| 148 | + |
| 149 | + private MerchantBalanceAggregate(Guid aggregateId) |
| 150 | + { |
| 151 | + Guard.ThrowIfInvalidGuid(aggregateId, "Aggregate Id cannot be an Empty Guid"); |
| 152 | + |
| 153 | + this.AggregateId = aggregateId; |
| 154 | + this.AuthorisedSales = new ActivityType(0, 0, DateTime.MinValue); |
| 155 | + this.DeclinedSales = new ActivityType(0, 0, DateTime.MinValue); |
| 156 | + this.Deposits = new ActivityType(0, 0, DateTime.MinValue); |
| 157 | + this.Withdrawals = new ActivityType(0, 0, DateTime.MinValue); |
| 158 | + this.Fees = new ActivityType(0, 0, DateTime.MinValue); |
| 159 | + } |
| 160 | + |
| 161 | + public static MerchantBalanceAggregate Create(Guid aggregateId) |
| 162 | + { |
| 163 | + return new MerchantBalanceAggregate(aggregateId); |
| 164 | + } |
| 165 | + |
| 166 | + public override void PlayEvent(IDomainEvent domainEvent) => MerchantBalanceAggregateExtensions.PlayEvent(this, (dynamic)domainEvent); |
| 167 | + |
| 168 | + [ExcludeFromCodeCoverage] |
| 169 | + protected override Object GetMetadata() |
| 170 | + { |
| 171 | + return new |
| 172 | + { |
| 173 | + EstateId = Guid.NewGuid() // TODO: Populate |
| 174 | + }; |
| 175 | + } |
| 176 | + } |
| 177 | +} |
0 commit comments