Skip to content

Reduce AssignOperatorToMerchant complexity in MerchantDomainService#1610

Merged
StuartFerguson merged 3 commits intomasterfrom
copilot/reduce-cyclomatic-complexity-assign-operator
Mar 13, 2026
Merged

Reduce AssignOperatorToMerchant complexity in MerchantDomainService#1610
StuartFerguson merged 3 commits intomasterfrom
copilot/reduce-cyclomatic-complexity-assign-operator

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Mar 13, 2026

MerchantDomainService.AssignOperatorToMerchant exceeded the configured cyclomatic complexity threshold, making the method harder to scan and maintain. This change narrows the public method to orchestration and moves operator-specific branching into focused private helpers.

  • Refactor

    • Extracted operator lookup / estate membership validation into GetOperatorAssignmentDetails(...)
    • Extracted operator rule checks into ValidateOperatorAssignment(...)
    • Kept the main method focused on aggregate retrieval, high-level validation, state transition, and persistence
  • Behavior preserved

    • Retains the original validation order:
      1. load estate + merchant
      2. validate estate + merchant state
      3. verify operator is configured for the estate
      4. load operator aggregate
      5. enforce required merchant/terminal numbers
      6. assign operator and save
  • Readability cleanup

    • Replaced inline branching with named helper methods
    • Clarified helper return contract by using a named tuple field and non-null fallback for the operator name on failure paths

Example of the new flow:

(Result validationResult, String operatorName) =
    await this.GetOperatorAssignmentDetails(command, estateAggregate, cancellationToken);

if (validationResult.IsFailed)
    return validationResult;

Result stateResult = merchantAggregate.AssignOperator(
    command.RequestDto.OperatorId,
    operatorName,
    command.RequestDto.MerchantNumber,
    command.RequestDto.TerminalNumber);

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 -p:RestoreIgnoreFailedSources=true --filter AssignOperatorToMerchant (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)
    • 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::AssignOperatorToMerchant has a cyclomatic complexity of 14 (limit is 8)</issue_title>
<issue_description>

Codacy ID: b51a0a395bd17a4feafb03f5c0ff3138

File: TransactionProcessor.BusinessLogic/Services/MerchantDomainService.cs
Line: 108
Rule: Complexity (Warning)
Commit: b53b61ccb0be078d0067c6404180c069a16f839b

Message: Method MerchantDomainService::AssignOperatorToMerchant has a cyclomatic complexity of 14 (limit is 8)

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

</issue_description>

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


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Copilot AI changed the title [WIP] [Complexity] Reduce cyclomatic complexity in AssignOperatorToMerchant Reduce AssignOperatorToMerchant complexity in MerchantDomainService Mar 13, 2026
Copilot AI requested a review from StuartFerguson March 13, 2026 13:10
@StuartFerguson StuartFerguson marked this pull request as ready for review March 13, 2026 13:41
@StuartFerguson
Copy link
Copy Markdown
Member

@copilot my integration tests failed can you explain why ?

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Mar 13, 2026

@copilot my integration tests failed can you explain why ?

The failure doesn’t look related to AssignOperatorToMerchant in 2d7a9a8. Build + unit tests passed; the only failing step was the integration test job. In run 23052318131, the failure happened during scenario setup at Given I have created the following estates, where TransactionProcessorClient.GetEstates(...) returned IsSuccess = False after 181.5s. That means the test never got as far as the operator-assignment steps. The same log also shows transient SQL Server pre-login handshake errors during container startup, so this looks more like an environment/read-model startup issue in the integration test stack than a regression from this refactor.

@StuartFerguson StuartFerguson merged commit 3f72c81 into master Mar 13, 2026
8 of 9 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::AssignOperatorToMerchant has a cyclomatic complexity of 14 (limit is 8)

2 participants