< Summary - Heimdall API

Information
Class: ArturRios.Heimdall.Command.Handlers.SetSelfRegistrationCommandHandler
Assembly: ArturRios.Heimdall.Command
File(s): /home/ci/agent/workspace/heimdall-api-docs_main/app/src/Application/ArturRios.Heimdall.Command/Handlers/SetSelfRegistrationCommandHandler.cs
Line coverage
100%
Covered lines: 37
Uncovered lines: 0
Coverable lines: 37
Total lines: 80
Line coverage: 100%
Branch coverage
100%
Covered branches: 8
Total branches: 8
Branch coverage: 100%
Method coverage
100%
Covered methods: 2
Fully covered methods: 2
Total methods: 2
Method coverage: 100%
Full method coverage: 100%

Metrics

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

File(s)

/home/ci/agent/workspace/heimdall-api-docs_main/app/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.Domain.Persistence;
 6using ArturRios.Heimdall.Shared.Messages;
 7using ArturRios.Heimdall.Shared.Services;
 8using ArturRios.Mediator.Command.Interfaces;
 9using ArturRios.Output;
 10using FluentValidation;
 11using Microsoft.EntityFrameworkCore;
 12
 13namespace ArturRios.Heimdall.Command.Handlers;
 14
 15/// <summary>
 16///     Handles <see cref="SetSelfRegistrationCommand" /> (UC-47 toggle, FR-SR-02): the same order of
 17///     checks as <see cref="SetGoogleSignInCommandHandler" /> — the value is stated, the scope is live,
 18///     a Scope Admin actor owns it — then writes <c>SelfRegistrationEnabled</c> and returns the scope.
 19/// </summary>
 920public class SetSelfRegistrationCommandHandler(
 921    IValidator<SetSelfRegistrationCommand> validator,
 922    IAsyncReadOnlyRepository<Scope, long> scopeReader,
 923    IAtomicWrites atomicWrites,
 924    IScopeOwnershipChecker scopeOwnership)
 25    : ICommandHandlerAsync<SetSelfRegistrationCommand, SetSelfRegistrationCommandOutput>
 26{
 27    public async Task<DataOutput<SetSelfRegistrationCommandOutput?>> HandleAsync(
 28        SetSelfRegistrationCommand command, CancellationToken cancellationToken = default)
 29    {
 930        var output = DataOutput<SetSelfRegistrationCommandOutput?>.New;
 31
 932        var validation = await validator.ValidateAsync(command, cancellationToken);
 33
 934        if (!validation.IsValid)
 35        {
 236            return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage));
 37        }
 38
 839        var scope = await scopeReader.Query()
 840            .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted, cancellationToken);
 41
 842        if (scope is null)
 43        {
 144            return output.WithError(ScopeMessages.ScopeNotFound);
 45        }
 46
 747        if (!await scopeOwnership.ActorMayManageScopeAsync(command.ActingRole, command.ActingPersonId, scope.Id))
 48        {
 149            return output.WithError(ScopeMessages.NotScopeOwner);
 50        }
 51
 652        var ownerIds = await scopeReader.Query()
 653            .Where(x => x.PublicId == command.Id)
 654            .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId))
 655            .ToListAsync(cancellationToken);
 56
 57        // The flag alone, and only while the scope is not deleted (#205), as UC-24 writes its own: one
 58        // deleted since the read above is answered as one deleted before it.
 659        if (!await atomicWrites.TrySetSelfRegistrationAsync(scope, command.Enabled!.Value, DateTime.UtcNow))
 60        {
 161            return output.WithError(ScopeMessages.ScopeNotFound);
 62        }
 63
 564        return output
 565            .WithData(new SetSelfRegistrationCommandOutput
 566            {
 567                Id = scope.PublicId,
 568                Name = scope.Name,
 569                Description = scope.Description,
 570                GoogleSignInEnabled = scope.GoogleSignInEnabled,
 571                SelfRegistrationEnabled = scope.SelfRegistrationEnabled,
 572                EmailVerificationUrl = scope.EmailVerificationUrl,
 573                PasswordResetUrl = scope.PasswordResetUrl,
 574                OwnerIds = ownerIds,
 575                CreatedAt = scope.CreatedAt,
 576                UpdatedAt = scope.UpdatedAt
 577            })
 578            .WithMessage(ScopeMessages.SelfRegistrationUpdatedSuccessfully);
 979    }
 80}