| | | 1 | | using Elsa.Alterations.Core.Contracts; |
| | | 2 | | using Elsa.Alterations.Core.Entities; |
| | | 3 | | using Elsa.Alterations.Core.Filters; |
| | | 4 | | using Elsa.Common.Entities; |
| | | 5 | | using Elsa.Common.Multitenancy; |
| | | 6 | | using Elsa.Common.Services; |
| | | 7 | | |
| | | 8 | | namespace Elsa.Alterations.Core.Stores; |
| | | 9 | | |
| | | 10 | | /// <summary> |
| | | 11 | | /// A memory-based store for alteration plans. |
| | | 12 | | /// </summary> |
| | | 13 | | /// <remarks> |
| | | 14 | | /// Ambient tenant is applied here rather than in callers. |
| | | 15 | | /// EF owns that via <c>SetTenantIdFilter</c> / <c>ApplyTenantId</c>; Memory must compensate. |
| | | 16 | | /// Alterations contracts have no TenantAgnostic flag, so isolation always applies (EF query filter). |
| | | 17 | | /// </remarks> |
| | | 18 | | public class MemoryAlterationPlanStore : IAlterationPlanStore |
| | | 19 | | { |
| | | 20 | | private readonly MemoryStore<AlterationPlan> _store; |
| | | 21 | | private readonly ITenantAccessor? _tenantAccessor; |
| | | 22 | | |
| | | 23 | | /// <summary> |
| | | 24 | | /// Initializes a new instance of the <see cref="MemoryAlterationPlanStore"/> class. |
| | | 25 | | /// </summary> |
| | 23 | 26 | | public MemoryAlterationPlanStore(MemoryStore<AlterationPlan> store, ITenantAccessor? tenantAccessor = null) |
| | | 27 | | { |
| | 23 | 28 | | _store = store; |
| | 23 | 29 | | _tenantAccessor = tenantAccessor; |
| | 23 | 30 | | } |
| | | 31 | | |
| | | 32 | | /// <inheritdoc /> |
| | | 33 | | public Task SaveAsync(AlterationPlan plan, CancellationToken cancellationToken = default) |
| | | 34 | | { |
| | 39 | 35 | | lock (_store.Sync) |
| | | 36 | | { |
| | 39 | 37 | | ApplyCurrentTenant(plan); |
| | 39 | 38 | | EnsureIdAvailable(plan); |
| | 70 | 39 | | _store.Save(plan, x => x.Id); |
| | 35 | 40 | | } |
| | | 41 | | |
| | 35 | 42 | | return Task.CompletedTask; |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | /// <inheritdoc /> |
| | | 46 | | public Task<AlterationPlan?> FindAsync(AlterationPlanFilter filter, CancellationToken cancellationToken = default) |
| | | 47 | | { |
| | 66 | 48 | | var entity = _store.Query(query => Filter(query, filter)).FirstOrDefault(); |
| | 33 | 49 | | return Task.FromResult(entity); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | /// <inheritdoc /> |
| | | 53 | | public Task<long> CountAsync(AlterationPlanFilter filter, CancellationToken cancellationToken = default) |
| | | 54 | | { |
| | 22 | 55 | | var count = _store.Query(query => Filter(query, filter)).LongCount(); |
| | 11 | 56 | | return Task.FromResult(count); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | /// <remarks> |
| | | 60 | | /// Ambient tenant is applied here rather than in <see cref="AlterationPlanFilter.Apply"/>. |
| | | 61 | | /// EF owns that via <c>SetTenantIdFilter</c>; Memory must compensate. |
| | | 62 | | /// </remarks> |
| | | 63 | | private IQueryable<AlterationPlan> Filter(IQueryable<AlterationPlan> query, AlterationPlanFilter filter) => |
| | 44 | 64 | | filter.Apply(query.WhereVisibleToTenant(CurrentTenantId)); |
| | | 65 | | |
| | 51 | 66 | | private string CurrentTenantId => _tenantAccessor?.TenantId ?? Tenant.DefaultTenantId; |
| | | 67 | | |
| | | 68 | | private void EnsureIdAvailable(AlterationPlan plan) |
| | | 69 | | { |
| | 75 | 70 | | var existing = _store.Find(x => x.Id == plan.Id); |
| | | 71 | | |
| | 39 | 72 | | if (existing is null) |
| | 32 | 73 | | return; |
| | | 74 | | |
| | 7 | 75 | | if (!CanReplace(existing, plan)) |
| | 4 | 76 | | throw AlterationStoreConflict.HiddenPlanId(plan.Id); |
| | | 77 | | |
| | | 78 | | // An accepted update may change the payload, but it must not rehome the row. |
| | 3 | 79 | | plan.TenantId = existing.TenantId; |
| | 3 | 80 | | } |
| | | 81 | | |
| | | 82 | | /// <summary> |
| | | 83 | | /// <c>*</c> is visible to every tenant, but only an agnostic writer may replace it. |
| | | 84 | | /// Named tenants may upsert their own visible rows. |
| | | 85 | | /// </summary> |
| | | 86 | | private bool CanReplace(Entity existing, Entity incoming) => |
| | 7 | 87 | | TenantVisibility.CanReplaceOwnedRow(existing.TenantId, incoming.TenantId, CurrentTenantId); |
| | | 88 | | |
| | | 89 | | private void ApplyCurrentTenant(Entity entity) |
| | | 90 | | { |
| | 39 | 91 | | if (entity.TenantId == Tenant.AgnosticTenantId || _tenantAccessor is null) |
| | 11 | 92 | | return; |
| | | 93 | | |
| | 28 | 94 | | entity.TenantId ??= _tenantAccessor.TenantId; |
| | 28 | 95 | | } |
| | | 96 | | } |