< 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
18%
Covered lines: 8
Uncovered lines: 35
Coverable lines: 43
Total lines: 80
Line coverage: 18.6%
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%210140%

File(s)

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

#LineLine coverage
 1using Elsa.Authorization;
 2using Elsa.Abstractions;
 3using Elsa.Common.Models;
 4using Elsa.Workflows.Api.Security;
 5using Elsa.Workflows.Management;
 6using Elsa.Workflows.Runtime;
 7using Elsa.Workflows.Runtime.Requests;
 8using JetBrains.Annotations;
 9
 10namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Dispatch;
 11
 12[UsedImplicitly]
 313internal class Endpoint(
 314    IWorkflowDefinitionService workflowDefinitionService,
 315    IWorkflowDispatcher workflowDispatcher,
 316    IIdentityGenerator identityGenerator,
 317    WorkflowDefinitionScriptAuthorizationService scriptAuthorizationService) : ElsaEndpoint<Request, Response>
 18{
 19    public override void Configure()
 20    {
 321        Post("/workflow-definitions/{definitionId}/dispatch");
 322        RequirePermission(Elsa.Workflows.Api.Permissions.WorkflowPermissions.Definitions, CoreVerbs.Execute);
 323    }
 24
 25    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 26    {
 027        var definitionId = request.DefinitionId;
 028        var versionOptions = request.VersionOptions ?? VersionOptions.Published;
 029        var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, versionOptions, cancell
 30
 031        if(workflowGraph == null)
 32        {
 033            await Send.NotFoundAsync(cancellationToken);
 034            return;
 35        }
 36
 037        var scriptAuthorizationResult = await scriptAuthorizationService.AuthorizeAsync(workflowGraph.Workflow, cancella
 038        if (!scriptAuthorizationResult.Succeeded)
 39        {
 040            await WorkflowDefinitionScriptAuthorizationFailure.SendAsync(scriptAuthorizationResult, message => AddError(
 041            return;
 42        }
 43
 044        var input = request.Input;
 45
 046        if(input != null && input is not IDictionary<string, object>)
 47        {
 048            AddError("Input must be a dictionary.");
 049            await Send.ErrorsAsync(cancellation: cancellationToken);
 050            return;
 51        }
 52
 053        var instanceId = request.InstanceId ?? identityGenerator.GenerateId();
 054        var dispatchRequest = new DispatchWorkflowDefinitionRequest(workflowGraph.Workflow.Identity.Id)
 055        {
 056            Input = input as IDictionary<string, object>,
 057            InstanceId = instanceId,
 058            CorrelationId = request.CorrelationId,
 059            TriggerActivityId = request.TriggerActivityId,
 060        };
 61
 062        var options = new DispatchWorkflowOptions
 063        {
 064            Channel = request.Channel
 065        };
 66
 067        var result = await workflowDispatcher.DispatchAsync(dispatchRequest, options, cancellationToken);
 68
 069        if(!result.Succeeded)
 70        {
 071            var fault = result.Fault!;
 072            AddError(fault.Message, fault.Code);
 073            await Send.ErrorsAsync(cancellation: cancellationToken);
 074            return;
 75        }
 76
 077        await Send.OkAsync(new Response(instanceId), cancellationToken);
 078    }
 79
 80}