| | | 1 | | using System.Text.Json; |
| | | 2 | | using Elsa.Abstractions; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Workflows.Activities; |
| | | 5 | | using Elsa.Workflows.Api.Constants; |
| | | 6 | | using Elsa.Workflows.Api.Models; |
| | | 7 | | using Elsa.Workflows.Api.Requirements; |
| | | 8 | | using Elsa.Workflows.Management; |
| | | 9 | | using Elsa.Workflows.Management.Mappers; |
| | | 10 | | using Elsa.Workflows.Management.Materializers; |
| | | 11 | | using Elsa.Workflows.Management.Models; |
| | | 12 | | using Elsa.Workflows.Models; |
| | | 13 | | using JetBrains.Annotations; |
| | | 14 | | using Medallion.Threading; |
| | | 15 | | using Microsoft.AspNetCore.Authorization; |
| | | 16 | | using Microsoft.AspNetCore.Http; |
| | | 17 | | |
| | | 18 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Post; |
| | | 19 | | |
| | | 20 | | [PublicAPI] |
| | 1 | 21 | | internal class Post( |
| | 1 | 22 | | IApiSerializer serializer, |
| | 1 | 23 | | IWorkflowDefinitionPublisher workflowDefinitionPublisher, |
| | 1 | 24 | | VariableDefinitionMapper variableDefinitionMapper, |
| | 1 | 25 | | IDistributedLockProvider distributedLockProvider, |
| | 1 | 26 | | IWorkflowDefinitionLinker linker, |
| | 1 | 27 | | IAuthorizationService authorizationService) |
| | | 28 | | : ElsaEndpoint<SaveWorkflowDefinitionRequest, LinkedWorkflowDefinitionModel> |
| | | 29 | | { |
| | | 30 | | public override void Configure() |
| | | 31 | | { |
| | 1 | 32 | | Post("/workflow-definitions"); |
| | 1 | 33 | | ConfigurePermissions("write:workflow-definitions"); |
| | 1 | 34 | | } |
| | | 35 | | |
| | | 36 | | public override async Task HandleAsync(SaveWorkflowDefinitionRequest request, CancellationToken cancellationToken) |
| | | 37 | | { |
| | 0 | 38 | | var model = request.Model; |
| | 0 | 39 | | var definitionId = model.DefinitionId; |
| | 0 | 40 | | var resourceName = $"{GetType().FullName}:{(!string.IsNullOrWhiteSpace(definitionId) ? definitionId : Guid.NewGu |
| | | 41 | | |
| | 0 | 42 | | await using var handle = await distributedLockProvider.AcquireLockAsync(resourceName, TimeSpan.FromMinutes(1), c |
| | | 43 | | |
| | 0 | 44 | | var draft = !string.IsNullOrWhiteSpace(definitionId) |
| | 0 | 45 | | ? await workflowDefinitionPublisher.GetDraftAsync(definitionId, VersionOptions.Latest, cancellationToken) |
| | 0 | 46 | | : null; |
| | | 47 | | |
| | 0 | 48 | | var isNew = draft == null; |
| | | 49 | | |
| | | 50 | | // Create a new workflow in case no existing definition was found. |
| | 0 | 51 | | if (isNew) |
| | | 52 | | { |
| | 0 | 53 | | draft = await workflowDefinitionPublisher.NewAsync(cancellationToken: cancellationToken); |
| | | 54 | | |
| | 0 | 55 | | if (!string.IsNullOrWhiteSpace(definitionId)) |
| | 0 | 56 | | draft.DefinitionId = definitionId; |
| | | 57 | | } |
| | | 58 | | |
| | 0 | 59 | | var authorizationResult = await authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(draft), Author |
| | | 60 | | |
| | 0 | 61 | | if (!authorizationResult.Succeeded) |
| | | 62 | | { |
| | 0 | 63 | | await Send.ForbiddenAsync(cancellationToken); |
| | 0 | 64 | | return; |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | // Update the draft with the received model. |
| | 0 | 68 | | var root = model.Root ?? new Sequence(); |
| | 0 | 69 | | var serializerOptions = serializer.GetOptions(); |
| | 0 | 70 | | var stringData = JsonSerializer.Serialize(root, serializerOptions); |
| | 0 | 71 | | var variables = variableDefinitionMapper.Map(model.Variables).ToList(); |
| | 0 | 72 | | var inputs = model.Inputs ?? new List<InputDefinition>(); |
| | 0 | 73 | | var outputs = model.Outputs ?? new List<OutputDefinition>(); |
| | 0 | 74 | | var outcomes = model.Outcomes ?? new List<string>(); |
| | | 75 | | |
| | 0 | 76 | | draft!.StringData = stringData; |
| | 0 | 77 | | draft.MaterializerName = JsonWorkflowMaterializer.MaterializerName; |
| | 0 | 78 | | draft.Name = model.Name?.Trim(); |
| | 0 | 79 | | draft.ToolVersion = model.ToolVersion; |
| | 0 | 80 | | draft.Description = model.Description?.Trim(); |
| | 0 | 81 | | draft.CustomProperties = model.CustomProperties ?? new Dictionary<string, object>(); |
| | 0 | 82 | | draft.Variables = variables; |
| | 0 | 83 | | draft.Inputs = inputs; |
| | 0 | 84 | | draft.Outputs = outputs; |
| | 0 | 85 | | draft.Outcomes = outcomes; |
| | 0 | 86 | | draft.Options = model.Options ?? new WorkflowOptions(); |
| | | 87 | | |
| | 0 | 88 | | PublishWorkflowDefinitionResult? result = null; |
| | | 89 | | |
| | 0 | 90 | | if (request.Publish == true) |
| | | 91 | | { |
| | 0 | 92 | | result = await workflowDefinitionPublisher.PublishAsync(draft, cancellationToken); |
| | | 93 | | |
| | 0 | 94 | | if (!result.Succeeded) |
| | | 95 | | { |
| | 0 | 96 | | foreach (var validationError in result.ValidationErrors) |
| | 0 | 97 | | AddError(validationError.Message); |
| | | 98 | | |
| | 0 | 99 | | await Send.ErrorsAsync(400, cancellationToken); |
| | 0 | 100 | | return; |
| | | 101 | | } |
| | | 102 | | } |
| | | 103 | | else |
| | | 104 | | { |
| | 0 | 105 | | await workflowDefinitionPublisher.SaveDraftAsync(draft, cancellationToken); |
| | | 106 | | } |
| | | 107 | | |
| | 0 | 108 | | var mappedDefinition = await linker.MapAsync(draft, cancellationToken); |
| | 0 | 109 | | var affectedWorkflows = result?.AffectedWorkflows?.WorkflowDefinitions ?? []; |
| | 0 | 110 | | var response = new Response(mappedDefinition, false, affectedWorkflows.Count); |
| | 0 | 111 | | await HttpContext.Response.WriteAsJsonAsync(response, serializerOptions, cancellationToken); |
| | 0 | 112 | | } |
| | | 113 | | } |