< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowInstances.BulkDelete.BulkDelete
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/BulkDelete/Endpoint.cs
Line coverage
100%
Covered lines: 38
Uncovered lines: 0
Coverable lines: 38
Total lines: 65
Line coverage: 100%
Branch coverage
100%
Covered branches: 4
Total branches: 4
Branch coverage: 100%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Configure()100%11100%
ExecuteAsync()100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/BulkDelete/Endpoint.cs

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Workflows.Management;
 3using Elsa.Workflows.Management.Filters;
 4using Elsa.Workflows.Runtime;
 5using JetBrains.Annotations;
 6
 7namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.BulkDelete;
 8
 9[PublicAPI]
 310internal class BulkDelete(IWorkflowInstanceStore workflowInstanceStore, IWorkflowInstanceManager workflowInstanceManager
 11{
 12    public override void Configure()
 13    {
 114        Post(
 115            "/bulk-actions/delete/workflow-instances",
 116            "/bulk-actions/delete/workflow-instances/by-id" // Deprecated route.
 117        );
 118        ConfigurePermissions("delete:workflow-instances");
 119    }
 20
 21    public override async Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken)
 22    {
 223        var baseFilter = new WorkflowInstanceFilter
 224        {
 225            Ids = request.Ids,
 226            DefinitionId = request.WorkflowDefinitionId,
 227            DefinitionIds = request.WorkflowDefinitionIds,
 228        };
 29
 30        // Step 1: Delete running instances individually (requires coordination by the workflow runtime).
 231        var runningFilter = new WorkflowInstanceFilter
 232        {
 233            Ids = baseFilter.Ids,
 234            DefinitionId = baseFilter.DefinitionId,
 235            DefinitionIds = baseFilter.DefinitionIds,
 236            WorkflowStatus = WorkflowStatus.Running
 237        };
 38
 239        var runningInstanceIds = await workflowInstanceStore.FindManyIdsAsync(runningFilter, cancellationToken);
 240        var count = 0L;
 41
 842        foreach (var instanceId in runningInstanceIds)
 43        {
 244            var client = await workflowRuntime.CreateClientAsync(instanceId, cancellationToken);
 245            var deleted = await client.DeleteAsync(cancellationToken);
 246            if (deleted)
 247                count++;
 48        }
 49
 50        // Step 2: Bulk delete finished instances (no coordination needed).
 51        // Use IWorkflowInstanceManager to ensure related records are also deleted.
 252        var finishedFilter = new WorkflowInstanceFilter
 253        {
 254            Ids = baseFilter.Ids,
 255            DefinitionId = baseFilter.DefinitionId,
 256            DefinitionIds = baseFilter.DefinitionIds,
 257            WorkflowStatus = WorkflowStatus.Finished
 258        };
 59
 260        var finishedDeletedCount = await workflowInstanceManager.BulkDeleteAsync(finishedFilter, cancellationToken);
 261        count += finishedDeletedCount;
 62
 263        return new(count);
 264    }
 65}