< Summary

Information
Class: Elsa.Workflows.Api.Services.StaticWorkflowDefinitionLinker
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Services/StaticWorkflowDefinitionLinker.cs
Line coverage
39%
Covered lines: 45
Uncovered lines: 68
Coverable lines: 113
Total lines: 165
Line coverage: 39.8%
Branch coverage
40%
Covered branches: 4
Total branches: 10
Branch coverage: 40%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
MapAsync()100%11100%
MapAsync(...)0%620%
MapAsync()0%620%
GenerateLinksForPagedList()0%620%
GenerateLinksForSingleEntry(...)100%44100%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Services/StaticWorkflowDefinitionLinker.cs

#LineLine coverage
 1using Elsa.Models;
 2using Elsa.Workflows.Api.Models;
 3using Elsa.Workflows.Management.Entities;
 4using Elsa.Workflows.Management.Mappers;
 5using Elsa.Workflows.Management.Models;
 6using Elsa.Workflows.Management.Options;
 7using Microsoft.Extensions.Options;
 8
 9namespace Elsa.Workflows.Api.Services;
 10
 11///  <inheritdoc/>
 212public class StaticWorkflowDefinitionLinker(
 213    IOptions<ManagementOptions> managementOptions,
 214    WorkflowDefinitionMapper workflowDefinitionMapper)
 15    : IWorkflowDefinitionLinker
 16{
 17    /// <inheritdoc />
 18    public async Task<LinkedWorkflowDefinitionModel> MapAsync(WorkflowDefinition definition, CancellationToken cancellat
 19    {
 120        var workflowDefinitionModel = await workflowDefinitionMapper.MapAsync(definition, cancellationToken);
 121        var linkedModel = new LinkedWorkflowDefinitionModel(GenerateLinksForSingleEntry(definition.DefinitionId, definit
 122        {
 123            Id = workflowDefinitionModel.Id,
 124            DefinitionId = workflowDefinitionModel.DefinitionId,
 125            Name = workflowDefinitionModel.Name,
 126            Description = workflowDefinitionModel.Description,
 127            CreatedAt = workflowDefinitionModel.CreatedAt,
 128            Version = workflowDefinitionModel.Version,
 129            ToolVersion = workflowDefinitionModel.ToolVersion,
 130            Variables = workflowDefinitionModel.Variables,
 131            Inputs = workflowDefinitionModel.Inputs,
 132            Outputs = workflowDefinitionModel.Outputs,
 133            Outcomes = workflowDefinitionModel.Outcomes,
 134            CustomProperties = workflowDefinitionModel.CustomProperties,
 135            IsReadonly = workflowDefinitionModel.IsReadonly,
 136            IsSystem = workflowDefinitionModel.IsSystem,
 137            IsLatest = workflowDefinitionModel.IsLatest,
 138            IsPublished = workflowDefinitionModel.IsPublished,
 139            Options = workflowDefinitionModel.Options,
 140            UsableAsActivity = workflowDefinitionModel.UsableAsActivity,
 141            Root = workflowDefinitionModel.Root
 142        };
 43
 144        return linkedModel;
 145    }
 46
 47    /// <inheritdoc />
 48    public PagedListResponse<LinkedWorkflowDefinitionSummary> MapAsync(PagedListResponse<WorkflowDefinitionSummary> list
 49    {
 050        var items = new List<LinkedWorkflowDefinitionSummary>();
 51
 052        foreach (var item in list.Items)
 53        {
 054            items.Add(new LinkedWorkflowDefinitionSummary
 055            {
 056                Links = GenerateLinksForSingleEntry(item.DefinitionId, item.IsReadonly),
 057                Id = item.Id,
 058                DefinitionId = item.DefinitionId,
 059                Name = item.Name,
 060                Description = item.Description,
 061                Version = item.Version,
 062                ToolVersion = item.ToolVersion,
 063                IsLatest = item.IsLatest,
 064                IsPublished = item.IsPublished,
 065                ProviderName = item.ProviderName,
 066                MaterializerName = item.MaterializerName,
 067                CreatedAt = item.CreatedAt,
 068                IsReadonly = item.IsReadonly
 069            });
 70        }
 71
 072        return new PagedListResponse<LinkedWorkflowDefinitionSummary>
 073        {
 074            TotalCount = list.TotalCount,
 075            Items = items,
 076            Links = GenerateLinksForPagedList().ToArray()
 077        };
 78    }
 79
 80    /// <inheritdoc />
 81    public async Task<List<LinkedWorkflowDefinitionModel>> MapAsync(List<WorkflowDefinition> definitions, CancellationTo
 82    {
 083        var models = (await workflowDefinitionMapper.MapAsync(definitions, cancellationToken)).ToList();
 084        var result = new List<LinkedWorkflowDefinitionModel>();
 85
 086        foreach (var item in models)
 87        {
 088            var linkedModel = new LinkedWorkflowDefinitionModel(GenerateLinksForSingleEntry(item.DefinitionId, item.IsRe
 089            {
 090                Id = item.Id,
 091                DefinitionId = item.DefinitionId,
 092                Name = item.Name,
 093                Description = item.Description,
 094                CreatedAt = item.CreatedAt,
 095                Version = item.Version,
 096                ToolVersion = item.ToolVersion,
 097                Variables = item.Variables,
 098                Inputs = item.Inputs,
 099                Outputs = item.Outputs,
 0100                Outcomes = item.Outcomes,
 0101                CustomProperties = item.CustomProperties,
 0102                IsReadonly = item.IsReadonly,
 0103                IsSystem = item.IsSystem,
 0104                IsLatest = item.IsLatest,
 0105                IsPublished = item.IsPublished,
 0106                Options = item.Options,
 0107                UsableAsActivity = item.UsableAsActivity,
 0108                Root = item.Root
 0109            };
 110
 0111            result.Add(linkedModel);
 112        }
 113
 0114        return result;
 0115    }
 116
 117    private Link[] GenerateLinksForPagedList()
 118    {
 0119        var linksList = new List<Link>
 0120        {
 0121            new($"/workflow-definitions", "self", "GET"),
 0122            new($"/workflow-definitions/validation/is-name-unique", "is-name-unique", "GET"),
 0123            new($"/workflow-definitions/query/count", "count", "GET"),
 0124            new($"/workflow-definitions/many-by-id", "many-by-id", "GET")
 0125        };
 126
 0127        if (!managementOptions.Value.IsReadOnlyMode)
 128        {
 0129            linksList.Add(new Link($"/bulk-actions/delete/workflow-definitions/by-definition-id", "bulk-delete-by-defini
 0130            linksList.Add(new Link($"/bulk-actions/delete/workflow-definitions/by-id", "bulk-delete-by-id", "POST"));
 0131            linksList.Add(new Link($"/bulk-actions/publish/workflow-definitions/by-definition-ids", "bulk-publish", "POS
 0132            linksList.Add(new Link($"/bulk-actions/retract/workflow-definitions/by-definition-ids", "bulk-retract", "POS
 0133            linksList.Add(new Link($"/workflow-definitions/import", "import", "POST"));
 0134            linksList.Add(new Link($"/workflow-definitions/import-files", "import-files", "POST"));
 0135            linksList.Add(new Link($"/workflow-definitions", "create", "POST"));
 136        }
 137
 0138        return linksList.ToArray();
 139    }
 140
 141    private Link[] GenerateLinksForSingleEntry(string definitionId, bool definitionIsReadonly)
 142    {
 1143        var links = new List<Link>
 1144        {
 1145            new($"/workflow-definitions/{definitionId}", "self", "GET"),
 1146            new($"/workflow-definitions/by-definition-id/{definitionId}", "self", "GET"),
 1147            new($"/workflow-definitions/{definitionId}/versions", "versions", "GET"),
 1148            new($"/workflow-definitions/{definitionId}/bulk-dispatch", "bulk-dispatch", "POST"),
 1149            new($"/workflow-definitions/{definitionId}/dispatch", "dispatch", "POST"),
 1150            new($"/workflow-definitions/{definitionId}/execute", "execute", "POST"),
 1151            new($"/workflow-definitions/{definitionId}/export", "export", "POST")
 1152        };
 153
 1154        if (!managementOptions.Value.IsReadOnlyMode && !definitionIsReadonly)
 155        {
 1156            links.Add(new Link($"/workflow-definitions/{definitionId}/publish", "publish", "POST"));
 1157            links.Add(new Link($"/workflow-definitions/{definitionId}/retract", "retract", "POST"));
 1158            links.Add(new Link($"/workflow-definitions/{definitionId}", "delete", "DELETE"));
 1159            links.Add(new Link($"/workflow-definitions/{definitionId}/import", "import", "PUT"));
 1160            links.Add(new Link($"/workflow-definitions/{definitionId}/update-references", "update-references", "POST"));
 161        }
 162
 1163        return links.ToArray();
 164    }
 165}