| | | 1 | | using Elsa.Common.Models; |
| | | 2 | | using Elsa.Mediator.Contracts; |
| | | 3 | | using Elsa.Workflows.Management.Entities; |
| | | 4 | | using Elsa.Workflows.Management.Filters; |
| | | 5 | | using Elsa.Workflows.Management.Notifications; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Workflows.Management.Services; |
| | | 8 | | |
| | | 9 | | /// <inheritdoc /> |
| | 6 | 10 | | public class WorkflowDefinitionManager( |
| | 6 | 11 | | IWorkflowDefinitionStore store, |
| | 6 | 12 | | INotificationSender notificationSender, |
| | 6 | 13 | | IWorkflowDefinitionPublisher workflowPublisher) : IWorkflowDefinitionManager |
| | | 14 | | { |
| | | 15 | | /// <inheritdoc /> |
| | | 16 | | public async Task<long> DeleteByDefinitionIdAsync(string definitionId, CancellationToken cancellationToken = default |
| | | 17 | | { |
| | 4 | 18 | | await notificationSender.SendAsync(new WorkflowDefinitionDeleting(definitionId), cancellationToken); |
| | 4 | 19 | | var filter = new WorkflowDefinitionFilter |
| | 4 | 20 | | { |
| | 4 | 21 | | DefinitionId = definitionId |
| | 4 | 22 | | }; |
| | 4 | 23 | | var count = await store.DeleteAsync(filter, cancellationToken); |
| | 4 | 24 | | await notificationSender.SendAsync(new WorkflowDefinitionDeleted(definitionId), cancellationToken); |
| | 4 | 25 | | return count; |
| | 4 | 26 | | } |
| | | 27 | | |
| | | 28 | | /// <inheritdoc /> |
| | | 29 | | public async Task<bool> DeleteByIdAsync(string id, CancellationToken cancellationToken = default) |
| | | 30 | | { |
| | 0 | 31 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 32 | | { |
| | 0 | 33 | | Id = id |
| | 0 | 34 | | }; |
| | 0 | 35 | | var definition = await store.FindAsync(filter, cancellationToken); |
| | | 36 | | |
| | 0 | 37 | | if (definition == null) |
| | 0 | 38 | | return false; |
| | | 39 | | |
| | 0 | 40 | | return await DeleteVersionAsync(definition, cancellationToken); |
| | 0 | 41 | | } |
| | | 42 | | |
| | | 43 | | /// <inheritdoc /> |
| | | 44 | | public async Task<long> BulkDeleteByDefinitionIdsAsync(IEnumerable<string> definitionIds, CancellationToken cancella |
| | | 45 | | { |
| | 0 | 46 | | var definitionIdList = definitionIds.Distinct().ToList(); |
| | 0 | 47 | | await notificationSender.SendAsync(new WorkflowDefinitionsDeleting(definitionIdList), cancellationToken); |
| | 0 | 48 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 49 | | { |
| | 0 | 50 | | DefinitionIds = definitionIdList, |
| | 0 | 51 | | IsReadonly = false |
| | 0 | 52 | | }; |
| | 0 | 53 | | var count = await store.DeleteAsync(filter, cancellationToken); |
| | 0 | 54 | | await EnsureLastVersionIsLatestAsync(definitionIdList, cancellationToken); |
| | 0 | 55 | | await notificationSender.SendAsync(new WorkflowDefinitionsDeleted(definitionIdList), cancellationToken); |
| | 0 | 56 | | return count; |
| | 0 | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <inheritdoc /> |
| | | 60 | | public async Task<long> BulkDeleteByIdsAsync(IEnumerable<string> ids, CancellationToken cancellationToken = default) |
| | | 61 | | { |
| | 0 | 62 | | var idList = ids.ToList(); |
| | 0 | 63 | | var definitions = await store.FindSummariesAsync(new WorkflowDefinitionFilter |
| | 0 | 64 | | { |
| | 0 | 65 | | Ids = idList |
| | 0 | 66 | | }, cancellationToken); |
| | 0 | 67 | | var definitionIds = definitions.Select(x => x.DefinitionId).Distinct().ToList(); |
| | 0 | 68 | | await notificationSender.SendAsync(new WorkflowDefinitionVersionsDeleting(idList), cancellationToken); |
| | 0 | 69 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 70 | | { |
| | 0 | 71 | | Ids = idList |
| | 0 | 72 | | }; |
| | 0 | 73 | | var count = await store.DeleteAsync(filter, cancellationToken); |
| | 0 | 74 | | await EnsureLastVersionIsLatestAsync(definitionIds, cancellationToken); |
| | 0 | 75 | | await notificationSender.SendAsync(new WorkflowDefinitionVersionsDeleted(idList), cancellationToken); |
| | 0 | 76 | | return count; |
| | 0 | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <inheritdoc /> |
| | | 80 | | public async Task<bool> DeleteVersionAsync(string definitionId, int versionToDelete, CancellationToken cancellationT |
| | | 81 | | { |
| | 0 | 82 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 83 | | { |
| | 0 | 84 | | DefinitionId = definitionId, |
| | 0 | 85 | | VersionOptions = VersionOptions.SpecificVersion(versionToDelete) |
| | 0 | 86 | | }; |
| | 0 | 87 | | var definitionToDelete = await store.FindAsync(filter, cancellationToken); |
| | | 88 | | |
| | 0 | 89 | | if (definitionToDelete == null) |
| | 0 | 90 | | return false; |
| | | 91 | | |
| | 0 | 92 | | return await DeleteVersionAsync(definitionToDelete, cancellationToken); |
| | 0 | 93 | | } |
| | | 94 | | |
| | | 95 | | /// <inheritdoc /> |
| | | 96 | | public async Task<bool> DeleteVersionAsync(WorkflowDefinition definitionToDelete, CancellationToken cancellationToke |
| | | 97 | | { |
| | 0 | 98 | | if (definitionToDelete.IsPublished) |
| | | 99 | | { |
| | 0 | 100 | | await workflowPublisher.RetractAsync(definitionToDelete, cancellationToken); |
| | | 101 | | } |
| | | 102 | | |
| | 0 | 103 | | await notificationSender.SendAsync(new WorkflowDefinitionVersionDeleting(definitionToDelete), cancellationToken) |
| | | 104 | | |
| | 0 | 105 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 106 | | { |
| | 0 | 107 | | Id = definitionToDelete.Id |
| | 0 | 108 | | }; |
| | 0 | 109 | | var isDeleted = await store.DeleteAsync(filter, cancellationToken) > 0; |
| | | 110 | | |
| | 0 | 111 | | if (!isDeleted) |
| | 0 | 112 | | return false; |
| | | 113 | | |
| | 0 | 114 | | await EnsureLastVersionIsLatestAsync(definitionToDelete.DefinitionId, cancellationToken); |
| | 0 | 115 | | await notificationSender.SendAsync(new WorkflowDefinitionVersionDeleted(definitionToDelete), cancellationToken); |
| | | 116 | | |
| | 0 | 117 | | return isDeleted; |
| | 0 | 118 | | } |
| | | 119 | | |
| | | 120 | | /// <inheritdoc /> |
| | | 121 | | public Task<WorkflowDefinition> RevertVersionAsync(string definitionId, int version, CancellationToken cancellationT |
| | | 122 | | { |
| | 0 | 123 | | return workflowPublisher.RevertVersionAsync(definitionId, version, cancellationToken); |
| | | 124 | | } |
| | | 125 | | |
| | | 126 | | private async Task EnsureLastVersionIsLatestAsync(IEnumerable<string> definitionIds, CancellationToken cancellationT |
| | | 127 | | { |
| | 0 | 128 | | foreach (var definitionId in definitionIds) |
| | 0 | 129 | | await EnsureLastVersionIsLatestAsync(definitionId, cancellationToken); |
| | 0 | 130 | | } |
| | | 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 | | { |
| | 0 | 139 | | var filter = new WorkflowDefinitionFilter |
| | 0 | 140 | | { |
| | 0 | 141 | | DefinitionId = definitionId |
| | 0 | 142 | | }; |
| | 0 | 143 | | var lastVersion = await store.FindLastVersionAsync(filter, cancellationToken); |
| | | 144 | | |
| | 0 | 145 | | if (lastVersion is null) |
| | 0 | 146 | | return; |
| | | 147 | | |
| | 0 | 148 | | lastVersion.IsLatest = true; |
| | 0 | 149 | | await store.SaveAsync(lastVersion, cancellationToken); |
| | 0 | 150 | | } |
| | | 151 | | } |