Skip to content

Commit 6d7b433

Browse files
Merge pull request #784 from TransactionProcessing/bug/#783_statement_date_issues
Enhance error handling and testing for merchant services
2 parents e214e5f + 0362235 commit 6d7b433

5 files changed

Lines changed: 36 additions & 74 deletions

File tree

.github/workflows/pullrequest.yml

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -36,19 +36,6 @@ jobs:
3636
- name: Run Integration Tests
3737
run: dotnet test "TransactionProcessor.IntegrationTests\TransactionProcessor.IntegrationTests.csproj" --filter Category=PRTest --logger "trx;LogFileName=test-results.trx"
3838

39-
- name: Extract Failed Tests (PowerShell)
40-
if: ${{ failure() }}
41-
run: |
42-
$xml = [xml](Get-Content test-results.trx)
43-
$failedTests = $xml.TestRun.Results.UnitTestResult | Where-Object { $_.outcome -eq "Failed" }
44-
if ($failedTests) {
45-
Write-Host "::error::Failed Tests:"
46-
foreach ($test in $failedTests) {
47-
Write-Host "::error::- $($test.testName)"
48-
}
49-
}
50-
shell: pwsh
51-
5239
- uses: actions/upload-artifact@v4.4.0
5340
if: ${{ failure() }}
5441
with:

TransactionProcessor.BusinessLogic.Tests/DomainEventHandlers/TransactionDomainEventHandlerTests.cs

Lines changed: 0 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -21,63 +21,6 @@ namespace TransactionProcessor.BusinessLogic.Tests.DomainEventHandlers
2121
using Testing;
2222
using Xunit;
2323

24-
public class TestLogger : ILogger
25-
{
26-
private readonly ITestOutputHelper TestOutputHelper;
27-
28-
public TestLogger(ITestOutputHelper testOutputHelper) {
29-
this.TestOutputHelper = testOutputHelper;
30-
}
31-
public void LogCritical(string message)
32-
{
33-
this.TestOutputHelper.WriteLine(message);
34-
}
35-
36-
public void LogCritical(Exception exception) {
37-
this.TestOutputHelper.WriteLine(exception.Message);
38-
}
39-
40-
public void LogCritical(String message,
41-
Exception exception) {
42-
this.TestOutputHelper.WriteLine(message);
43-
this.TestOutputHelper.WriteLine(exception.Message);
44-
}
45-
46-
public void LogDebug(string message)
47-
{
48-
this.TestOutputHelper.WriteLine(message);
49-
}
50-
51-
public void LogError(Exception exception) {
52-
this.TestOutputHelper.WriteLine(exception.Message);
53-
}
54-
55-
public void LogError(String message,
56-
Exception exception) {
57-
this.TestOutputHelper.WriteLine(message);
58-
this.TestOutputHelper.WriteLine(exception.Message);
59-
}
60-
61-
public void LogError(string message)
62-
{
63-
this.TestOutputHelper.WriteLine(message);
64-
}
65-
public void LogInformation(string message)
66-
{
67-
this.TestOutputHelper.WriteLine(message);
68-
}
69-
public void LogTrace(string message)
70-
{
71-
this.TestOutputHelper.WriteLine(message);
72-
}
73-
public void LogWarning(string message)
74-
{
75-
this.TestOutputHelper.WriteLine(message);
76-
}
77-
78-
public Boolean IsInitialised { get; set; }
79-
}
80-
8124
[Collection("Sequential")]
8225
public abstract class DomainEventHandlerTests
8326
{
@@ -87,7 +30,6 @@ public DomainEventHandlerTests(ITestOutputHelper testOutputHelper) {
8730
this.Mediator = new Mock<IMediator>();
8831
IConfigurationRoot configurationRoot = new ConfigurationBuilder().AddInMemoryCollection(TestData.DefaultAppSettings).Build();
8932
ConfigurationReader.Initialise(configurationRoot);
90-
Logger.Initialise(new TestLogger(testOutputHelper));
9133
this.Mediator.Setup(s => s.Send(It.IsAny<IRequest<Result>>(), It.IsAny<CancellationToken>())).ReturnsAsync(Result.Success());
9234
}
9335
}

TransactionProcessor.BusinessLogic.Tests/Services/MerchantDomainServiceTests.cs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
using TransactionProcessor.Aggregates;
1919
using TransactionProcessor.BusinessLogic.Requests;
2020
using TransactionProcessor.BusinessLogic.Services;
21+
using TransactionProcessor.DomainEvents;
2122
using TransactionProcessor.Models.Contract;
2223
using TransactionProcessor.Models.Merchant;
2324
using TransactionProcessor.Testing;
@@ -1057,4 +1058,12 @@ public async Task MerchantDomainService_RemoveContractFromMerchant_ValidationFai
10571058
var result = await this.DomainService.RemoveContractFromMerchant(TestData.Commands.RemoveMerchantContractCommand, CancellationToken.None);
10581059
result.IsFailed.ShouldBeTrue();
10591060
}
1061+
1062+
[Fact]
1063+
public void Test1() {
1064+
var domainEvent = JsonConvert.DeserializeObject<SettlementDomainEvents.MerchantFeeSettledEvent>("{\r\n \"transactionId\": \"a4702780-03a3-4f9d-8ed0-5b5c88f276ba\",\r\n \"estateId\": \"435613ac-a468-47a3-ac4f-649d89764c22\",\r\n \"merchantId\": \"8bc8434d-41f9-4cc3-83bc-e73f20c02e1d\",\r\n \"calculatedValue\": 0.5,\r\n \"feeId\": \"957939ba-7bec-48b6-adb8-0277faf752ba\",\r\n \"feeValue\": 0.5,\r\n \"feeCalculatedDateTime\": \"2025-01-01T09:17:40\",\r\n \"settledDateTime\": \"2025-01-01T00:00:00\",\r\n \"settlementId\": \"0a4f96f9-0e58-9b43-2f0b-4bdca9367a3f\",\r\n \"transactionDateTime\": \"2025-01-01T09:17:40\",\t\t\t\r\n \"eventId\": \"435613ac-a468-47a3-ac4f-649d89764c22\"\r\n}");
1065+
1066+
var statementDate = MerchantStatementDomainService.CalculateStatementDate(domainEvent.FeeCalculatedDateTime);
1067+
statementDate.ShouldBe(new DateTime(2025,2,1));
1068+
}
10601069
}

