Skip to content

Refactor ProcessSettlement to reduce method complexity#1648

Merged
StuartFerguson merged 2 commits intomasterfrom
copilot/reduce-complexity-process-settlement
Mar 14, 2026
Merged

Refactor ProcessSettlement to reduce method complexity#1648
StuartFerguson merged 2 commits intomasterfrom
copilot/reduce-complexity-process-settlement

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

SettlementDomainService.ProcessSettlement had accumulated aggregate loading, settlement state transitions, and transaction fee processing into a single high-branching method. This change breaks that flow into focused helpers while preserving the existing retry and failure behavior.

  • Refactor settlement orchestration

    • Moved the main ProcessSettlement body into ProcessSettlementInternal(...) so the public method is limited to policy execution and top-level exception handling.
    • Kept aggregate ID calculation, settlement lookup, merchant lookup, and early exit for missing settlement state unchanged.
  • Isolate settlement state preparation

    • Extracted settlement update logic into PrepareSettlementForProcessing(...).
    • Centralized the immediate-settlement path, fee discovery, settlement start-processing transition, and settlement aggregate save handling.
  • Isolate fee application to transactions

    • Extracted per-fee transaction processing into ProcessSettlementFees(...).
    • Preserved existing behavior for:
      • transaction aggregate retrieval failures
      • transaction save failures
      • per-transaction exception logging and aggregated failure reporting

Example of the resulting structure:

public async Task<Result<Guid>> ProcessSettlement(ProcessSettlementCommand command,
                                                  CancellationToken cancellationToken) {
    IAsyncPolicy<Result<Guid>> retryPolicy =
        PolicyFactory.CreatePolicy<Result<Guid>>("SettlementDomainService - ProcessSettlement");

    try {
        return await PolicyFactory.ExecuteWithPolicyAsync(
            () => this.ProcessSettlementInternal(command, cancellationToken),
            retryPolicy,
            "SettlementDomainService - ProcessSettlement");
    }
    catch (Exception ex) {
        return Result.Failure(ex.GetExceptionMessages());
    }
}

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • f.feedz.io
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter SettlementDomainService_ProcessSettlement -nologo (dns block)
    • Triggering command: /usr/bin/dotnet dotnet test /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.BusinessLogic.Tests/TransactionProcessor.BusinessLogic.Tests.csproj --filter SettlementDomainService_ProcessSettlement -p:RestoreIgnoreFailedSources=true -nologo (dns block)
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.24.2/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (dns block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

This section details on the original issue you should resolve

<issue_title>[Complexity] Method SettlementDomainService::ProcessSettlement has a cyclomatic complexity of 13 (limit is 8)</issue_title>
<issue_description>Codacy ID: d391070116051a5f8f455be7ba7044f

File: TransactionProcessor.BusinessLogic/Services/SettlementDomainService.cs
Line: 38
Rule: Complexity (Warning)
Commit: 5b8f3e64d731c7b181b225e9d4117fda74faa3ac

Message: Method SettlementDomainService::ProcessSettlement has a cyclomatic complexity of 13 (limit is 8)

**Code Snippet:**
```csharp
        public async Task<Result<Guid>> ProcessSettlement(SettlementCommands.ProcessSettlementCommand command, 
```

</issue_description>

Comments on the Issue (you are @copilot in this section)


🔒 GitHub Advanced Security automatically protects Copilot coding agent pull requests. You can protect all pull requests by enabling Advanced Security for your repositories. Learn more about Advanced Security.

Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Copilot AI changed the title [WIP] [Complexity] Fix cyclomatic complexity in ProcessSettlement method Refactor ProcessSettlement to reduce method complexity Mar 13, 2026
Copilot AI requested a review from StuartFerguson March 13, 2026 23:40
@StuartFerguson StuartFerguson marked this pull request as ready for review March 13, 2026 23:41
@StuartFerguson StuartFerguson merged commit 20cc2a9 into master Mar 14, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Complexity] Method SettlementDomainService::ProcessSettlement has a cyclomatic complexity of 13 (limit is 8)

2 participants