|
| 1 | +using Shared.DomainDrivenDesign.EventSourcing; |
| 2 | +using Shared.EventStore.Aggregate; |
| 3 | +using SimpleResults; |
| 4 | +using System.Diagnostics.CodeAnalysis; |
| 5 | +using TransactionProcessor.DomainEvents; |
| 6 | +using MerchantScheduleModel = TransactionProcessor.Models.MerchantSchedule.MerchantSchedule; |
| 7 | +using MerchantScheduleMonthModel = TransactionProcessor.Models.MerchantSchedule.MerchantScheduleMonth; |
| 8 | + |
| 9 | +namespace TransactionProcessor.Aggregates |
| 10 | +{ |
| 11 | + public static class MerchantScheduleAggregateExtensions |
| 12 | + { |
| 13 | + public static Result Create(this MerchantScheduleAggregate aggregate, |
| 14 | + Guid estateId, |
| 15 | + Guid merchantId, |
| 16 | + Int32 year, |
| 17 | + IEnumerable<MerchantScheduleMonthModel>? months) |
| 18 | + { |
| 19 | + Result result = ValidateCreateArguments(aggregate, estateId, merchantId, year); |
| 20 | + if (result.IsFailed) |
| 21 | + return result; |
| 22 | + |
| 23 | + if (aggregate.IsCreated) |
| 24 | + return Result.Success(); |
| 25 | + |
| 26 | + MerchantScheduleDomainEvents.MerchantScheduleCreatedEvent merchantScheduleCreatedEvent = |
| 27 | + new(aggregate.AggregateId, estateId, merchantId, year); |
| 28 | + |
| 29 | + aggregate.ApplyAndAppend(merchantScheduleCreatedEvent); |
| 30 | + |
| 31 | + return aggregate.UpdateSchedule(months ?? []); |
| 32 | + } |
| 33 | + |
| 34 | + public static Result UpdateSchedule(this MerchantScheduleAggregate aggregate, |
| 35 | + IEnumerable<MerchantScheduleMonthModel> months) |
| 36 | + { |
| 37 | + Result result = aggregate.EnsureScheduleHasBeenCreated(); |
| 38 | + if (result.IsFailed) |
| 39 | + return result; |
| 40 | + |
| 41 | + List<MerchantScheduleMonthModel> monthList = months?.ToList() ?? []; |
| 42 | + if (monthList.GroupBy(m => m.Month).Any(g => g.Count() > 1)) |
| 43 | + return Result.Invalid("Each month can only be provided once when updating a merchant schedule"); |
| 44 | + |
| 45 | + foreach (MerchantScheduleMonthModel month in monthList.OrderBy(m => m.Month)) |
| 46 | + { |
| 47 | + Result monthResult = aggregate.SetMonthSchedule(month.Month, month.ClosedDays); |
| 48 | + if (monthResult.IsFailed) |
| 49 | + return monthResult; |
| 50 | + } |
| 51 | + |
| 52 | + return Result.Success(); |
| 53 | + } |
| 54 | + |
| 55 | + public static Result SetMonthSchedule(this MerchantScheduleAggregate aggregate, |
| 56 | + Int32 month, |
| 57 | + IEnumerable<Int32>? closedDays) |
| 58 | + { |
| 59 | + Result result = aggregate.EnsureScheduleHasBeenCreated(); |
| 60 | + if (result.IsFailed) |
| 61 | + return result; |
| 62 | + |
| 63 | + result = ValidateMonth(aggregate.Year, month, closedDays); |
| 64 | + if (result.IsFailed) |
| 65 | + return result; |
| 66 | + |
| 67 | + Int32[] normalisedClosedDays = NormaliseDays(closedDays); |
| 68 | + |
| 69 | + if (aggregate.Months.TryGetValue(month, out MerchantScheduleMonthModel? existingMonth)) |
| 70 | + { |
| 71 | + if (existingMonth.ClosedDays.SequenceEqual(normalisedClosedDays)) |
| 72 | + return Result.Success(); |
| 73 | + } |
| 74 | + |
| 75 | + MerchantScheduleDomainEvents.MerchantScheduleMonthUpdatedEvent merchantScheduleMonthUpdatedEvent = |
| 76 | + new(aggregate.AggregateId, aggregate.EstateId, aggregate.MerchantId, aggregate.Year, month, normalisedClosedDays); |
| 77 | + |
| 78 | + aggregate.ApplyAndAppend(merchantScheduleMonthUpdatedEvent); |
| 79 | + |
| 80 | + return Result.Success(); |
| 81 | + } |
| 82 | + |
| 83 | + public static MerchantScheduleModel GetSchedule(this MerchantScheduleAggregate aggregate) |
| 84 | + { |
| 85 | + MerchantScheduleModel model = new() |
| 86 | + { |
| 87 | + MerchantScheduleId = aggregate.AggregateId, |
| 88 | + EstateId = aggregate.EstateId, |
| 89 | + MerchantId = aggregate.MerchantId, |
| 90 | + Year = aggregate.Year, |
| 91 | + Months = [] |
| 92 | + }; |
| 93 | + |
| 94 | + foreach (MerchantScheduleMonthModel month in aggregate.Months.Values.OrderBy(m => m.Month)) |
| 95 | + { |
| 96 | + model.Months.Add(new MerchantScheduleMonthModel |
| 97 | + { |
| 98 | + Month = month.Month, |
| 99 | + ClosedDays = [.. month.ClosedDays] |
| 100 | + }); |
| 101 | + } |
| 102 | + |
| 103 | + return model; |
| 104 | + } |
| 105 | + |
| 106 | + public static void PlayEvent(this MerchantScheduleAggregate aggregate, |
| 107 | + MerchantScheduleDomainEvents.MerchantScheduleCreatedEvent domainEvent) |
| 108 | + { |
| 109 | + aggregate.EstateId = domainEvent.EstateId; |
| 110 | + aggregate.MerchantId = domainEvent.MerchantId; |
| 111 | + aggregate.Year = domainEvent.Year; |
| 112 | + aggregate.IsCreated = true; |
| 113 | + } |
| 114 | + |
| 115 | + public static void PlayEvent(this MerchantScheduleAggregate aggregate, |
| 116 | + MerchantScheduleDomainEvents.MerchantScheduleMonthUpdatedEvent domainEvent) |
| 117 | + { |
| 118 | + aggregate.Months[domainEvent.Month] = new MerchantScheduleMonthModel |
| 119 | + { |
| 120 | + Month = domainEvent.Month, |
| 121 | + ClosedDays = [.. domainEvent.ClosedDays] |
| 122 | + }; |
| 123 | + } |
| 124 | + |
| 125 | + private static Result EnsureScheduleHasBeenCreated(this MerchantScheduleAggregate aggregate) |
| 126 | + { |
| 127 | + if (aggregate.IsCreated == false) |
| 128 | + return Result.Invalid("Merchant schedule has not been created"); |
| 129 | + |
| 130 | + return Result.Success(); |
| 131 | + } |
| 132 | + |
| 133 | + private static Result ValidateCreateArguments(MerchantScheduleAggregate aggregate, |
| 134 | + Guid estateId, |
| 135 | + Guid merchantId, |
| 136 | + Int32 year) |
| 137 | + { |
| 138 | + if (aggregate.AggregateId == Guid.Empty) |
| 139 | + return Result.Invalid("Merchant schedule id must be provided"); |
| 140 | + |
| 141 | + if (estateId == Guid.Empty) |
| 142 | + return Result.Invalid("Estate id must be provided when creating a merchant schedule"); |
| 143 | + |
| 144 | + if (merchantId == Guid.Empty) |
| 145 | + return Result.Invalid("Merchant id must be provided when creating a merchant schedule"); |
| 146 | + |
| 147 | + if (year < 1900) |
| 148 | + return Result.Invalid("A valid year must be provided when creating a merchant schedule"); |
| 149 | + |
| 150 | + return Result.Success(); |
| 151 | + } |
| 152 | + |
| 153 | + private static Result ValidateMonth(Int32 year, |
| 154 | + Int32 month, |
| 155 | + IEnumerable<Int32>? closedDays) |
| 156 | + { |
| 157 | + if (month is < 1 or > 12) |
| 158 | + return Result.Invalid("A valid month must be provided when updating a merchant schedule"); |
| 159 | + |
| 160 | + Int32 daysInMonth = DateTime.DaysInMonth(year, month); |
| 161 | + Int32[] normalisedClosedDays = NormaliseDays(closedDays); |
| 162 | + |
| 163 | + if (normalisedClosedDays.Any(day => day < 1 || day > daysInMonth)) |
| 164 | + return Result.Invalid($"Only days between 1 and {daysInMonth} can be supplied for {year}-{month:D2}"); |
| 165 | + |
| 166 | + return Result.Success(); |
| 167 | + } |
| 168 | + |
| 169 | + private static Int32[] NormaliseDays(IEnumerable<Int32>? days) => |
| 170 | + (days ?? []).Distinct().OrderBy(day => day).ToArray(); |
| 171 | + } |
| 172 | + |
| 173 | + public record MerchantScheduleAggregate : Aggregate |
| 174 | + { |
| 175 | + internal readonly Dictionary<Int32, MerchantScheduleMonthModel> Months; |
| 176 | + |
| 177 | + [ExcludeFromCodeCoverage] |
| 178 | + public MerchantScheduleAggregate() |
| 179 | + { |
| 180 | + this.Months = new Dictionary<Int32, MerchantScheduleMonthModel>(); |
| 181 | + } |
| 182 | + |
| 183 | + private MerchantScheduleAggregate(Guid aggregateId) |
| 184 | + { |
| 185 | + if (aggregateId == Guid.Empty) |
| 186 | + throw new ArgumentException("Value cannot be empty.", nameof(aggregateId)); |
| 187 | + |
| 188 | + this.AggregateId = aggregateId; |
| 189 | + this.Months = new Dictionary<Int32, MerchantScheduleMonthModel>(); |
| 190 | + } |
| 191 | + |
| 192 | + public Guid EstateId { get; internal set; } |
| 193 | + |
| 194 | + public Guid MerchantId { get; internal set; } |
| 195 | + |
| 196 | + public Int32 Year { get; internal set; } |
| 197 | + |
| 198 | + public Boolean IsCreated { get; internal set; } |
| 199 | + |
| 200 | + public static MerchantScheduleAggregate Create(Guid aggregateId) |
| 201 | + { |
| 202 | + return new MerchantScheduleAggregate(aggregateId); |
| 203 | + } |
| 204 | + |
| 205 | + public override void PlayEvent(IDomainEvent domainEvent) => MerchantScheduleAggregateExtensions.PlayEvent(this, (dynamic)domainEvent); |
| 206 | + |
| 207 | + [ExcludeFromCodeCoverage] |
| 208 | + protected override Object GetMetadata() |
| 209 | + { |
| 210 | + return new |
| 211 | + { |
| 212 | + MerchantScheduleId = this.AggregateId, |
| 213 | + this.EstateId, |
| 214 | + this.MerchantId, |
| 215 | + this.Year |
| 216 | + }; |
| 217 | + } |
| 218 | + } |
| 219 | +} |
0 commit comments