| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Data.Relational.Core.Repositories; |
| | | 3 | | using ArturRios.Heimdall.Command.Input; |
| | | 4 | | using ArturRios.Heimdall.Command.Output; |
| | | 5 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Heimdall.Shared.Services; |
| | | 8 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 9 | | using ArturRios.Output; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Handles <see cref="DiscardPendingRegistrationCommand" /> (UC-50, FR-SR-12): removes a scope's |
| | | 16 | | /// pending self-registration, so its mailed link stops working and confirming it behaves as for any |
| | | 17 | | /// unknown token (UC-14 AF-14c). The registrant may simply sign up again. |
| | | 18 | | /// </summary> |
| | | 19 | | /// <remarks> |
| | | 20 | | /// <para> |
| | | 21 | | /// Same access rule as UC-49: a System Admin, or an owner of the scope. A missing or logically |
| | | 22 | | /// deleted scope is AF-50a; a caller who may not manage it is AF-50b; a registration that is not |
| | | 23 | | /// this scope's is AF-50c, indistinguishable from one that does not exist. |
| | | 24 | | /// </para> |
| | | 25 | | /// <para> |
| | | 26 | | /// A delete that loses a race — the registrant confirmed, or the purge removed the row, after |
| | | 27 | | /// the lookup — is reported as not found. The row is gone either way, and a conflict would ask |
| | | 28 | | /// the owner to retry something there is nothing left to do. |
| | | 29 | | /// </para> |
| | | 30 | | /// </remarks> |
| | 15 | 31 | | public class DiscardPendingRegistrationCommandHandler( |
| | 15 | 32 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 15 | 33 | | IAsyncReadOnlyRepository<PendingRegistration, long> pendingReader, |
| | 15 | 34 | | IAsyncRepository<PendingRegistration, long> pendingWriter, |
| | 15 | 35 | | IScopeOwnershipChecker scopeOwnership) |
| | | 36 | | : ICommandHandlerAsync<DiscardPendingRegistrationCommand, DiscardPendingRegistrationCommandOutput> |
| | | 37 | | { |
| | | 38 | | public async Task<DataOutput<DiscardPendingRegistrationCommandOutput?>> HandleAsync( |
| | | 39 | | DiscardPendingRegistrationCommand command, CancellationToken cancellationToken = default) |
| | | 40 | | { |
| | 15 | 41 | | var output = DataOutput<DiscardPendingRegistrationCommandOutput?>.New; |
| | | 42 | | |
| | | 43 | | // AF-50a. |
| | 15 | 44 | | var scope = await scopeReader.Query() |
| | 15 | 45 | | .FirstOrDefaultAsync(x => x.PublicId == command.ScopeId && !x.IsDeleted, cancellationToken); |
| | | 46 | | |
| | 15 | 47 | | if (scope is null) |
| | | 48 | | { |
| | 3 | 49 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // AF-50b. Before the registration lookup, so a non-owner cannot learn which ids exist. |
| | 12 | 53 | | if (!await scopeOwnership.ActorMayManageScopeAsync(command.ActingRole, command.ActingPersonId, scope.Id)) |
| | | 54 | | { |
| | 2 | 55 | | return output.WithError(ScopeMessages.NotScopeOwner); |
| | | 56 | | } |
| | | 57 | | |
| | | 58 | | // AF-50c: looked up within the scope, so another scope's registration is simply not found. |
| | 10 | 59 | | var pending = await pendingReader.Query() |
| | 10 | 60 | | .FirstOrDefaultAsync( |
| | 10 | 61 | | x => x.PublicId == command.RegistrationId && x.ScopeId == scope.Id, cancellationToken); |
| | | 62 | | |
| | 10 | 63 | | if (pending is null) |
| | | 64 | | { |
| | 4 | 65 | | return output.WithError(ScopeMessages.PendingRegistrationNotFound); |
| | | 66 | | } |
| | | 67 | | |
| | 6 | 68 | | var removal = await pendingWriter.DeleteRangeAsync([pending.Id], cancellationToken); |
| | | 69 | | |
| | 6 | 70 | | if (!removal.Success) |
| | | 71 | | { |
| | 2 | 72 | | return removal.Errors.Contains(RelationalErrors.ConcurrencyMessage) |
| | 2 | 73 | | ? output.WithError(ScopeMessages.PendingRegistrationNotFound) |
| | 2 | 74 | | : output.WithErrors(removal.Errors); |
| | | 75 | | } |
| | | 76 | | |
| | 4 | 77 | | if ((removal.Data?.Count() ?? 0) == 0) |
| | | 78 | | { |
| | 0 | 79 | | return output.WithError(ScopeMessages.PendingRegistrationNotFound); |
| | | 80 | | } |
| | | 81 | | |
| | 4 | 82 | | return output |
| | 4 | 83 | | .WithData(new DiscardPendingRegistrationCommandOutput { Id = pending.PublicId }) |
| | 4 | 84 | | .WithMessage(ScopeMessages.PendingRegistrationDiscarded); |
| | 15 | 85 | | } |
| | | 86 | | } |