< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Dispatch.Endpoint
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Dispatch/Endpoint.cs
Line coverage
11%
Covered lines: 4
Uncovered lines: 31
Coverable lines: 35
Total lines: 66
Line coverage: 11.4%
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%11100%
Configure()100%11100%
HandleAsync()0%156120%

File(s)

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

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Common.Models;
 3using Elsa.Workflows.Management;
 4using Elsa.Workflows.Runtime;
 5using Elsa.Workflows.Runtime.Requests;
 6using JetBrains.Annotations;
 7
 8namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Dispatch;
 9
 10[UsedImplicitly]
 111internal class Endpoint(IWorkflowDefinitionService workflowDefinitionService, IWorkflowDispatcher workflowDispatcher, II
 12{
 13    public override void Configure()
 14    {
 115        Post("/workflow-definitions/{definitionId}/dispatch");
 116        ConfigurePermissions("exec:workflow-definitions");
 117    }
 18
 19    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 20    {
 021        var definitionId = request.DefinitionId;
 022        var versionOptions = request.VersionOptions ?? VersionOptions.Published;
 023        var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, versionOptions, cancell
 24
 025        if(workflowGraph == null)
 26        {
 027            await Send.NotFoundAsync(cancellationToken);
 028            return;
 29        }
 30
 031        var input = request.Input;
 32
 033        if(input != null && input is not IDictionary<string, object>)
 34        {
 035            AddError("Input must be a dictionary.");
 036            await Send.ErrorsAsync(cancellation: cancellationToken);
 037            return;
 38        }
 39
 040        var instanceId = request.InstanceId ?? identityGenerator.GenerateId();
 041        var dispatchRequest = new DispatchWorkflowDefinitionRequest(workflowGraph.Workflow.Identity.Id)
 042        {
 043            Input = input as IDictionary<string, object>,
 044            InstanceId = instanceId,
 045            CorrelationId = request.CorrelationId,
 046            TriggerActivityId = request.TriggerActivityId,
 047        };
 48
 049        var options = new DispatchWorkflowOptions
 050        {
 051            Channel = request.Channel
 052        };
 53
 054        var result = await workflowDispatcher.DispatchAsync(dispatchRequest, options, cancellationToken);
 55
 056        if(!result.Succeeded)
 57        {
 058            var fault = result.Fault!;
 059            AddError(fault.Message, fault.Code);
 060            await Send.ErrorsAsync(cancellation: cancellationToken);
 061            return;
 62        }
 63
 064        await Send.OkAsync(new Response(instanceId), cancellationToken);
 065    }
 66}