| | | 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.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="UpdateScopeCommand" /> (UC-03): validates the request, verifies the scope |
| | | 16 | | /// exists and is not logically deleted (AF-03a), verifies the new name does not collide with |
| | | 17 | | /// another scope (AF-03b), then applies the changes and stamps <c>UpdatedAt</c>. All failures are |
| | | 18 | | /// returned as errors on the <see cref="DataOutput{T}" /> rather than thrown, using the canonical |
| | | 19 | | /// <see cref="ScopeMessages" /> so the response resolver can pick the matching status code. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// The changes are one targeted write of the columns this request carries (#205). Google sign-in, |
| | | 23 | | /// self-registration and the email links are left as they stand: they have endpoints of their own |
| | | 24 | | /// (UC-24, FR-SR-02, the email-links one), and this request does not carry them, so it never |
| | | 25 | | /// changes them. |
| | | 26 | | /// </remarks> |
| | 18 | 27 | | public class UpdateScopeCommandHandler( |
| | 18 | 28 | | IValidator<UpdateScopeCommand> validator, |
| | 18 | 29 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 18 | 30 | | IAtomicWrites atomicWrites) |
| | | 31 | | : ICommandHandlerAsync<UpdateScopeCommand, UpdateScopeCommandOutput> |
| | | 32 | | { |
| | | 33 | | public async Task<DataOutput<UpdateScopeCommandOutput?>> HandleAsync(UpdateScopeCommand command, CancellationToken c |
| | | 34 | | { |
| | 18 | 35 | | var output = DataOutput<UpdateScopeCommandOutput?>.New; |
| | | 36 | | |
| | | 37 | | // Step 2: validate input fields. |
| | 18 | 38 | | var validation = await validator.ValidateAsync(command); |
| | | 39 | | |
| | 18 | 40 | | if (!validation.IsValid) |
| | | 41 | | { |
| | 4 | 42 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 43 | | } |
| | | 44 | | |
| | | 45 | | // Step 3 (AF-03a): the scope must exist and not be logically deleted. |
| | 16 | 46 | | var scope = await scopeReader.Query() |
| | 16 | 47 | | .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted); |
| | | 48 | | |
| | 16 | 49 | | if (scope is null) |
| | | 50 | | { |
| | 4 | 51 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | // Step 4 (AF-03b): the new name must not collide with another scope, compared |
| | | 55 | | // case-insensitively (LOWER() in SQL) so the app layer authoritatively rejects case variants |
| | | 56 | | // that the case-sensitive DB unique index on Name would not catch. Checked against all scopes |
| | | 57 | | // (deleted included), excluding this scope so an unchanged name is not a false conflict. |
| | 12 | 58 | | var nameTaken = await scopeReader.Query() |
| | 12 | 59 | | .AnyAsync(x => x.Name.ToLower() == command.Name.ToLower() && x.PublicId != command.Id); |
| | | 60 | | |
| | 12 | 61 | | if (nameTaken) |
| | | 62 | | { |
| | 3 | 63 | | return output.WithError(ScopeMessages.NameAlreadyExists); |
| | | 64 | | } |
| | | 65 | | |
| | | 66 | | // Owner PublicIds for the response. A projection over the navigation (no Include needed), |
| | | 67 | | // which EF translates to a join and the in-memory fake evaluates directly. |
| | 9 | 68 | | var ownerIds = await scopeReader.Query() |
| | 9 | 69 | | .Where(x => x.PublicId == command.Id) |
| | 9 | 70 | | .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId)) |
| | 9 | 71 | | .ToListAsync(); |
| | | 72 | | |
| | | 73 | | // Step 4 (main flow): apply the updates and stamp UpdatedAt (no DB trigger maintains it) — those |
| | | 74 | | // columns only, and only while the scope is not deleted (#205). A whole-row save of the copy read |
| | | 75 | | // above would write the settings back as read, over a change their own endpoints made meanwhile, |
| | | 76 | | // and the deletion flag with them. |
| | 9 | 77 | | var update = await atomicWrites.TryUpdateScopeDetailsAsync(scope, command.Name, command.Description, |
| | 9 | 78 | | command.DefaultLegalBasis, command.PrivacyNoticeUri, DateTime.UtcNow); |
| | | 79 | | |
| | | 80 | | switch (update) |
| | | 81 | | { |
| | | 82 | | // AF-03a: deleted since the read above. |
| | | 83 | | case ScopeUpdateOutcome.NotFound: |
| | 2 | 84 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 85 | | // AF-03b: the unique index on the name, for a name another scope took since the check above. |
| | | 86 | | case ScopeUpdateOutcome.NameTaken: |
| | 1 | 87 | | return output.WithError(ScopeMessages.NameAlreadyExists); |
| | | 88 | | } |
| | | 89 | | |
| | | 90 | | // Step 5: return the updated scope. |
| | 6 | 91 | | return output |
| | 6 | 92 | | .WithData(new UpdateScopeCommandOutput |
| | 6 | 93 | | { |
| | 6 | 94 | | Id = scope.PublicId, |
| | 6 | 95 | | Name = scope.Name, |
| | 6 | 96 | | Description = scope.Description, |
| | 6 | 97 | | GoogleSignInEnabled = scope.GoogleSignInEnabled, |
| | 6 | 98 | | SelfRegistrationEnabled = scope.SelfRegistrationEnabled, |
| | 6 | 99 | | EmailVerificationUrl = scope.EmailVerificationUrl, |
| | 6 | 100 | | PasswordResetUrl = scope.PasswordResetUrl, |
| | 6 | 101 | | OwnerIds = ownerIds, |
| | 6 | 102 | | CreatedAt = scope.CreatedAt, |
| | 6 | 103 | | UpdatedAt = scope.UpdatedAt |
| | 6 | 104 | | }) |
| | 6 | 105 | | .WithMessage(ScopeMessages.ScopeUpdatedSuccessfully); |
| | 18 | 106 | | } |
| | | 107 | | } |