| | | 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 Elsa.Workflows.Management.Models; |
| | | 8 | | using JetBrains.Annotations; |
| | | 9 | | using Microsoft.AspNetCore.Authorization; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Version; |
| | | 12 | | |
| | | 13 | | [PublicAPI] |
| | 1 | 14 | | internal class RevertVersion(IWorkflowDefinitionManager workflowDefinitionManager, IAuthorizationService authorizationSe |
| | | 15 | | : ElsaEndpointWithoutRequest |
| | | 16 | | { |
| | | 17 | | public override void Configure() |
| | | 18 | | { |
| | 1 | 19 | | Post("workflow-definitions/{definitionId}/revert/{version}"); |
| | 1 | 20 | | ConfigurePermissions("publish:workflow-definitions"); |
| | 1 | 21 | | } |
| | | 22 | | |
| | | 23 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 24 | | { |
| | 0 | 25 | | var definitionId = Route<string>("definitionId")!; |
| | 0 | 26 | | var version = Route<int>("version"); |
| | | 27 | | |
| | 0 | 28 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 29 | | { |
| | 0 | 30 | | DefinitionId = definitionId, |
| | 0 | 31 | | VersionOptions = VersionOptions.SpecificVersion(version) |
| | 0 | 32 | | }; |
| | | 33 | | |
| | 0 | 34 | | var definition = await store.FindAsync(filter, cancellationToken); |
| | | 35 | | |
| | 0 | 36 | | if (definition == null) |
| | | 37 | | { |
| | 0 | 38 | | await Send.NotFoundAsync(cancellationToken); |
| | 0 | 39 | | return; |
| | | 40 | | } |
| | | 41 | | |
| | 0 | 42 | | var authorizationResult = await authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(definition), A |
| | | 43 | | |
| | 0 | 44 | | if (!authorizationResult.Succeeded) |
| | | 45 | | { |
| | 0 | 46 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 47 | | return; |
| | | 48 | | } |
| | | 49 | | |
| | 0 | 50 | | var newDefinition = await workflowDefinitionManager.RevertVersionAsync(definitionId, version, cancellationToken) |
| | 0 | 51 | | var newDefinitionSummary = WorkflowDefinitionSummary.FromDefinition(newDefinition); |
| | | 52 | | |
| | 0 | 53 | | await Send.CreatedAtAsync("GetWorkflowDefinitionById", new |
| | 0 | 54 | | { |
| | 0 | 55 | | id = newDefinition.Id |
| | 0 | 56 | | }, newDefinitionSummary, cancellation: cancellationToken); |
| | 0 | 57 | | } |
| | | 58 | | } |