| | | 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="SetScopeEmailLinksCommand" />: the same order of checks as |
| | | 16 | | /// <see cref="SetSelfRegistrationCommandHandler" /> — the links are usable, the scope is live, a |
| | | 17 | | /// Scope Admin actor owns it — then writes <c>EmailVerificationUrl</c> and <c>PasswordResetUrl</c> |
| | | 18 | | /// and returns the scope. Nothing else on the scope is touched. |
| | | 19 | | /// </summary> |
| | 15 | 20 | | public class SetScopeEmailLinksCommandHandler( |
| | 15 | 21 | | IValidator<SetScopeEmailLinksCommand> validator, |
| | 15 | 22 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 15 | 23 | | IAsyncRepository<Scope, long> scopeWriter, |
| | 15 | 24 | | IScopeOwnershipChecker scopeOwnership) |
| | | 25 | | : ICommandHandlerAsync<SetScopeEmailLinksCommand, SetScopeEmailLinksCommandOutput> |
| | | 26 | | { |
| | | 27 | | public async Task<DataOutput<SetScopeEmailLinksCommandOutput?>> HandleAsync( |
| | | 28 | | SetScopeEmailLinksCommand command, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 15 | 30 | | var output = DataOutput<SetScopeEmailLinksCommandOutput?>.New; |
| | | 31 | | |
| | 15 | 32 | | var validation = await validator.ValidateAsync(command, cancellationToken); |
| | | 33 | | |
| | 15 | 34 | | if (!validation.IsValid) |
| | | 35 | | { |
| | 4 | 36 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 37 | | } |
| | | 38 | | |
| | 13 | 39 | | var scope = await scopeReader.Query() |
| | 13 | 40 | | .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted, cancellationToken); |
| | | 41 | | |
| | 13 | 42 | | if (scope is null) |
| | | 43 | | { |
| | 3 | 44 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 45 | | } |
| | | 46 | | |
| | 10 | 47 | | if (!await scopeOwnership.ActorMayManageScopeAsync(command.ActingRole, command.ActingPersonId, scope.Id)) |
| | | 48 | | { |
| | 1 | 49 | | return output.WithError(ScopeMessages.NotScopeOwner); |
| | | 50 | | } |
| | | 51 | | |
| | 9 | 52 | | var ownerIds = await scopeReader.Query() |
| | 9 | 53 | | .Where(x => x.PublicId == command.Id) |
| | 9 | 54 | | .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId)) |
| | 9 | 55 | | .ToListAsync(cancellationToken); |
| | | 56 | | |
| | 9 | 57 | | scope.EmailVerificationUrl = Normalised(command.EmailVerificationUrl); |
| | 9 | 58 | | scope.PasswordResetUrl = Normalised(command.PasswordResetUrl); |
| | 9 | 59 | | scope.UpdatedAt = DateTime.UtcNow; |
| | | 60 | | |
| | 9 | 61 | | var update = await scopeWriter.UpdateAsync(scope); |
| | | 62 | | |
| | 9 | 63 | | if (!update.Success) |
| | | 64 | | { |
| | 0 | 65 | | return output.WithErrors(update.Errors); |
| | | 66 | | } |
| | | 67 | | |
| | 9 | 68 | | return output |
| | 9 | 69 | | .WithData(new SetScopeEmailLinksCommandOutput |
| | 9 | 70 | | { |
| | 9 | 71 | | Id = scope.PublicId, |
| | 9 | 72 | | Name = scope.Name, |
| | 9 | 73 | | Description = scope.Description, |
| | 9 | 74 | | GoogleSignInEnabled = scope.GoogleSignInEnabled, |
| | 9 | 75 | | SelfRegistrationEnabled = scope.SelfRegistrationEnabled, |
| | 9 | 76 | | EmailVerificationUrl = scope.EmailVerificationUrl, |
| | 9 | 77 | | PasswordResetUrl = scope.PasswordResetUrl, |
| | 9 | 78 | | OwnerIds = ownerIds, |
| | 9 | 79 | | CreatedAt = scope.CreatedAt, |
| | 9 | 80 | | UpdatedAt = scope.UpdatedAt |
| | 9 | 81 | | }) |
| | 9 | 82 | | .WithMessage(ScopeMessages.EmailLinksUpdatedSuccessfully); |
| | 15 | 83 | | } |
| | | 84 | | |
| | | 85 | | // An empty or blank link is stored as "no link", so the senders have one way to read it. |
| | 18 | 86 | | private static string? Normalised(string? url) => string.IsNullOrWhiteSpace(url) ? null : url.Trim(); |
| | | 87 | | } |