| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.SasTokens.Contracts; |
| | | 3 | | using Elsa.Workflows.Runtime; |
| | | 4 | | using FastEndpoints; |
| | | 5 | | using JetBrains.Annotations; |
| | | 6 | | using Microsoft.AspNetCore.Http; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Workflows.Api.Endpoints.Bookmarks.Resume; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// Resumes a bookmarked workflow instance with the bookmark ID specified in the provided SAS token. |
| | | 12 | | /// </summary> |
| | | 13 | | [PublicAPI] |
| | 1 | 14 | | internal class Resume(ITokenService tokenService, IWorkflowResumer workflowResumer, IBookmarkQueue bookmarkQueue, IPaylo |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | public override void Configure() |
| | | 18 | | { |
| | 1 | 19 | | Routes("/bookmarks/resume"); |
| | 1 | 20 | | Verbs(Http.GET, Http.POST); |
| | 1 | 21 | | AllowAnonymous(); |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 26 | | { |
| | 0 | 27 | | var token = Query<string>("t")!; |
| | 0 | 28 | | var asynchronous = Query<bool>("async", false); |
| | | 29 | | |
| | 0 | 30 | | if (!tokenService.TryDecryptToken<BookmarkTokenPayload>(token, out var payload)) |
| | 0 | 31 | | AddError("Invalid token."); |
| | | 32 | | |
| | 0 | 33 | | var input = HttpContext.Request.Method == HttpMethods.Post ? request.Input : GetInputFromQueryString(); |
| | | 34 | | |
| | 0 | 35 | | if (ValidationFailed) |
| | | 36 | | { |
| | 0 | 37 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 38 | | return; |
| | | 39 | | } |
| | | 40 | | |
| | | 41 | | // Some clients, like Blazor, may prematurely cancel their request upon navigation away from the page. |
| | | 42 | | // In this case, we don't want to cancel the workflow execution. |
| | | 43 | | // We need to better understand the conditions that cause this. |
| | 0 | 44 | | var workflowCancellationToken = CancellationToken.None; |
| | 0 | 45 | | await ResumeBookmarkedWorkflowAsync(payload, input, asynchronous, workflowCancellationToken); |
| | | 46 | | |
| | 0 | 47 | | if (!HttpContext.Response.HasStarted) |
| | 0 | 48 | | await Send.OkAsync(cancellationToken); |
| | 0 | 49 | | } |
| | | 50 | | |
| | | 51 | | private IDictionary<string, object>? GetInputFromQueryString() |
| | | 52 | | { |
| | 0 | 53 | | var inputJson = Query<string?>("in", false); |
| | 0 | 54 | | if (string.IsNullOrWhiteSpace(inputJson)) |
| | 0 | 55 | | return null; |
| | | 56 | | |
| | | 57 | | try |
| | | 58 | | { |
| | 0 | 59 | | return serializer.Deserialize<IDictionary<string, object>>(inputJson); |
| | | 60 | | } |
| | 0 | 61 | | catch |
| | | 62 | | { |
| | 0 | 63 | | AddError("Invalid input format. Expected a valid JSON string."); |
| | 0 | 64 | | return null; |
| | | 65 | | } |
| | 0 | 66 | | } |
| | | 67 | | |
| | | 68 | | private async Task ResumeBookmarkedWorkflowAsync(BookmarkTokenPayload tokenPayload, IDictionary<string, object>? inp |
| | | 69 | | { |
| | 0 | 70 | | var bookmarkId = tokenPayload.BookmarkId; |
| | 0 | 71 | | var workflowInstanceId = tokenPayload.WorkflowInstanceId; |
| | | 72 | | |
| | 0 | 73 | | if (asynchronous) |
| | | 74 | | { |
| | 0 | 75 | | var item = new NewBookmarkQueueItem |
| | 0 | 76 | | { |
| | 0 | 77 | | BookmarkId = bookmarkId, |
| | 0 | 78 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 79 | | Options = new() |
| | 0 | 80 | | { |
| | 0 | 81 | | Input = input |
| | 0 | 82 | | } |
| | 0 | 83 | | }; |
| | | 84 | | |
| | 0 | 85 | | await bookmarkQueue.EnqueueAsync(item, cancellationToken); |
| | 0 | 86 | | return; |
| | | 87 | | } |
| | | 88 | | |
| | 0 | 89 | | var resumeRequest = new ResumeBookmarkRequest |
| | 0 | 90 | | { |
| | 0 | 91 | | BookmarkId = bookmarkId, |
| | 0 | 92 | | WorkflowInstanceId = workflowInstanceId, |
| | 0 | 93 | | Input = input |
| | 0 | 94 | | }; |
| | | 95 | | |
| | 0 | 96 | | await workflowResumer.ResumeAsync(resumeRequest, cancellationToken); |
| | 0 | 97 | | } |
| | | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <summary> |
| | | 101 | | /// The request model for the Resume endpoint. |
| | | 102 | | /// </summary> |
| | | 103 | | internal class Request |
| | | 104 | | { |
| | | 105 | | /// <summary> |
| | | 106 | | /// The input to provide to the workflow when resuming. |
| | | 107 | | /// </summary> |
| | | 108 | | public IDictionary<string, object>? Input { get; set; } |
| | | 109 | | } |