< Summary

Information
Class: Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Import.Import
Assembly: Elsa.Workflows.Api
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Api/Endpoints/WorkflowDefinitions/Import/Endpoint.cs
Line coverage
30%
Covered lines: 12
Uncovered lines: 28
Coverable lines: 40
Total lines: 89
Line coverage: 30%
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%
HandleAsync()0%7280%
ImportSingleWorkflowDefinitionAsync()0%2040%

File(s)

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

#LineLine coverage
 1using Elsa.Abstractions;
 2using Elsa.Workflows.Api.Constants;
 3using Elsa.Workflows.Api.Requirements;
 4using Elsa.Workflows.Management;
 5using Elsa.Workflows.Management.Models;
 6using JetBrains.Annotations;
 7using Microsoft.AspNetCore.Authorization;
 8
 9namespace Elsa.Workflows.Api.Endpoints.WorkflowDefinitions.Import;
 10
 11/// <summary>
 12/// Imports JSON and/or ZIP files containing a workflow definitions.
 13/// </summary>
 14[PublicAPI]
 15internal class Import : ElsaEndpoint<WorkflowDefinitionModel>
 16{
 17    private readonly IWorkflowDefinitionImporter _workflowDefinitionImporter;
 18    private readonly IWorkflowDefinitionLinker _linker;
 19    private readonly IAuthorizationService _authorizationService;
 20
 21    /// <inheritdoc />
 122    public Import(
 123        IWorkflowDefinitionImporter workflowDefinitionImporter,
 124        IWorkflowDefinitionLinker linker,
 125        IAuthorizationService authorizationService)
 26    {
 127        _workflowDefinitionImporter = workflowDefinitionImporter;
 128        _linker = linker;
 129        _authorizationService = authorizationService;
 130    }
 31
 32    /// <inheritdoc />
 33    public override void Configure()
 34    {
 135        Routes("workflow-definitions/import", "workflow-definitions/{definitionId}/import");
 136        Verbs(FastEndpoints.Http.POST, FastEndpoints.Http.PUT);
 137        ConfigurePermissions("write:workflow-definitions");
 138    }
 39
 40    /// <inheritdoc />
 41    public override async Task HandleAsync(WorkflowDefinitionModel model, CancellationToken cancellationToken)
 42    {
 043        var definitionId = model.DefinitionId;
 044        var isNew = string.IsNullOrWhiteSpace(definitionId);
 045        var result = await ImportSingleWorkflowDefinitionAsync(model, cancellationToken);
 046        var definition = result.WorkflowDefinition;
 47
 048        var authorizationResult = await _authorizationService.AuthorizeAsync(User, new NotReadOnlyResource(definition), 
 49
 050        if (!authorizationResult.Succeeded)
 51        {
 052            await Send.ForbiddenAsync(cancellationToken);
 053            return;
 54        }
 55
 056        var updatedModel = await _linker.MapAsync(definition, cancellationToken);
 57
 058        if (result.Succeeded)
 59        {
 060            if (isNew)
 061                await Send.CreatedAtAsync<GetByDefinitionId.GetByDefinitionId>(new { DefinitionId = definitionId }, upda
 62            else
 063                await Send.OkAsync(updatedModel, cancellationToken);
 64        }
 65
 066        if (ValidationFailed)
 067            await Send.ErrorsAsync(400, cancellationToken);
 068    }
 69
 70    private async Task<ImportWorkflowResult> ImportSingleWorkflowDefinitionAsync(WorkflowDefinitionModel model, Cancella
 71    {
 72        // Import workflow
 073        var saveWorkflowRequest = new SaveWorkflowDefinitionRequest
 074        {
 075            Model = model,
 076            Publish = false,
 077        };
 78
 079        var result = await _workflowDefinitionImporter.ImportAsync(saveWorkflowRequest, cancellationToken);
 80
 081        if (result.Succeeded)
 082            return result;
 83
 084        foreach (var validationError in result.ValidationErrors)
 085            AddError(validationError.Message);
 86
 087        return result;
 088    }
 89}