| | | 1 | | using Elsa.Common.Entities; |
| | | 2 | | using Elsa.Common.Multitenancy; |
| | | 3 | | using Elsa.Common.Services; |
| | | 4 | | using Elsa.Labels.Contracts; |
| | | 5 | | using Elsa.Labels.Entities; |
| | | 6 | | |
| | | 7 | | namespace Elsa.Labels.Services; |
| | | 8 | | |
| | | 9 | | /// <summary> |
| | | 10 | | /// An in-memory store of workflow-label associations. |
| | | 11 | | /// </summary> |
| | | 12 | | /// <remarks> |
| | | 13 | | /// Ambient tenant is applied here rather than in callers. |
| | | 14 | | /// EF owns that via <c>SetTenantIdFilter</c> / <c>ApplyTenantId</c>; Memory must compensate. |
| | | 15 | | /// Labels contracts have no TenantAgnostic flag, so isolation always applies (EF query filter). |
| | | 16 | | /// </remarks> |
| | | 17 | | public class InMemoryWorkflowDefinitionLabelStore : IWorkflowDefinitionLabelStore, IWorkflowDefinitionLabelQuery |
| | | 18 | | { |
| | | 19 | | private readonly MemoryStore<WorkflowDefinitionLabel> _store; |
| | | 20 | | private readonly ITenantAccessor? _tenantAccessor; |
| | | 21 | | |
| | | 22 | | /// <summary> |
| | | 23 | | /// Constructor. |
| | | 24 | | /// </summary> |
| | 19 | 25 | | public InMemoryWorkflowDefinitionLabelStore(MemoryStore<WorkflowDefinitionLabel> store, ITenantAccessor? tenantAcces |
| | | 26 | | { |
| | 19 | 27 | | _store = store; |
| | 19 | 28 | | _tenantAccessor = tenantAccessor; |
| | 19 | 29 | | } |
| | | 30 | | |
| | | 31 | | /// <inheritdoc /> |
| | | 32 | | public Task SaveAsync(WorkflowDefinitionLabel record, CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 36 | 34 | | ApplyCurrentTenant(record); |
| | 36 | 35 | | lock (_store.Sync) |
| | 72 | 36 | | _store.Save(record, x => x.Id); |
| | 36 | 37 | | return Task.CompletedTask; |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <inheritdoc /> |
| | | 41 | | public Task SaveManyAsync(IEnumerable<WorkflowDefinitionLabel> records, CancellationToken cancellationToken = defaul |
| | | 42 | | { |
| | 3 | 43 | | var list = records.ToList(); |
| | | 44 | | |
| | 16 | 45 | | foreach (var record in list) |
| | 5 | 46 | | ApplyCurrentTenant(record); |
| | | 47 | | |
| | 3 | 48 | | lock (_store.Sync) |
| | 8 | 49 | | _store.SaveMany(list, x => x.Id); |
| | 3 | 50 | | return Task.CompletedTask; |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | /// <inheritdoc /> |
| | | 54 | | public Task<bool> DeleteAsync(string id, CancellationToken cancellationToken = default) |
| | | 55 | | { |
| | 3 | 56 | | lock (_store.Sync) |
| | 10 | 57 | | return Task.FromResult(_store.DeleteWhere(x => x.Id == id && IsVisible(x)) > 0); |
| | 3 | 58 | | } |
| | | 59 | | |
| | | 60 | | /// <inheritdoc /> |
| | | 61 | | public Task<IEnumerable<WorkflowDefinitionLabel>> FindByWorkflowDefinitionVersionIdAsync(string workflowDefinitionVe |
| | | 62 | | { |
| | 36 | 63 | | var result = _store.Query(query => query.WhereVisibleToTenant(CurrentTenantId).Where(x => x.WorkflowDefinitionVe |
| | 18 | 64 | | return Task.FromResult(result); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | /// <inheritdoc /> |
| | | 68 | | public Task<IEnumerable<WorkflowDefinitionLabel>> FindByLabelIdsAsync(IEnumerable<string> labelIds, CancellationToke |
| | | 69 | | { |
| | 24 | 70 | | var ids = labelIds.ToHashSet(); |
| | 48 | 71 | | var result = _store.Query(query => query.WhereVisibleToTenant(CurrentTenantId).Where(x => ids.Contains(x.LabelId |
| | 24 | 72 | | return Task.FromResult(result); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | /// <inheritdoc /> |
| | | 76 | | public Task ReplaceAsync(IEnumerable<WorkflowDefinitionLabel> removed, IEnumerable<WorkflowDefinitionLabel> added, C |
| | | 77 | | { |
| | 8 | 78 | | var removedIds = removed.Select(x => x.Id).ToHashSet(); |
| | 3 | 79 | | var addedList = added.ToList(); |
| | | 80 | | |
| | 12 | 81 | | foreach (var record in addedList) |
| | 3 | 82 | | ApplyCurrentTenant(record); |
| | | 83 | | |
| | 3 | 84 | | lock (_store.Sync) |
| | | 85 | | { |
| | 11 | 86 | | _store.DeleteWhere(x => removedIds.Contains(x.Id) && IsVisible(x)); |
| | 6 | 87 | | _store.SaveMany(addedList, x => x.Id); |
| | 3 | 88 | | } |
| | | 89 | | |
| | 3 | 90 | | return Task.CompletedTask; |
| | | 91 | | } |
| | | 92 | | |
| | | 93 | | /// <inheritdoc /> |
| | | 94 | | public Task<long> DeleteByWorkflowDefinitionIdAsync(string workflowDefinitionId, CancellationToken cancellationToken |
| | | 95 | | { |
| | 3 | 96 | | lock (_store.Sync) |
| | 12 | 97 | | return Task.FromResult(_store.DeleteWhere(x => x.WorkflowDefinitionId == workflowDefinitionId && IsVisible(x |
| | 3 | 98 | | } |
| | | 99 | | |
| | | 100 | | /// <inheritdoc /> |
| | | 101 | | public Task<long> DeleteByWorkflowDefinitionVersionIdAsync(string workflowDefinitionVersionId, CancellationToken can |
| | | 102 | | { |
| | 1 | 103 | | lock (_store.Sync) |
| | 5 | 104 | | return Task.FromResult(_store.DeleteWhere(x => x.WorkflowDefinitionVersionId == workflowDefinitionVersionId |
| | 1 | 105 | | } |
| | | 106 | | |
| | | 107 | | /// <inheritdoc /> |
| | | 108 | | public Task<long> DeleteByWorkflowDefinitionIdsAsync(IEnumerable<string> workflowDefinitionIds, CancellationToken ca |
| | | 109 | | { |
| | 1 | 110 | | var ids = workflowDefinitionIds.ToList(); |
| | 1 | 111 | | lock (_store.Sync) |
| | 4 | 112 | | return Task.FromResult(_store.DeleteWhere(x => ids.Contains(x.WorkflowDefinitionId) && IsVisible(x))); |
| | 1 | 113 | | } |
| | | 114 | | |
| | | 115 | | /// <inheritdoc /> |
| | | 116 | | public Task<long> DeleteByWorkflowDefinitionVersionIdsAsync(IEnumerable<string> workflowDefinitionVersionIds, Cancel |
| | | 117 | | { |
| | 1 | 118 | | var ids = workflowDefinitionVersionIds.ToList(); |
| | 1 | 119 | | lock (_store.Sync) |
| | 6 | 120 | | return Task.FromResult(_store.DeleteWhere(x => ids.Contains(x.WorkflowDefinitionVersionId) && IsVisible(x))) |
| | 1 | 121 | | } |
| | | 122 | | |
| | 19 | 123 | | private bool IsVisible(Entity entity) => TenantVisibility.IsVisible(entity.TenantId, CurrentTenantId); |
| | | 124 | | |
| | 61 | 125 | | private string CurrentTenantId => _tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | | 126 | | |
| | | 127 | | private void ApplyCurrentTenant(Entity entity) |
| | | 128 | | { |
| | 44 | 129 | | if (entity.TenantId == Tenant.AgnosticTenantId || _tenantAccessor is null) |
| | 5 | 130 | | return; |
| | | 131 | | |
| | 39 | 132 | | entity.TenantId ??= _tenantAccessor.TenantId; |
| | 39 | 133 | | } |
| | | 134 | | } |