| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Workflows.Api.Security; |
| | | 4 | | using Elsa.Workflows.Management; |
| | | 5 | | using Elsa.Workflows.Runtime; |
| | | 6 | | using Elsa.Workflows.Runtime.Contracts; |
| | | 7 | | using Elsa.Workflows.Runtime.Requests; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.BulkDispatch; |
| | | 11 | | |
| | | 12 | | [PublicAPI] |
| | 3 | 13 | | internal class Endpoint( |
| | 3 | 14 | | IWorkflowDefinitionService workflowDefinitionService, |
| | 3 | 15 | | IWorkflowDispatcher workflowDispatcher, |
| | 3 | 16 | | IIdentityGenerator identityGenerator, |
| | 3 | 17 | | WorkflowDefinitionScriptAuthorizationService scriptAuthorizationService) |
| | | 18 | | : ElsaEndpoint<Request, Response> |
| | | 19 | | { |
| | | 20 | | public override void Configure() |
| | | 21 | | { |
| | 3 | 22 | | Post("/workflow-definitions/{definitionId}/bulk-dispatch"); |
| | 3 | 23 | | ConfigurePermissions("exec:workflow-definitions"); |
| | 3 | 24 | | } |
| | | 25 | | |
| | | 26 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 27 | | { |
| | 0 | 28 | | var definitionId = request.DefinitionId; |
| | 0 | 29 | | var versionOptions = request.VersionOptions ?? VersionOptions.Published; |
| | 0 | 30 | | var workflowGraph = await workflowDefinitionService.FindWorkflowGraphAsync(definitionId, versionOptions, cancell |
| | | 31 | | |
| | 0 | 32 | | if (workflowGraph == null) |
| | | 33 | | { |
| | 0 | 34 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 35 | | return; |
| | | 36 | | } |
| | | 37 | | |
| | 0 | 38 | | var scriptAuthorizationResult = await scriptAuthorizationService.AuthorizeAsync(workflowGraph.Workflow, User, ca |
| | 0 | 39 | | if (!scriptAuthorizationResult.Succeeded) |
| | | 40 | | { |
| | 0 | 41 | | await WorkflowDefinitionScriptAuthorizationFailure.SendAsync(scriptAuthorizationResult, Send.ForbiddenAsync, |
| | 0 | 42 | | return; |
| | | 43 | | } |
| | | 44 | | |
| | 0 | 45 | | var instanceIds = new List<string>(); |
| | | 46 | | |
| | 0 | 47 | | for (var i = 0; i < request.Count; i++) |
| | | 48 | | { |
| | 0 | 49 | | var instanceId = identityGenerator.GenerateId(); |
| | 0 | 50 | | var triggerActivityId = request.TriggerActivityId; |
| | 0 | 51 | | var input = (IDictionary<string, object>?)request.Input; |
| | 0 | 52 | | var dispatchRequest = new DispatchWorkflowDefinitionRequest(workflowGraph.Workflow.Identity.Id) |
| | 0 | 53 | | { |
| | 0 | 54 | | Input = input, |
| | 0 | 55 | | InstanceId = instanceId, |
| | 0 | 56 | | TriggerActivityId = triggerActivityId |
| | 0 | 57 | | }; |
| | | 58 | | |
| | 0 | 59 | | await workflowDispatcher.DispatchAsync(dispatchRequest, cancellationToken: cancellationToken); |
| | 0 | 60 | | instanceIds.Add(instanceId); |
| | 0 | 61 | | } |
| | | 62 | | |
| | 0 | 63 | | await Send.OkAsync(new Response(instanceIds), cancellationToken); |
| | 0 | 64 | | } |
| | | 65 | | |
| | | 66 | | } |