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