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