Skip to content

Commit ae03d87

Browse files
Merge pull request #435 from TransactionProcessing/task/#432_recordprocesssettlementcall
Record process settlement calls
2 parents 3db7d0a + 3de4103 commit ae03d87

File tree

5 files changed

+70
-2
lines changed

5 files changed

+70
-2
lines changed

TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
{
33
using System;
44
using System.Collections.Generic;
5+
using System.Linq;
56
using System.Threading;
67
using System.Threading.Tasks;
78
using Common;
@@ -38,7 +39,12 @@ public async Task<ProcessSettlementResponse> ProcessSettlement(DateTime settleme
3839
List<(Guid transactionId, Guid merchantId, CalculatedFee calculatedFee)> feesToBeSettled = settlementAggregate.GetFeesToBeSettled();
3940
response.NumberOfFeesPendingSettlement = feesToBeSettled.Count;
4041

41-
42+
if (feesToBeSettled.Any()){
43+
// Record the process call
44+
settlementAggregate.StartProcessing(DateTime.Now);
45+
await this.SettlementAggregateRepository.SaveChanges(settlementAggregate, cancellationToken);
46+
}
47+
4248

4349
foreach ((Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) feeToSettle in feesToBeSettled)
4450
{
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
namespace TransactionProcessor.Settlement.DomainEvents;
2+
3+
using System;
4+
using Shared.DomainDrivenDesign.EventSourcing;
5+
6+
public record SettlementProcessingStartedEvent(Guid SettlementId,
7+
Guid EstateId,
8+
Guid MerchantId,
9+
DateTime ProcessingStartedDateTime) : DomainEvent(SettlementId, Guid.NewGuid());

TransactionProcessor.SettlementAggregates.Tests/SettlementAggregateTests.cs

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -190,5 +190,36 @@ public void SettlementAggregate_ImmediatelyMarkFeeAsSettled_FeeIsSettledAndSettl
190190
aggregate.SettlementComplete.ShouldBeFalse();
191191
}
192192

193+
[Fact]
194+
public void SettlementAggregate_StartProcessing_ProcessingStarted()
195+
{
196+
SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId);
197+
aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate);
198+
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime);
199+
200+
aggregate.ProcessingStarted.ShouldBeTrue();
201+
aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTime);
202+
}
203+
204+
[Fact]
205+
public void SettlementAggregate_StartProcessing_CalledTwice_ProcessingStarted(){
206+
SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId);
207+
aggregate.Create(TestData.EstateId, TestData.MerchantId, TestData.SettlementDate);
208+
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime);
209+
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTimeSecondCall);
210+
211+
aggregate.ProcessingStarted.ShouldBeTrue();
212+
aggregate.ProcessingStartedDateTime.ShouldBe(TestData.SettlementProcessingStartedDateTimeSecondCall);
213+
}
214+
215+
[Fact]
216+
public void SettlementAggregate_StartProcessing_SettlementNotCreated_ErrorThron()
217+
{
218+
SettlementAggregate aggregate = SettlementAggregate.Create(TestData.SettlementAggregateId);
219+
220+
Should.Throw<InvalidOperationException>(() => {
221+
aggregate.StartProcessing(TestData.SettlementProcessingStartedDateTime);
222+
});
223+
}
193224
}
194225
}

TransactionProcessor.SettlementAggregates/SettlementAggregate.cs

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@
1111
using Shared.General;
1212

1313
public static class SettlementAggregateExtensions{
14+
public static void StartProcessing(this SettlementAggregate aggregate, DateTime dateTime){
15+
16+
aggregate.CheckHasBeenCreated();
17+
18+
SettlementProcessingStartedEvent startedEvent = new SettlementProcessingStartedEvent(aggregate.AggregateId,
19+
aggregate.EstateId,
20+
aggregate.MerchantId,
21+
dateTime);
22+
aggregate.ApplyAndAppend(startedEvent);
23+
}
24+
1425
public static void MarkFeeAsSettled(this SettlementAggregate aggregate, Guid merchantId, Guid transactionId, Guid feeId)
1526
{
1627
(Guid transactionId, Guid merchantId, CalculatedFee calculatedFee) pendingFee = SettlementAggregateExtensions.GetPendingFee(aggregate, merchantId, transactionId, feeId);
@@ -220,6 +231,11 @@ public static void PlayEvent(this SettlementAggregate aggregate, SettlementCompl
220231
{
221232
aggregate.SettlementComplete = true;
222233
}
234+
235+
public static void PlayEvent(this SettlementAggregate aggregate, SettlementProcessingStartedEvent domainEvent){
236+
aggregate.ProcessingStarted= true;
237+
aggregate.ProcessingStartedDateTime = domainEvent.ProcessingStartedDateTime;
238+
}
223239
}
224240

225241
public record SettlementAggregate : Aggregate
@@ -274,10 +290,13 @@ private SettlementAggregate(Guid aggregateId)
274290

275291
public Boolean SettlementComplete { get; internal set; }
276292

293+
public Boolean ProcessingStarted { get; internal set; }
294+
295+
public DateTime ProcessingStartedDateTime { get; internal set; }
277296
#endregion
278297

279298
#region Methods
280-
299+
281300
/// <summary>
282301
/// Creates the specified aggregate identifier.
283302
/// </summary>

TransactionProcessor.Testing/TestData.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1187,6 +1187,9 @@ public static SettlementAggregate GetSettlementAggregateWithNotAllFeesSettled(In
11871187

11881188
public static Int32 EstateReportingId = 1;
11891189

1190+
public static DateTime SettlementProcessingStartedDateTime = new DateTime(2023,7,17,11,12,20);
1191+
public static DateTime SettlementProcessingStartedDateTimeSecondCall = new DateTime(2023, 7, 17, 11, 12, 40);
1192+
11901193
public static RedeemVoucherResponse RedeemVoucherResponse =>
11911194
new RedeemVoucherResponse
11921195
{

0 commit comments

Comments
 (0)