| | | 1 | | using Elsa.Workflows.Runtime.Options; |
| | | 2 | | using Elsa.Workflows.Runtime.Requests; |
| | | 3 | | using Elsa.Workflows.Runtime.Responses; |
| | | 4 | | using Microsoft.Extensions.Options; |
| | | 5 | | |
| | | 6 | | namespace Elsa.Workflows.Runtime; |
| | | 7 | | |
| | | 8 | | /// <summary> |
| | | 9 | | /// Validates the workflow request before dispatching it to the workflow dispatcher. |
| | | 10 | | /// </summary> |
| | | 11 | | /// <param name="decoratedService">The workflow dispatcher to decorate.</param> |
| | | 12 | | /// <param name="dispatcherOptions">The workflow dispatcher options.</param> |
| | 329 | 13 | | public class ValidatingWorkflowDispatcher(IWorkflowDispatcher decoratedService, IOptions<WorkflowDispatcherOptions> disp |
| | | 14 | | { |
| | 352 | 15 | | private IWorkflowDispatcher DecoratedService { get; set; } = decoratedService; |
| | | 16 | | |
| | | 17 | | /// <inheritdoc /> |
| | | 18 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchWorkflowDefinitionRequest request, DispatchWorkflo |
| | | 19 | | { |
| | 23 | 20 | | if (!ValidateChannel(options?.Channel)) |
| | 0 | 21 | | return DispatchWorkflowResponse.UnknownChannel(); |
| | | 22 | | |
| | 23 | 23 | | return await DecoratedService.DispatchAsync(request, options, cancellationToken); |
| | 23 | 24 | | } |
| | | 25 | | |
| | | 26 | | /// <inheritdoc /> |
| | | 27 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchWorkflowInstanceRequest request, DispatchWorkflowO |
| | | 28 | | { |
| | 0 | 29 | | if (!ValidateChannel(options?.Channel)) |
| | 0 | 30 | | return DispatchWorkflowResponse.UnknownChannel(); |
| | | 31 | | |
| | 0 | 32 | | return await DecoratedService.DispatchAsync(request, options, cancellationToken); |
| | 0 | 33 | | } |
| | | 34 | | |
| | | 35 | | /// <inheritdoc /> |
| | | 36 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchTriggerWorkflowsRequest request, DispatchWorkflowO |
| | | 37 | | { |
| | 0 | 38 | | if (!ValidateChannel(options?.Channel)) |
| | 0 | 39 | | return DispatchWorkflowResponse.UnknownChannel(); |
| | | 40 | | |
| | 0 | 41 | | return await DecoratedService.DispatchAsync(request, options, cancellationToken); |
| | 0 | 42 | | } |
| | | 43 | | |
| | | 44 | | /// <inheritdoc /> |
| | | 45 | | public async Task<DispatchWorkflowResponse> DispatchAsync(DispatchResumeWorkflowsRequest request, DispatchWorkflowOp |
| | | 46 | | { |
| | 0 | 47 | | if (!ValidateChannel(options?.Channel)) |
| | 0 | 48 | | return DispatchWorkflowResponse.UnknownChannel(); |
| | | 49 | | |
| | 0 | 50 | | return await DecoratedService.DispatchAsync(request, options, cancellationToken); |
| | 0 | 51 | | } |
| | | 52 | | |
| | | 53 | | private bool ValidateChannel(string? channelName) |
| | | 54 | | { |
| | 23 | 55 | | return string.IsNullOrEmpty(channelName) || GetChannelExists(channelName); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | private bool GetChannelExists(string channelName) |
| | | 59 | | { |
| | 0 | 60 | | return dispatcherOptions.Value.Channels.Any(x => x.Name == channelName); |
| | | 61 | | } |
| | | 62 | | } |