| | | 1 | | using Elsa.Abstractions; |
| | | 2 | | using Elsa.Common.Entities; |
| | | 3 | | using Elsa.Common.Models; |
| | | 4 | | using Elsa.Workflows.Api.Models; |
| | | 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.Http; |
| | | 10 | | |
| | | 11 | | namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.List; |
| | | 12 | | |
| | | 13 | | [UsedImplicitly] |
| | 1 | 14 | | internal class List(IWorkflowInstanceStore store) : ElsaEndpoint<Request, Response> |
| | | 15 | | { |
| | | 16 | | public override void Configure() |
| | | 17 | | { |
| | 1 | 18 | | Verbs(FastEndpoints.Http.GET, FastEndpoints.Http.POST); |
| | 1 | 19 | | Routes("/workflow-instances"); |
| | 1 | 20 | | ConfigurePermissions("read:workflow-instances"); |
| | 1 | 21 | | } |
| | | 22 | | |
| | | 23 | | public override async Task HandleAsync(Request request, CancellationToken cancellationToken) |
| | | 24 | | { |
| | 0 | 25 | | var pageArgs = PageArgs.FromPage(request.Page, request.PageSize); |
| | 0 | 26 | | var workflowStatuses = request.Statuses?.Any() == true ? ParseEnumStrings<WorkflowStatus>(request.Statuses).ToLi |
| | 0 | 27 | | var workflowSubStatuses = request.SubStatuses?.Any() == true ? ParseEnumStrings<WorkflowSubStatus>(request.SubSt |
| | | 28 | | |
| | 0 | 29 | | ValidateInput(request); |
| | | 30 | | |
| | 0 | 31 | | if (ValidationFailed) |
| | | 32 | | { |
| | 0 | 33 | | await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken); |
| | 0 | 34 | | return; |
| | | 35 | | } |
| | | 36 | | |
| | 0 | 37 | | var filter = new WorkflowInstanceFilter |
| | 0 | 38 | | { |
| | 0 | 39 | | IsSystem = request.IsSystem, |
| | 0 | 40 | | SearchTerm = request.SearchTerm, |
| | 0 | 41 | | Name = request.Name, |
| | 0 | 42 | | DefinitionId = request.DefinitionId, |
| | 0 | 43 | | DefinitionIds = request.DefinitionIds?.Any() == true ? request.DefinitionIds : null, |
| | 0 | 44 | | Version = request.Version, |
| | 0 | 45 | | CorrelationId = request.CorrelationId, |
| | 0 | 46 | | WorkflowStatus = request.Status, |
| | 0 | 47 | | WorkflowSubStatus = request.SubStatus, |
| | 0 | 48 | | WorkflowStatuses = workflowStatuses, |
| | 0 | 49 | | WorkflowSubStatuses = workflowSubStatuses, |
| | 0 | 50 | | HasIncidents = request.HasIncidents, |
| | 0 | 51 | | TimestampFilters = request.TimestampFilters?.Any() == true ? request.TimestampFilters : null, |
| | 0 | 52 | | }; |
| | | 53 | | |
| | 0 | 54 | | var summaries = await FindAsync(request, filter, pageArgs, cancellationToken); |
| | 0 | 55 | | var response = new Response(summaries.Items, summaries.TotalCount); |
| | 0 | 56 | | await Send.OkAsync(response, cancellationToken); |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | private IEnumerable<TEnum> ParseEnumStrings<TEnum>(IEnumerable<string> strings) where TEnum : struct |
| | | 60 | | { |
| | 0 | 61 | | foreach (string s in strings) |
| | | 62 | | { |
| | 0 | 63 | | if (Enum.TryParse<TEnum>(s, true, out var result)) |
| | 0 | 64 | | yield return result; |
| | | 65 | | else |
| | | 66 | | { |
| | 0 | 67 | | AddError($"Invalid enum value '{s}'."); |
| | 0 | 68 | | yield break; |
| | | 69 | | } |
| | | 70 | | } |
| | 0 | 71 | | } |
| | | 72 | | |
| | | 73 | | private bool ValidateInput(Request request) |
| | | 74 | | { |
| | 0 | 75 | | if (request.Page is < 0) |
| | | 76 | | { |
| | 0 | 77 | | AddError("Page must be greater than or equal to 1."); |
| | 0 | 78 | | return false; |
| | | 79 | | } |
| | | 80 | | |
| | 0 | 81 | | if (request.PageSize is < 1) |
| | | 82 | | { |
| | 0 | 83 | | AddError("Page size must be greater than or equal to 1."); |
| | 0 | 84 | | return false; |
| | | 85 | | } |
| | | 86 | | |
| | 0 | 87 | | var columnWhitelist = new[] |
| | 0 | 88 | | { |
| | 0 | 89 | | "CreatedAt", "UpdatedAt", "FinishedAt" |
| | 0 | 90 | | }; |
| | | 91 | | |
| | 0 | 92 | | if (request.TimestampFilters?.Any() == true) |
| | | 93 | | { |
| | 0 | 94 | | foreach (var timestampFilter in request.TimestampFilters) |
| | | 95 | | { |
| | 0 | 96 | | if (string.IsNullOrWhiteSpace(timestampFilter.Column)) |
| | | 97 | | { |
| | 0 | 98 | | AddError("Column must be specified."); |
| | 0 | 99 | | return false; |
| | | 100 | | } |
| | | 101 | | |
| | 0 | 102 | | if (!columnWhitelist.Contains(timestampFilter.Column)) |
| | | 103 | | { |
| | 0 | 104 | | AddError($"Invalid column '{timestampFilter.Column}'."); |
| | 0 | 105 | | return false; |
| | | 106 | | } |
| | | 107 | | } |
| | | 108 | | } |
| | | 109 | | |
| | 0 | 110 | | return true; |
| | 0 | 111 | | } |
| | | 112 | | |
| | | 113 | | private async Task<Page<WorkflowInstanceSummary>> FindAsync(Request request, WorkflowInstanceFilter filter, PageArgs |
| | | 114 | | { |
| | 0 | 115 | | request.OrderBy ??= OrderByWorkflowInstance.Created; |
| | 0 | 116 | | var direction = request.OrderBy == OrderByWorkflowInstance.Name ? request.OrderDirection ?? OrderDirection.Ascen |
| | | 117 | | |
| | 0 | 118 | | switch (request.OrderBy) |
| | | 119 | | { |
| | | 120 | | default: |
| | | 121 | | { |
| | 0 | 122 | | var o = new WorkflowInstanceOrder<DateTimeOffset> |
| | 0 | 123 | | { |
| | 0 | 124 | | KeySelector = p => p.CreatedAt, |
| | 0 | 125 | | Direction = direction |
| | 0 | 126 | | }; |
| | | 127 | | |
| | 0 | 128 | | return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken); |
| | | 129 | | } |
| | | 130 | | case OrderByWorkflowInstance.UpdatedAt: |
| | | 131 | | { |
| | 0 | 132 | | var o = new WorkflowInstanceOrder<DateTimeOffset?> |
| | 0 | 133 | | { |
| | 0 | 134 | | KeySelector = p => p.UpdatedAt, |
| | 0 | 135 | | Direction = direction |
| | 0 | 136 | | }; |
| | | 137 | | |
| | 0 | 138 | | return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken); |
| | | 139 | | } |
| | | 140 | | case OrderByWorkflowInstance.Finished: |
| | | 141 | | { |
| | 0 | 142 | | var o = new WorkflowInstanceOrder<DateTimeOffset?> |
| | 0 | 143 | | { |
| | 0 | 144 | | KeySelector = p => p.FinishedAt, |
| | 0 | 145 | | Direction = direction |
| | 0 | 146 | | }; |
| | | 147 | | |
| | 0 | 148 | | return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken); |
| | | 149 | | } |
| | | 150 | | case OrderByWorkflowInstance.Name: |
| | | 151 | | { |
| | 0 | 152 | | var o = new WorkflowInstanceOrder<string> |
| | 0 | 153 | | { |
| | 0 | 154 | | KeySelector = p => p.Name!, |
| | 0 | 155 | | Direction = direction |
| | 0 | 156 | | }; |
| | | 157 | | |
| | 0 | 158 | | return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken); |
| | | 159 | | } |
| | | 160 | | } |
| | 0 | 161 | | } |
| | | 162 | | } |