< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.Command.Services.LastScopeOwnerGuard
Assembly: ArturRios.Heimdall.Command
File(s): /home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Services/LastScopeOwnerGuard.cs
Line coverage
100%
Covered lines: 9
Uncovered lines: 0
Coverable lines: 9
Total lines: 50
Line coverage: 100%
Branch coverage
100%
Covered branches: 2
Total branches: 2
Branch coverage: 100%
Method coverage
100%
Covered methods: 1
Fully covered methods: 1
Total methods: 1
Method coverage: 100%
Full method coverage: 100%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
WouldStripLastOwnerAsync()100%22100%

File(s)

/home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Services/LastScopeOwnerGuard.cs

#LineLine coverage
 1using ArturRios.Data.Relational.Core.Interfaces;
 2using ArturRios.Heimdall.Domain.Entities;
 3using ArturRios.Heimdall.Domain.Enums;
 4using Microsoft.EntityFrameworkCore;
 5
 6namespace ArturRios.Heimdall.Command.Services;
 7
 8/// <summary>
 9///     NFR-12: whether suspending a person would leave a scope they own with no owner at all.
 10/// </summary>
 11/// <remarks>
 12///     <para>
 13///         The erasure request (UC-42) and the anonymisation pass (NFR-20) both need this answer —
 14///         one to decide whether the request can take effect, the other to decide whether a blocked
 15///         request can now proceed — so it is written once here rather than twice more.
 16///     </para>
 17///     <para>
 18///         Three handlers predating this carry their own copies of the same guard
 19///         (<c>DeletePersonCommandHandler</c>, <c>HardDeletePersonCommandHandler</c>,
 20///         <c>UpdatePersonCommandHandler</c>). Consolidating those is worth doing and is deliberately
 21///         not done here: they are covered by their own tests, and folding four call sites into one
 22///         belongs in a change about that rather than tacked onto this one.
 23///     </para>
 24/// </remarks>
 25public static class LastScopeOwnerGuard
 26{
 27    /// <summary>
 28    ///     Whether any scope <paramref name="person" /> owns is owned by nobody else who is still
 29    ///     live. Requires <c>ScopeOwnerships</c> to have been loaded.
 30    /// </summary>
 31    public static async Task<bool> WouldStripLastOwnerAsync(
 32        Person person, IAsyncReadOnlyRepository<Person, long> personReader)
 33    {
 34        // Whatever the role: a SystemAdmin may own a scope too (FR-SC-08).
 1835        if (person.ScopeOwnerships.Count == 0)
 36        {
 937            return false;
 38        }
 39
 40        // Persons already logically deleted are excluded: a suspended owner cannot authenticate, so
 41        // they no longer keep a scope owned.
 942        var coOwnedScopeIds = await personReader.Query()
 943            .Where(other => other.Id != person.Id && !other.IsDeleted)
 944            .SelectMany(other => other.ScopeOwnerships.Select(ownership => ownership.ScopeId))
 945            .Distinct()
 946            .ToListAsync();
 47
 1848        return person.ScopeOwnerships.Any(ownership => !coOwnedScopeIds.Contains(ownership.ScopeId));
 1849    }
 50}

Methods/Properties

WouldStripLastOwnerAsync()