< Summary

Information
Class: Elsa.Persistence.EFCore.EntityHandlers.ApplyTenantId
Assembly: Elsa.Persistence.EFCore.Common
File(s): /home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/EntityHandlers/ApplyTenantId.cs
Line coverage
70%
Covered lines: 7
Uncovered lines: 3
Coverable lines: 10
Total lines: 34
Line coverage: 70%
Branch coverage
70%
Covered branches: 7
Total branches: 10
Branch coverage: 70%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAsync(...)70%141066.66%

File(s)

/home/runner/work/elsa-core/elsa-core/src/modules/Elsa.Persistence.EFCore.Common/EntityHandlers/ApplyTenantId.cs

#LineLine coverage
 1using Elsa.Common.Entities;
 2using Elsa.Common.Multitenancy;
 3using Elsa.Tenants.Options;
 4using Microsoft.EntityFrameworkCore.ChangeTracking;
 5using Microsoft.Extensions.Options;
 6
 7namespace Elsa.Persistence.EFCore.EntityHandlers;
 8
 9/// <summary>
 10/// Represents a handler for applying the tenant ID to an entity before saving changes.
 11/// </summary>
 552612public class ApplyTenantId(IOptions<TenantsOptions> tenantsOptions) : IEntitySavingHandler
 13{
 14    /// <inheritdoc />
 15    public ValueTask HandleAsync(ElsaDbContextBase dbContext, EntityEntry entry, CancellationToken cancellationToken = d
 16    {
 17        // Only apply tenant ID if multitenancy is enabled
 3490218        if (!tenantsOptions.Value.IsEnabled)
 019            return default;
 20
 3490221        if (entry.Entity is not Entity entity)
 022            return default;
 23
 24        // Don't touch tenant-agnostic entities (marked with "*")
 3490225        if (entity.TenantId == Tenant.AgnosticTenantId)
 026            return default;
 27
 28        // Apply current tenant ID to entities without one
 3490229        if (entity.TenantId == null && dbContext.TenantId != null)
 581030            entity.TenantId = dbContext.TenantId;
 31
 3490232        return default;
 33    }
 34}