TransactionProcessor.BusinessLogic/EventHandling/MerchantStatementDomainEventHandler.cs

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using System.Threading;
99
using System.Threading.Tasks;
1010
using Prometheus;
11+
using Shared.Logger;
1112
using TransactionProcessor.BusinessLogic.Common;
1213
using TransactionProcessor.BusinessLogic.Requests;
1314
using TransactionProcessor.BusinessLogic.Services;
@@ -92,15 +93,28 @@ private async Task<Result> HandleSpecificDomainEvent(TransactionDomainEvents.Tra
9293
CancellationToken cancellationToken) {
9394
MerchantStatementCommands.AddTransactionToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.CompletedDateTime, domainEvent.TransactionAmount, domainEvent.IsAuthorised, domainEvent.TransactionId);
9495

95-
return await this.Mediator.Send(command, cancellationToken);
96+
//return await this.Mediator.Send(command, cancellationToken);
97+
var result = await this.Mediator.Send(command, cancellationToken);
98+
if (result.Status == ResultStatus.CriticalError) {
99+
Logger.LogWarning($"Domain Events is {domainEvent}");
100+
return result;
101+
}
102+
return result;
96103
}
97104

98105
private async Task<Result> HandleSpecificDomainEvent(SettlementDomainEvents.MerchantFeeSettledEvent domainEvent,
99106
CancellationToken cancellationToken)
100107
{
101108
MerchantStatementCommands.AddSettledFeeToMerchantStatementCommand command = new(domainEvent.EstateId, domainEvent.MerchantId, domainEvent.FeeCalculatedDateTime, domainEvent.CalculatedValue, domainEvent.TransactionId, domainEvent.FeeId);
102109

103-
return await this.Mediator.Send(command, cancellationToken);
110+
//return await this.Mediator.Send(command, cancellationToken);
111+
var result = await this.Mediator.Send(command, cancellationToken);
112+
if (result.Status == ResultStatus.CriticalError)
113+
{
114+
Logger.LogWarning($"Domain Events is {domainEvent}");
115+
return result;
116+
}
117+
return result;
104118
}
105119

106120
#endregion

TransactionProcessor.BusinessLogic/Services/MerchantStatementDomainService.cs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
using System.IO.Abstractions;
1515
using System.Threading;
1616
using System.Threading.Tasks;
17+
using Google.Protobuf.Reflection;
1718
using TransactionProcessor.Aggregates;
1819
using TransactionProcessor.Aggregates.Models;
1920
using TransactionProcessor.BusinessLogic.Common;
@@ -119,6 +120,10 @@ public async Task<Result> AddSettledFeeToStatement(MerchantStatementCommands.Add
119120
// Work out the next statement date
120121
DateTime nextStatementDate = CalculateStatementDate(command.SettledDateTime);
121122

123+
if (nextStatementDate.Year == 1) {
124+
return Result.CriticalError($"Error in statement date generation Generated date is {nextStatementDate}");
125+
}
126+
122127
Guid merchantStatementId = IdGenerationService.GenerateMerchantStatementAggregateId(command.EstateId, command.MerchantId, nextStatementDate);
123128
Guid merchantStatementForDateId = IdGenerationService.GenerateMerchantStatementForDateAggregateId(command.EstateId, command.MerchantId, nextStatementDate, command.SettledDateTime);
124129
Guid settlementFeeId = GuidCalculator.Combine(command.TransactionId, command.SettledFeeId);
@@ -254,7 +259,7 @@ public async Task<Result> RecordActivityDateOnMerchantStatement(MerchantStatemen
254259

255260
merchantStatementAggregate.RecordActivityDateOnStatement(command.MerchantStatementId, command.StatementDate,
256261
command.EstateId, command.MerchantId,
257-
command.MerchantStatementForDateId, command.StatementDate);
262+
command.MerchantStatementForDateId, command.StatementActivityDate);
258263

259264
return Result.Success();
260265
}, command.MerchantStatementId, cancellationToken, false);
@@ -277,6 +282,11 @@ public async Task<Result> AddTransactionToStatement(MerchantStatementCommands.Ad
277282
// Work out the next statement date
278283
DateTime nextStatementDate = CalculateStatementDate(command.TransactionDateTime);
279284

285+
if (nextStatementDate.Year == 1)
286+
{
287+
return Result.CriticalError($"Error in statement date generation Generated date is {nextStatementDate}");
288+
}
289+
280290
Guid merchantStatementId = IdGenerationService.GenerateMerchantStatementAggregateId(command.EstateId, command.MerchantId, nextStatementDate);
281291
Guid merchantStatementForDateId = IdGenerationService.GenerateMerchantStatementForDateAggregateId(command.EstateId, command.MerchantId, nextStatementDate, command.TransactionDateTime);
282292

0 commit comments

Comments
 (0)