| | | 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.BulkRetract; |
| | | 11 | | |
| | | 12 | | [PublicAPI] |
| | 1 | 13 | | internal class BulkRetract(IWorkflowDefinitionStore store, IWorkflowDefinitionPublisher workflowDefinitionPublisher, IAu |
| | | 14 | | : ElsaEndpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 1 | 18 | | Post("/bulk-actions/retract/workflow-definitions/by-definition-ids"); |
| | 1 | 19 | | ConfigurePermissions("retract: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 retracted = new List<string>(); |
| | 0 | 33 | | var notFound = new List<string>(); |
| | 0 | 34 | | var notPublished = new List<string>(); |
| | 0 | 35 | | var skipped = new List<string>(); |
| | | 36 | | |
| | 0 | 37 | | foreach (var definitionId in request.DefinitionIds) |
| | | 38 | | { |
| | 0 | 39 | | var definitions = (await store.FindManyAsync(new WorkflowDefinitionFilter |
| | 0 | 40 | | { |
| | 0 | 41 | | DefinitionId = definitionId, |
| | 0 | 42 | | VersionOptions = VersionOptions.LatestOrPublished |
| | 0 | 43 | | }, cancellationToken: cancellationToken)).ToList(); |
| | | 44 | | |
| | 0 | 45 | | if (!definitions.Any()) |
| | | 46 | | { |
| | 0 | 47 | | notFound.Add(definitionId); |
| | 0 | 48 | | continue; |
| | | 49 | | } |
| | | 50 | | |
| | 0 | 51 | | var published = definitions.FirstOrDefault(d => d.IsPublished); |
| | 0 | 52 | | if (published is null) |
| | | 53 | | { |
| | 0 | 54 | | notPublished.Add(definitionId); |
| | 0 | 55 | | continue; |
| | | 56 | | } |
| | | 57 | | |
| | 0 | 58 | | if (published.IsReadonly) |
| | | 59 | | { |
| | 0 | 60 | | skipped.Add(definitionId); |
| | 0 | 61 | | continue; |
| | | 62 | | } |
| | | 63 | | |
| | 0 | 64 | | await workflowDefinitionPublisher.RetractAsync(published, cancellationToken); |
| | 0 | 65 | | retracted.Add(definitionId); |
| | 0 | 66 | | } |
| | | 67 | | |
| | 0 | 68 | | return new Response(retracted, notPublished, notFound, skipped); |
| | 0 | 69 | | } |
| | | 70 | | } |