Skip to content

Reduce MakeMerchantWithdrawal complexity in MerchantDomainService#1611

Merged
StuartFerguson merged 4 commits intomasterfrom
copilot/fix-merchant-withdrawal-complexity
Mar 13, 2026
Merged

Reduce MakeMerchantWithdrawal complexity in MerchantDomainService#1611
StuartFerguson merged 4 commits intomasterfrom
copilot/fix-merchant-withdrawal-complexity

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

MerchantDomainService.MakeMerchantWithdrawal exceeded the configured cyclomatic complexity limit, with aggregate loading, withdrawal preconditions, and balance checks all inlined into one method. This change separates those responsibilities while preserving the existing withdrawal flow.

  • Refactor withdrawal orchestration

    • Simplifies MakeMerchantWithdrawal to three steps:
      1. load and validate the merchant deposit aggregate
      2. validate available balance
      3. apply and persist the withdrawal
  • Extract aggregate loading / validation

    • Adds GetMerchantDepositListForWithdrawal(...) to encapsulate:
      • estate lookup
      • merchant lookup
      • estate/merchant validation
      • merchant deposit list lookup
      • deposit-list-created check
  • Extract balance validation

    • Adds ValidateWithdrawalBalance(...) to isolate projection lookup and insufficient-funds validation from the main withdrawal path.
  • Harden projection deserialization

    • Guards against invalid or null merchant balance projection payloads and returns a domain failure instead of relying on an exception path.

Example of the resulting method shape:

Result<MerchantDepositListAggregate> getDepositListResult =
    await this.GetMerchantDepositListForWithdrawal(command, cancellationToken);
if (getDepositListResult.IsFailed)
    return ResultHelpers.CreateFailure(getDepositListResult);

Result validateBalanceResult = await this.ValidateWithdrawalBalance(command, cancellationToken);
if (validateBalanceResult.IsFailed)
    return validateBalanceResult;

PositiveMoney amount = PositiveMoney.Create(Money.Create(command.RequestDto.Amount));
MerchantDepositListAggregate merchantDepositListAggregate = getDepositListResult.Data;

Result stateResult = merchantDepositListAggregate.MakeWithdrawal(command.RequestDto.WithdrawalDateTime, amount);

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 MakeMerchantWithdrawal --no-restore (dns block)
    • Triggering command: /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp /opt/hostedtoolcache/CodeQL/2.24.3/x64/codeql/csharp/tools/linux64/Semmle.Autobuild.CSharp (dns block)
    • Triggering command: /usr/bin/dotnet dotnet restore --no-dependencies /home/REDACTED/work/TransactionProcessor/TransactionProcessor/TransactionProcessor.sln --packages /tmp/codeql-scratch-7ae8d40573b4aa77/dbs/csharp/working/packages /p:DisableImplicitNuGetFallbackFolder=true --verbosity normal /p:TargetFrameworkRootPath=/tmp/codeql-scratch-7ae8d40573b4aa77/dbs/csharp/working/emptyFakeDotnetRoot /p:NetCoreTargetingPackRoot=/tmp/codeql-scratch-7ae8d40573b4aa77/dbs/csharp/working/emptyFakeDotnetRoot /p:AllowMissingPrunePackageData=true (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 MerchantDomainService::MakeMerchantWithdrawal has a cyclomatic complexity of 11 (limit is 8)</issue_title>
<issue_description>

Codacy ID: 62c7b6cad126bec37558f60391e19453

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

Message: Method MerchantDomainService::MakeMerchantWithdrawal has a cyclomatic complexity of 11 (limit is 8)

**Code Snippet:**
```csharp
        public async Task<Result> MakeMerchantWithdrawal(MerchantCommands.MakeMerchantWithdrawalCommand command, 
```

</issue_description>

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


📍 Connect Copilot coding agent with Jira, Azure Boards or Linear to delegate work to Copilot in one click without leaving your project management tool.

Copilot AI and others added 2 commits March 13, 2026 14:47
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Co-authored-by: StuartFerguson <16325469+StuartFerguson@users.noreply.github.com>
Copilot AI changed the title [WIP] [CODACY-ID:62c7b6cad126bec37558f60391e19453] Refactor MakeMerchantWithdrawal method Reduce MakeMerchantWithdrawal complexity in MerchantDomainService Mar 13, 2026
Copilot AI requested a review from StuartFerguson March 13, 2026 14:53
@StuartFerguson StuartFerguson marked this pull request as ready for review March 13, 2026 17:32
@StuartFerguson StuartFerguson merged commit 61aeb3c into master Mar 13, 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 MerchantDomainService::MakeMerchantWithdrawal has a cyclomatic complexity of 11 (limit is 8)

2 participants