| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Models; |
| | | 3 | | using Elsa.Workflows.Api.Constants; |
| | | 4 | | using Elsa.Workflows.Api.Requirements; |
| | | 5 | | using Elsa.Workflows.Management; |
| | | 6 | | using Elsa.Workflows.Management.Filters; |
| | | 7 | | using JetBrains.Annotations; |
| | | 8 | | using Microsoft.AspNetCore.Authorization; |
| | | 9 | | |
| | | 10 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.BulkPublish; |
| | | 11 | | |
| | | 12 | | [PublicAPI] |
| | 1 | 13 | | internal class BulkPublish(IWorkflowDefinitionStore store, IWorkflowDefinitionPublisher workflowDefinitionPublisher, IAu |
| | | 14 | | : ElsaEndpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 1 | 18 | | Post("/bulk-actions/publish/workflow-definitions/by-definition-ids"); |
| | 1 | 19 | | ConfigurePermissions("publish:workflow-definitions"); |
| | 1 | 20 | | } |
| | | 21 | | |
| | | 22 | | public override async Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken) |
| | | 23 | | { |
| | 0 | 24 | | var authorizationResult = await authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(), Authorizati |
| | | 25 | | |
| | 0 | 26 | | if (!authorizationResult.Succeeded) |
| | | 27 | | { |
| | 0 | 28 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 29 | | return null!; |
| | | 30 | | } |
| | | 31 | | |
| | 0 | 32 | | var published = new List<string>(); |
| | 0 | 33 | | var notFound = new List<string>(); |
| | 0 | 34 | | var alreadyPublished = new List<string>(); |
| | 0 | 35 | | var skipped = new List<string>(); |
| | 0 | 36 | | var updatedConsumers = new List<string>(); |
| | | 37 | | |
| | 0 | 38 | | var definitions = (await store.FindManyAsync(new WorkflowDefinitionFilter |
| | 0 | 39 | | { |
| | 0 | 40 | | DefinitionIds = request.DefinitionIds, |
| | 0 | 41 | | VersionOptions = VersionOptions.Latest |
| | 0 | 42 | | }, cancellationToken: cancellationToken)) |
| | 0 | 43 | | .DistinctBy(x => x.DefinitionId) |
| | 0 | 44 | | .ToDictionary(x => x.DefinitionId); |
| | | 45 | | |
| | 0 | 46 | | foreach (var definitionId in request.DefinitionIds) |
| | | 47 | | { |
| | 0 | 48 | | if (!definitions.TryGetValue(definitionId, out var definition)) |
| | | 49 | | { |
| | 0 | 50 | | notFound.Add(definitionId); |
| | 0 | 51 | | continue; |
| | | 52 | | } |
| | | 53 | | |
| | 0 | 54 | | if (definition.IsPublished) |
| | | 55 | | { |
| | 0 | 56 | | alreadyPublished.Add(definitionId); |
| | 0 | 57 | | continue; |
| | | 58 | | } |
| | | 59 | | |
| | 0 | 60 | | if (definition.IsReadonly) |
| | | 61 | | { |
| | 0 | 62 | | skipped.Add(definitionId); |
| | 0 | 63 | | continue; |
| | | 64 | | } |
| | | 65 | | |
| | 0 | 66 | | var result = await workflowDefinitionPublisher.PublishAsync(definition, cancellationToken); |
| | 0 | 67 | | published.Add(definitionId); |
| | | 68 | | |
| | 0 | 69 | | if (result.AffectedWorkflows.WorkflowDefinitions.Count > 0) |
| | 0 | 70 | | updatedConsumers.AddRange(result.AffectedWorkflows.WorkflowDefinitions.Select(x => x.DefinitionId)); |
| | 0 | 71 | | } |
| | | 72 | | |
| | 0 | 73 | | return new(published, alreadyPublished, notFound, skipped, updatedConsumers); |
| | 0 | 74 | | } |
| | | 75 | | } |