< Summary

Information
Class: Elsa.Workflows.Management.Services.WorkflowDefinitionManager
Assembly: Elsa.Workflows.Management
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/WorkflowDefinitionManager.cs
Line coverage
14%
Covered lines: 13
Uncovered lines: 75
Coverable lines: 88
Total lines: 151
Line coverage: 14.7%
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

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Workflows.Management/Services/WorkflowDefinitionManager.cs

#LineLine coverage
 1using Elsa.Common.Models;
 2using Elsa.Mediator.Contracts;
 3using Elsa.Workflows.Management.Entities;
 4using Elsa.Workflows.Management.Filters;
 5using Elsa.Workflows.Management.Notifications;
 6
 7namespace Elsa.Workflows.Management.Services;
 8
 9/// <inheritdoc />
 610public class WorkflowDefinitionManager(
 611    IWorkflowDefinitionStore store,
 612    INotificationSender notificationSender,
 613    IWorkflowDefinitionPublisher workflowPublisher) : IWorkflowDefinitionManager
 14{
 15    /// <inheritdoc />
 16    public async Task<long> DeleteByDefinitionIdAsync(string definitionId, CancellationToken cancellationToken = default
 17    {
 418        await notificationSender.SendAsync(new WorkflowDefinitionDeleting(definitionId), cancellationToken);
 419        var filter = new WorkflowDefinitionFilter
 420        {
 421            DefinitionId = definitionId
 422        };
 423        var count = await store.DeleteAsync(filter, cancellationToken);
 424        await notificationSender.SendAsync(new WorkflowDefinitionDeleted(definitionId), cancellationToken);
 425        return count;
 426    }
 27
 28    /// <inheritdoc />
 29    public async Task<bool> DeleteByIdAsync(string id, CancellationToken cancellationToken = default)
 30    {
 031        var filter = new WorkflowDefinitionFilter
 032        {
 033            Id = id
 034        };
 035        var definition = await store.FindAsync(filter, cancellationToken);
 36
 037        if (definition == null)
 038            return false;
 39
 040        return await DeleteVersionAsync(definition, cancellationToken);
 041    }
 42
 43    /// <inheritdoc />
 44    public async Task<long> BulkDeleteByDefinitionIdsAsync(IEnumerable<string> definitionIds, CancellationToken cancella
 45    {
 046        var definitionIdList = definitionIds.Distinct().ToList();
 047        await notificationSender.SendAsync(new WorkflowDefinitionsDeleting(definitionIdList), cancellationToken);
 048        var filter = new WorkflowDefinitionFilter
 049        {
 050            DefinitionIds = definitionIdList,
 051            IsReadonly = false
 052        };
 053        var count = await store.DeleteAsync(filter, cancellationToken);
 054        await EnsureLastVersionIsLatestAsync(definitionIdList, cancellationToken);
 055        await notificationSender.SendAsync(new WorkflowDefinitionsDeleted(definitionIdList), cancellationToken);
 056        return count;
 057    }
 58
 59    /// <inheritdoc />
 60    public async Task<long> BulkDeleteByIdsAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default)
 61    {
 062        var idList = ids.ToList();
 063        var definitions = await store.FindSummariesAsync(new WorkflowDefinitionFilter
 064        {
 065            Ids = idList
 066        }, cancellationToken);
 067        var definitionIds = definitions.Select(x => x.DefinitionId).Distinct().ToList();
 068        await notificationSender.SendAsync(new WorkflowDefinitionVersionsDeleting(idList), cancellationToken);
 069        var filter = new WorkflowDefinitionFilter
 070        {
 071            Ids = idList
 072        };
 073        var count = await store.DeleteAsync(filter, cancellationToken);
 074        await EnsureLastVersionIsLatestAsync(definitionIds, cancellationToken);
 075        await notificationSender.SendAsync(new WorkflowDefinitionVersionsDeleted(idList), cancellationToken);
 076        return count;
 077    }
 78
 79    /// <inheritdoc />
 80    public async Task<bool> DeleteVersionAsync(string definitionId, int versionToDelete, CancellationToken cancellationT
 81    {
 082        var filter = new WorkflowDefinitionFilter
 083        {
 084            DefinitionId = definitionId,
 085            VersionOptions = VersionOptions.SpecificVersion(versionToDelete)
 086        };
 087        var definitionToDelete = await store.FindAsync(filter, cancellationToken);
 88
 089        if (definitionToDelete == null)
 090            return false;
 91
 092        return await DeleteVersionAsync(definitionToDelete, cancellationToken);
 093    }
 94
 95    /// <inheritdoc />
 96    public async Task<bool> DeleteVersionAsync(WorkflowDefinition definitionToDelete, CancellationToken cancellationToke
 97    {
 098        if (definitionToDelete.IsPublished)
 99        {
 0100            await workflowPublisher.RetractAsync(definitionToDelete, cancellationToken);
 101        }
 102
 0103        await notificationSender.SendAsync(new WorkflowDefinitionVersionDeleting(definitionToDelete), cancellationToken)
 104
 0105        var filter = new WorkflowDefinitionFilter
 0106        {
 0107            Id = definitionToDelete.Id
 0108        };
 0109        var isDeleted = await store.DeleteAsync(filter, cancellationToken) > 0;
 110
 0111        if (!isDeleted)
 0112            return false;
 113
 0114        await EnsureLastVersionIsLatestAsync(definitionToDelete.DefinitionId, cancellationToken);
 0115        await notificationSender.SendAsync(new WorkflowDefinitionVersionDeleted(definitionToDelete), cancellationToken);
 116
 0117        return isDeleted;
 0118    }
 119
 120    /// <inheritdoc />
 121    public Task<WorkflowDefinition> RevertVersionAsync(string definitionId, int version, CancellationToken cancellationT
 122    {
 0123        return workflowPublisher.RevertVersionAsync(definitionId, version, cancellationToken);
 124    }
 125
 126    private async Task EnsureLastVersionIsLatestAsync(IEnumerable<string> definitionIds, CancellationToken cancellationT
 127    {
 0128        foreach (var definitionId in definitionIds)
 0129            await EnsureLastVersionIsLatestAsync(definitionId, cancellationToken);
 0130    }
 131
 132    /// <summary>
 133    /// Ensures that the last version of a workflow definition is marked as "IsLatest = true".
 134    /// </summary>
 135    /// <param name="definitionId">The definition ID.</param>
 136    /// <param name="cancellationToken">The cancellation token.</param>
 137    private async Task EnsureLastVersionIsLatestAsync(string definitionId, CancellationToken cancellationToken)
 138    {
 0139        var filter = new WorkflowDefinitionFilter
 0140        {
 0141            DefinitionId = definitionId
 0142        };
 0143        var lastVersion = await store.FindLastVersionAsync(filter, cancellationToken);
 144
 0145        if (lastVersion is null)
 0146            return;
 147
 0148        lastVersion.IsLatest = true;
 0149        await store.SaveAsync(lastVersion, cancellationToken);
 0150    }
 151}