< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowInstances.List.List
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/List/Endpoint.cs
Line coverage
5%
Covered lines: 5
Uncovered lines: 81
Coverable lines: 86
Total lines: 162
Line coverage: 5.8%
Branch coverage
0%
Covered branches: 0
Total branches: 50
Branch coverage: 0%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
Configure()100%11100%
HandleAsync()0%342180%
ParseEnumStrings()0%2040%
ValidateInput(...)0%342180%
FindAsync()0%110100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowInstances/List/Endpoint.cs

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Common.Entities;
 3using Elsa.Common.Models;
 4using Elsa.Workflows.Api.Models;
 5using Elsa.Workflows.Management;
 6using Elsa.Workflows.Management.Filters;
 7using Elsa.Workflows.Management.Models;
 8using JetBrains.Annotations;
 9using Microsoft.AspNetCore.Http;
 10
 11namespace Elsa.Workflows.Api.Endpoints.WorkflowInstances.List;
 12
 13[UsedImplicitly]
 114internal class List(IWorkflowInstanceStore store) : ElsaEndpoint<Request, Response>
 15{
 16    public override void Configure()
 17    {
 118        Verbs(FastEndpoints.Http.GET, FastEndpoints.Http.POST);
 119        Routes("/workflow-instances");
 120        ConfigurePermissions("read:workflow-instances");
 121    }
 22
 23    public override async Task HandleAsync(Request request, CancellationToken cancellationToken)
 24    {
 025        var pageArgs = PageArgs.FromPage(request.Page, request.PageSize);
 026        var workflowStatuses = request.Statuses?.Any() == true ? ParseEnumStrings<WorkflowStatus>(request.Statuses).ToLi
 027        var workflowSubStatuses = request.SubStatuses?.Any() == true ? ParseEnumStrings<WorkflowSubStatus>(request.SubSt
 28
 029        ValidateInput(request);
 30
 031        if (ValidationFailed)
 32        {
 033            await Send.ErrorsAsync(StatusCodes.Status400BadRequest, cancellationToken);
 034            return;
 35        }
 36
 037        var filter = new WorkflowInstanceFilter
 038        {
 039            IsSystem = request.IsSystem,
 040            SearchTerm = request.SearchTerm,
 041            Name = request.Name,
 042            DefinitionId = request.DefinitionId,
 043            DefinitionIds = request.DefinitionIds?.Any() == true ? request.DefinitionIds : null,
 044            Version = request.Version,
 045            CorrelationId = request.CorrelationId,
 046            WorkflowStatus = request.Status,
 047            WorkflowSubStatus = request.SubStatus,
 048            WorkflowStatuses = workflowStatuses,
 049            WorkflowSubStatuses = workflowSubStatuses,
 050            HasIncidents = request.HasIncidents,
 051            TimestampFilters = request.TimestampFilters?.Any() == true ? request.TimestampFilters : null,
 052        };
 53
 054        var summaries = await FindAsync(request, filter, pageArgs, cancellationToken);
 055        var response = new Response(summaries.Items, summaries.TotalCount);
 056        await Send.OkAsync(response, cancellationToken);
 057    }
 58
 59    private IEnumerable<TEnum> ParseEnumStrings<TEnum>(IEnumerable<string> strings) where TEnum : struct
 60    {
 061        foreach (string s in strings)
 62        {
 063            if (Enum.TryParse<TEnum>(s, true, out var result))
 064                yield return result;
 65            else
 66            {
 067                AddError($"Invalid enum value '{s}'.");
 068                yield break;
 69            }
 70        }
 071    }
 72
 73    private bool ValidateInput(Request request)
 74    {
 075        if (request.Page is < 0)
 76        {
 077            AddError("Page must be greater than or equal to 1.");
 078            return false;
 79        }
 80
 081        if (request.PageSize is < 1)
 82        {
 083            AddError("Page size must be greater than or equal to 1.");
 084            return false;
 85        }
 86
 087        var columnWhitelist = new[]
 088        {
 089            "CreatedAt", "UpdatedAt", "FinishedAt"
 090        };
 91
 092        if (request.TimestampFilters?.Any() == true)
 93        {
 094            foreach (var timestampFilter in request.TimestampFilters)
 95            {
 096                if (string.IsNullOrWhiteSpace(timestampFilter.Column))
 97                {
 098                    AddError("Column must be specified.");
 099                    return false;
 100                }
 101
 0102                if (!columnWhitelist.Contains(timestampFilter.Column))
 103                {
 0104                    AddError($"Invalid column '{timestampFilter.Column}'.");
 0105                    return false;
 106                }
 107            }
 108        }
 109
 0110        return true;
 0111    }
 112
 113    private async Task<Page<WorkflowInstanceSummary>> FindAsync(Request request, WorkflowInstanceFilter filter, PageArgs
 114    {
 0115        request.OrderBy ??= OrderByWorkflowInstance.Created;
 0116        var direction = request.OrderBy == OrderByWorkflowInstance.Name ? request.OrderDirection ?? OrderDirection.Ascen
 117
 0118        switch (request.OrderBy)
 119        {
 120            default:
 121                {
 0122                    var o = new WorkflowInstanceOrder<DateTimeOffset>
 0123                    {
 0124                        KeySelector = p => p.CreatedAt,
 0125                        Direction = direction
 0126                    };
 127
 0128                    return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken);
 129                }
 130            case OrderByWorkflowInstance.UpdatedAt:
 131                {
 0132                    var o = new WorkflowInstanceOrder<DateTimeOffset?>
 0133                    {
 0134                        KeySelector = p => p.UpdatedAt,
 0135                        Direction = direction
 0136                    };
 137
 0138                    return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken);
 139                }
 140            case OrderByWorkflowInstance.Finished:
 141                {
 0142                    var o = new WorkflowInstanceOrder<DateTimeOffset?>
 0143                    {
 0144                        KeySelector = p => p.FinishedAt,
 0145                        Direction = direction
 0146                    };
 147
 0148                    return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken);
 149                }
 150            case OrderByWorkflowInstance.Name:
 151                {
 0152                    var o = new WorkflowInstanceOrder<string>
 0153                    {
 0154                        KeySelector = p => p.Name!,
 0155                        Direction = direction
 0156                    };
 157
 0158                    return await store.SummarizeManyAsync(filter, pageArgs, o, cancellationToken);
 159                }
 160        }
 0161    }
 162}