< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Graph.Segments.Nodes
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Graph/Segments/Endpoint.cs
Line coverage
12%
Covered lines: 4
Uncovered lines: 28
Coverable lines: 32
Total lines: 68
Line coverage: 12.5%
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
.ctor(...)100%11100%
Configure()100%11100%
HandleAsync()0%342180%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Graph/Segments/Endpoint.cs

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Extensions;
 3using Elsa.Workflows.Activities;
 4using Elsa.Workflows.Activities.Flowchart.Activities;
 5using Elsa.Workflows.Api.Models;
 6using Elsa.Workflows.Management;
 7using Elsa.Workflows.Serialization.Converters;
 8using Elsa.Workflows.Serialization.Helpers;
 9using JetBrains.Annotations;
 10using Microsoft.AspNetCore.Http;
 11
 12namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Graph.Segments;
 13
 14[PublicAPI]
 115internal class Nodes(IWorkflowDefinitionService workflowDefinitionService, IApiSerializer apiSerializer, ActivityWriter 
 16{
 17    public override void Configure()
 18    {
 119        Get("/workflow-definitions/subgraph/segments/{id}");
 120        ConfigurePermissions("read:workflow-definitions");
 121    }
 22
 23    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 24    {
 025        var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(request.Id, cancellationToken);
 26
 027        if (workflowGraph?.Root == null)
 28        {
 029            await Send.NotFoundAsync(cancellationToken);
 030            return;
 31        }
 32
 033        var nodeId = request.ChildNodeId;
 34
 035        if (!workflowGraph.NodeIdLookup.TryGetValue(nodeId, out var childNode))
 36        {
 037            AddError("Unknown node ID");
 038            await Send.ErrorsAsync(cancellation: cancellationToken);
 039            return;
 40        }
 41
 042        var ancestors = childNode.Ancestors().ToList();
 043        var serializerOptions = apiSerializer.GetOptions().Clone().WithConverters(new RootActivityNodeConverter(activity
 044        var segments = new Stack<ActivityPathSegment>();
 045        var currentNode = ancestors.FirstOrDefault();
 046        var previousNode = childNode;
 47
 048        while (currentNode != null)
 49        {
 050            if (currentNode.Activity is not Workflow and not Flowchart)
 51            {
 052                var currentActivity = currentNode.Activity;
 053                var currentPort = previousNode.Port;
 054                var activityName = currentActivity.Name ?? currentActivity.Type;
 055                var segment = new ActivityPathSegment(currentNode.NodeId, currentActivity.Id, currentActivity.Type, curr
 056                segments.Push(segment);
 57            }
 58
 059            previousNode = currentNode;
 060            currentNode = currentNode.Parents.FirstOrDefault();
 61        }
 62
 063        var leafSegment = segments.LastOrDefault();
 064        var container = leafSegment == null ? ancestors.LastOrDefault() ?? childNode : ancestors.Last(x => x.Activity.Id
 065        var response = new Response(childNode, container, segments.ToList());
 066        await HttpContext.Response.WriteAsJsonAsync(response, serializerOptions, cancellationToken);
 067    }
 68}