< Summary

Information
Class: Elsa.Alterations.AlterationHandlers.CancelActivityHandler
Assembly: Elsa.Alterations
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/AlterationHandlers/CancelActivityHandler.cs
Line coverage
0%
Covered lines: 0
Uncovered lines: 23
Coverable lines: 23
Total lines: 62
Line coverage: 0%
Branch coverage
0%
Covered branches: 0
Total branches: 14
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
HandleAsync(...)0%7280%
CancelAsync()0%620%
CancelAsync()100%210%
GetActivityExecutionContexts(...)0%2040%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Alterations/AlterationHandlers/CancelActivityHandler.cs

#LineLine coverage
 1using Elsa.Alterations.AlterationTypes;
 2using Elsa.Alterations.Core.Abstractions;
 3using Elsa.Alterations.Core.Contexts;
 4using Elsa.Extensions;
 5using Elsa.Workflows;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Alterations.AlterationHandlers;
 9
 10/// <summary>
 11/// Cancels an activity.
 12/// </summary>
 13[UsedImplicitly]
 14public class CancelActivityHandler : AlterationHandlerBase<CancelActivity>
 15{
 16    /// <inheritdoc />
 17    protected override ValueTask HandleAsync(AlterationContext context, CancelActivity alteration)
 18    {
 019        if (alteration.ActivityInstanceId == null && alteration.ActivityId == null)
 20        {
 021            context.Fail("Either ActivityInstanceId or ActivityId must be specified");
 022            return ValueTask.CompletedTask;
 23        }
 24
 025        var activityExecutionContexts = GetActivityExecutionContexts(context, alteration).ToList();
 26
 027        if (!activityExecutionContexts.Any())
 28        {
 029            context.Fail(
 030                alteration.ActivityInstanceId != null
 031                    ? $"Activity execution context with ID {alteration.ActivityInstanceId} not found"
 032                    : $"Activity execution contexts for activity with ID {alteration.ActivityId} not found");
 33
 034            return ValueTask.CompletedTask;
 35        }
 36
 037        context.Succeed(() => CancelAsync(activityExecutionContexts));
 038        return ValueTask.CompletedTask;
 39    }
 40
 41    private async Task CancelAsync(IEnumerable<ActivityExecutionContext> activityExecutionContexts)
 42    {
 043        foreach (var activityExecutionContext in activityExecutionContexts)
 044            await CancelAsync(activityExecutionContext);
 045    }
 46
 47    private async Task CancelAsync(ActivityExecutionContext activityExecutionContext)
 48    {
 049        await activityExecutionContext.CancelActivityAsync();
 050    }
 51
 52    private static IEnumerable<ActivityExecutionContext> GetActivityExecutionContexts(AlterationContext context, CancelA
 53    {
 054        var workflowExecutionContext = context.WorkflowExecutionContext;
 55
 056        return alteration.ActivityInstanceId != null
 057            ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Id == alteration.ActivityInstanceId)
 058            : alteration.ActivityId != null
 059                ? workflowExecutionContext.ActivityExecutionContexts.Where(x => x.Activity.Id == alteration.ActivityId)
 060                : Enumerable.Empty<ActivityExecutionContext>();
 61    }
 62}