< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.BulkPublish.BulkPublish
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/BulkPublish/Endpoint.cs
Line coverage
10%
Covered lines: 4
Uncovered lines: 33
Coverable lines: 37
Total lines: 75
Line coverage: 10.8%
Branch coverage
0%
Covered branches: 0
Total branches: 12
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%
ExecuteAsync()0%156120%

File(s)

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

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Common.Models;
 3using Elsa.Workflows.Api.Constants;
 4using Elsa.Workflows.Api.Requirements;
 5using Elsa.Workflows.Management;
 6using Elsa.Workflows.Management.Filters;
 7using JetBrains.Annotations;
 8using Microsoft.AspNetCore.Authorization;
 9
 10namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.BulkPublish;
 11
 12[PublicAPI]
 113internal class BulkPublish(IWorkflowDefinitionStore store, IWorkflowDefinitionPublisher workflowDefinitionPublisher, IAu
 14    : ElsaEndpoint<Request, Response>
 15{
 16    public override void Configure()
 17    {
 118        Post("/bulk-actions/publish/workflow-definitions/by-definition-ids");
 119        ConfigurePermissions("publish:workflow-definitions");
 120    }
 21
 22    public override async Task<Response> ExecuteAsync(Request request, CancellationToken cancellationToken)
 23    {
 024        var authorizationResult = await authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(), Authorizati
 25
 026        if (!authorizationResult.Succeeded)
 27        {
 028            await Send.ForbiddenAsync(cancellationToken);
 029            return null!;
 30        }
 31
 032        var published = new List<string>();
 033        var notFound = new List<string>();
 034        var alreadyPublished = new List<string>();
 035        var skipped = new List<string>();
 036        var updatedConsumers = new List<string>();
 37
 038        var definitions = (await store.FindManyAsync(new WorkflowDefinitionFilter
 039        {
 040            DefinitionIds = request.DefinitionIds,
 041            VersionOptions = VersionOptions.Latest
 042        }, cancellationToken: cancellationToken))
 043            .DistinctBy(x => x.DefinitionId)
 044            .ToDictionary(x => x.DefinitionId);
 45
 046        foreach (var definitionId in request.DefinitionIds)
 47        {
 048            if (!definitions.TryGetValue(definitionId, out var definition))
 49            {
 050                notFound.Add(definitionId);
 051                continue;
 52            }
 53
 054            if (definition.IsPublished)
 55            {
 056                alreadyPublished.Add(definitionId);
 057                continue;
 58            }
 59
 060            if (definition.IsReadonly)
 61            {
 062                skipped.Add(definitionId);
 063                continue;
 64            }
 65
 066            var result = await workflowDefinitionPublisher.PublishAsync(definition, cancellationToken);
 067            published.Add(definitionId);
 68
 069            if (result.AffectedWorkflows.WorkflowDefinitions.Count > 0)
 070                updatedConsumers.AddRange(result.AffectedWorkflows.WorkflowDefinitions.Select(x => x.DefinitionId));
 071        }
 72
 073        return new(published, alreadyPublished, notFound, skipped, updatedConsumers);
 074    }
 75}