< Summary

Information
Class: Elsa.Labels.Services.WorkflowDefinitionLabelFilterProvider
Assembly: Elsa.Labels
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Labels/Services/WorkflowDefinitionLabelFilterProvider.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 17
Coverable lines: 17
Total lines: 46
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 12
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%210%
CanApply(...)0%620%
GetRequiredPermissions(...)0%620%
ApplyAsync()0%7280%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Labels/Services/WorkflowDefinitionLabelFilterProvider.cs

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Labels.Contracts;
 3using Elsa.Labels.Permissions;
 4using Elsa.Workflows.Management;
 5using Elsa.Workflows.Management.Exceptions;
 6using Elsa.Workflows.Management.Filters;
 7
 8namespace Elsa.Labels.Services;
 9
 10/// <summary>
 11/// Applies label relationships to workflow definition filters. A version qualifies when it has any requested label ID; 
 12/// </summary>
 013public class WorkflowDefinitionLabelFilterProvider(IWorkflowDefinitionLabelStore store) : IWorkflowDefinitionFilterProvi
 14{
 15    /// <inheritdoc />
 016    public bool CanApply(WorkflowDefinitionFilter filter) => filter.LabelIds is { Count: > 0 };
 17
 18    /// <summary>Requires workflow-definition-label view permission when a label filter is present.</summary>
 19    public IEnumerable<Permission> GetRequiredPermissions(WorkflowDefinitionFilter filter) =>
 020        CanApply(filter) ? [new(LabelPermissions.WorkflowDefinitionLabels, CoreVerbs.View)] : [];
 21
 22    /// <inheritdoc />
 23    public async Task ApplyAsync(WorkflowDefinitionFilter filter, CancellationToken cancellationToken = default)
 24    {
 025        if (filter.LabelIds is not { Count: > 0 })
 26        {
 027            return;
 28        }
 29
 030        if (store is not IWorkflowDefinitionLabelQuery query)
 31        {
 032            throw new WorkflowDefinitionFilterNotSupportedException("Filtering workflow definitions by labels is not sup
 33        }
 34
 035        var labelIds = filter.LabelIds.Distinct().ToList();
 036        var matchingVersionIds = (await query.FindByLabelIdsAsync(labelIds, cancellationToken))
 037            .Select(x => x.WorkflowDefinitionVersionId)
 038            .Distinct()
 039            .ToList();
 40
 041        filter.Ids = filter.Ids == null
 042            ? matchingVersionIds
 043            : filter.Ids.Intersect(matchingVersionIds).ToList();
 044        filter.LabelIds = null;
 045    }
 46}