< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.Command.Handlers.SetSelfRegistrationCommandHandler
Assembly: ArturRios.Heimdall.Command
File(s): /home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Handlers/SetSelfRegistrationCommandHandler.cs
Line coverage
97%
Covered lines: 39
Uncovered lines: 1
Coverable lines: 40
Total lines: 82
Line coverage: 97.5%
Branch coverage
87%
Covered branches: 7
Total branches: 8
Branch coverage: 87.5%
Method coverage
100%
Covered methods: 2
Fully covered methods: 1
Total methods: 2
Method coverage: 100%
Full method coverage: 50%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
HandleAsync()87.5%8897.05%

File(s)

/home/runner/work/heimdall-api/heimdall-api/src/Application/ArturRios.Heimdall.Command/Handlers/SetSelfRegistrationCommandHandler.cs

#LineLine coverage
 1using ArturRios.Data.Relational.Core.Interfaces;
 2using ArturRios.Heimdall.Command.Input;
 3using ArturRios.Heimdall.Command.Output;
 4using ArturRios.Heimdall.Domain.Entities;
 5using ArturRios.Heimdall.Shared.Messages;
 6using ArturRios.Heimdall.Shared.Services;
 7using ArturRios.Mediator.Command.Interfaces;
 8using ArturRios.Output;
 9using FluentValidation;
 10using Microsoft.EntityFrameworkCore;
 11
 12namespace ArturRios.Heimdall.Command.Handlers;
 13
 14/// <summary>
 15///     Handles <see cref="SetSelfRegistrationCommand" /> (UC-47 toggle, FR-SR-02): the same order of
 16///     checks as <see cref="SetGoogleSignInCommandHandler" /> — the value is stated, the scope is live,
 17///     a Scope Admin actor owns it — then writes <c>SelfRegistrationEnabled</c> and returns the scope.
 18/// </summary>
 719public class SetSelfRegistrationCommandHandler(
 720    IValidator<SetSelfRegistrationCommand> validator,
 721    IAsyncReadOnlyRepository<Scope, long> scopeReader,
 722    IAsyncRepository<Scope, long> scopeWriter,
 723    IScopeOwnershipChecker scopeOwnership)
 24    : ICommandHandlerAsync<SetSelfRegistrationCommand, SetSelfRegistrationCommandOutput>
 25{
 26    public async Task<DataOutput<SetSelfRegistrationCommandOutput?>> HandleAsync(
 27        SetSelfRegistrationCommand command, CancellationToken cancellationToken = default)
 28    {
 729        var output = DataOutput<SetSelfRegistrationCommandOutput?>.New;
 30
 731        var validation = await validator.ValidateAsync(command, cancellationToken);
 32
 733        if (!validation.IsValid)
 34        {
 235            return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage));
 36        }
 37
 638        var scope = await scopeReader.Query()
 639            .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted, cancellationToken);
 40
 641        if (scope is null)
 42        {
 143            return output.WithError(ScopeMessages.ScopeNotFound);
 44        }
 45
 546        if (!await scopeOwnership.ActorMayManageScopeAsync(command.ActingRole, command.ActingPersonId, scope.Id))
 47        {
 148            return output.WithError(ScopeMessages.NotScopeOwner);
 49        }
 50
 451        var ownerIds = await scopeReader.Query()
 452            .Where(x => x.PublicId == command.Id)
 453            .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId))
 454            .ToListAsync(cancellationToken);
 55
 456        scope.SelfRegistrationEnabled = command.Enabled!.Value;
 457        scope.UpdatedAt = DateTime.UtcNow;
 58
 459        var update = await scopeWriter.UpdateAsync(scope);
 60
 461        if (!update.Success)
 62        {
 063            return output.WithErrors(update.Errors);
 64        }
 65
 466        return output
 467            .WithData(new SetSelfRegistrationCommandOutput
 468            {
 469                Id = scope.PublicId,
 470                Name = scope.Name,
 471                Description = scope.Description,
 472                GoogleSignInEnabled = scope.GoogleSignInEnabled,
 473                SelfRegistrationEnabled = scope.SelfRegistrationEnabled,
 474                EmailVerificationUrl = scope.EmailVerificationUrl,
 475                PasswordResetUrl = scope.PasswordResetUrl,
 476                OwnerIds = ownerIds,
 477                CreatedAt = scope.CreatedAt,
 478                UpdatedAt = scope.UpdatedAt
 479            })
 480            .WithMessage(ScopeMessages.SelfRegistrationUpdatedSuccessfully);
 781    }
 82}