| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Command.Input; |
| | | 3 | | using ArturRios.Heimdall.Command.Output; |
| | | 4 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 5 | | using ArturRios.Heimdall.Domain.Persistence; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Heimdall.Shared.Services; |
| | | 8 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 9 | | using ArturRios.Output; |
| | | 10 | | using FluentValidation; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | |
| | | 13 | | namespace 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> |
| | 9 | 20 | | public class SetSelfRegistrationCommandHandler( |
| | 9 | 21 | | IValidator<SetSelfRegistrationCommand> validator, |
| | 9 | 22 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 9 | 23 | | IAtomicWrites atomicWrites, |
| | 9 | 24 | | IScopeOwnershipChecker scopeOwnership) |
| | | 25 | | : ICommandHandlerAsync<SetSelfRegistrationCommand, SetSelfRegistrationCommandOutput> |
| | | 26 | | { |
| | | 27 | | public async Task<DataOutput<SetSelfRegistrationCommandOutput?>> HandleAsync( |
| | | 28 | | SetSelfRegistrationCommand command, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 9 | 30 | | var output = DataOutput<SetSelfRegistrationCommandOutput?>.New; |
| | | 31 | | |
| | 9 | 32 | | var validation = await validator.ValidateAsync(command, cancellationToken); |
| | | 33 | | |
| | 9 | 34 | | if (!validation.IsValid) |
| | | 35 | | { |
| | 2 | 36 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 37 | | } |
| | | 38 | | |
| | 8 | 39 | | var scope = await scopeReader.Query() |
| | 8 | 40 | | .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted, cancellationToken); |
| | | 41 | | |
| | 8 | 42 | | if (scope is null) |
| | | 43 | | { |
| | 1 | 44 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 45 | | } |
| | | 46 | | |
| | 7 | 47 | | if (!await scopeOwnership.ActorMayManageScopeAsync(command.ActingRole, command.ActingPersonId, scope.Id)) |
| | | 48 | | { |
| | 1 | 49 | | return output.WithError(ScopeMessages.NotScopeOwner); |
| | | 50 | | } |
| | | 51 | | |
| | 6 | 52 | | var ownerIds = await scopeReader.Query() |
| | 6 | 53 | | .Where(x => x.PublicId == command.Id) |
| | 6 | 54 | | .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId)) |
| | 6 | 55 | | .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. |
| | 6 | 59 | | if (!await atomicWrites.TrySetSelfRegistrationAsync(scope, command.Enabled!.Value, DateTime.UtcNow)) |
| | | 60 | | { |
| | 1 | 61 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 62 | | } |
| | | 63 | | |
| | 5 | 64 | | return output |
| | 5 | 65 | | .WithData(new SetSelfRegistrationCommandOutput |
| | 5 | 66 | | { |
| | 5 | 67 | | Id = scope.PublicId, |
| | 5 | 68 | | Name = scope.Name, |
| | 5 | 69 | | Description = scope.Description, |
| | 5 | 70 | | GoogleSignInEnabled = scope.GoogleSignInEnabled, |
| | 5 | 71 | | SelfRegistrationEnabled = scope.SelfRegistrationEnabled, |
| | 5 | 72 | | EmailVerificationUrl = scope.EmailVerificationUrl, |
| | 5 | 73 | | PasswordResetUrl = scope.PasswordResetUrl, |
| | 5 | 74 | | OwnerIds = ownerIds, |
| | 5 | 75 | | CreatedAt = scope.CreatedAt, |
| | 5 | 76 | | UpdatedAt = scope.UpdatedAt |
| | 5 | 77 | | }) |
| | 5 | 78 | | .WithMessage(ScopeMessages.SelfRegistrationUpdatedSuccessfully); |
| | 9 | 79 | | } |
| | | 80 | | } |