| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.SasTokens.Contracts; |
| | | 3 | | using Elsa.Workflows.Runtime; |
| | | 4 | | using JetBrains.Annotations; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Api.Endpoints.Events.TriggerPublic; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Resumes a workflow instance blocked by a specified event encoded in the provided SAS token. |
| | | 10 | | /// </summary> |
| | | 11 | | [PublicAPI] |
| | | 12 | | internal class Trigger : ElsaEndpointWithoutRequest |
| | | 13 | | { |
| | | 14 | | private readonly ITokenService _tokenService; |
| | | 15 | | private readonly IEventPublisher _eventPublisher; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | 1 | 18 | | public Trigger(ITokenService tokenService, IEventPublisher eventPublisher) |
| | | 19 | | { |
| | 1 | 20 | | _tokenService = tokenService; |
| | 1 | 21 | | _eventPublisher = eventPublisher; |
| | 1 | 22 | | } |
| | | 23 | | |
| | | 24 | | /// <inheritdoc /> |
| | | 25 | | public override void Configure() |
| | | 26 | | { |
| | 1 | 27 | | Get("/events/trigger"); |
| | 1 | 28 | | AllowAnonymous(); |
| | 1 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public override async Task HandleAsync(CancellationToken cancellationToken) |
| | | 33 | | { |
| | 0 | 34 | | var token = Query<string>("t")!; |
| | | 35 | | |
| | 0 | 36 | | if (!_tokenService.TryDecryptToken<EventTokenPayload>(token, out var payload)) |
| | | 37 | | { |
| | 0 | 38 | | AddError("Invalid token."); |
| | 0 | 39 | | await Send.ErrorsAsync(cancellation: cancellationToken); |
| | 0 | 40 | | return; |
| | | 41 | | } |
| | | 42 | | |
| | 0 | 43 | | var eventName = payload.EventName; |
| | 0 | 44 | | var workflowInstanceId = payload.WorkflowInstanceId; |
| | 0 | 45 | | await _eventPublisher.PublishAsync(eventName, workflowInstanceId: workflowInstanceId, cancellationToken: cancell |
| | | 46 | | |
| | 0 | 47 | | if (!HttpContext.Response.HasStarted) |
| | 0 | 48 | | await Send.OkAsync(cancellationToken); |
| | 0 | 49 | | } |
| | | 50 | | } |