| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Workflows.Management; |
| | | 4 | | using Elsa.Workflows.Runtime; |
| | | 5 | | using Elsa.Workflows.Runtime.Requests; |
| | | 6 | | using JetBrains.Annotations; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Dispatch; |
| | | 9 | | |
| | | 10 | | [UsedImplicitly] |
| | 1 | 11 | | internal class Endpoint(IWorkflowDefinitionService workflowDefinitionService, IWorkflowDispatcher workflowDispatcher, II |
| | | 12 | | { |
| | | 13 | | public override void Configure() |
| | | 14 | | { |
| | 1 | 15 | | Post("/workflow-definitions/{definitionId}/dispatch"); |
| | 1 | 16 | | ConfigurePermissions("exec:workflow-definitions"); |
| | 1 | 17 | | } |
| | | 18 | | |
| | | 19 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 20 | | { |
| | 0 | 21 | | var definitionId = request.DefinitionId; |
| | 0 | 22 | | var versionOptions = request.VersionOptions ?? VersionOptions.Published; |
| | 0 | 23 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, versionOptions, cancell |
| | | 24 | | |
| | 0 | 25 | | if(workflowGraph == null) |
| | | 26 | | { |
| | 0 | 27 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 28 | | return; |
| | | 29 | | } |
| | | 30 | | |
| | 0 | 31 | | var input = request.Input; |
| | | 32 | | |
| | 0 | 33 | | if(input != null && input is not IDictionary<string, object>) |
| | | 34 | | { |
| | 0 | 35 | | AddError("Input must be a dictionary."); |
| | 0 | 36 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 37 | | return; |
| | | 38 | | } |
| | | 39 | | |
| | 0 | 40 | | var instanceId = request.InstanceId ?? identityGenerator.GenerateId(); |
| | 0 | 41 | | var dispatchRequest = new DispatchWorkflowDefinitionRequest(workflowGraph.Workflow.Identity.Id) |
| | 0 | 42 | | { |
| | 0 | 43 | | Input = input as IDictionary<string, object>, |
| | 0 | 44 | | InstanceId = instanceId, |
| | 0 | 45 | | CorrelationId = request.CorrelationId, |
| | 0 | 46 | | TriggerActivityId = request.TriggerActivityId, |
| | 0 | 47 | | }; |
| | | 48 | | |
| | 0 | 49 | | var options = new DispatchWorkflowOptions |
| | 0 | 50 | | { |
| | 0 | 51 | | Channel = request.Channel |
| | 0 | 52 | | }; |
| | | 53 | | |
| | 0 | 54 | | var result = await workflowDispatcher.DispatchAsync(dispatchRequest, options, cancellationToken); |
| | | 55 | | |
| | 0 | 56 | | if(!result.Succeeded) |
| | | 57 | | { |
| | 0 | 58 | | var fault = result.Fault!; |
| | 0 | 59 | | AddError(fault.Message, fault.Code); |
| | 0 | 60 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 61 | | return; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | await Send.OkAsync(new Response(instanceId), cancellationToken); |
| | 0 | 65 | | } |
| | | 66 | | } |