| | | 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.Mediator.Command.Interfaces; |
| | | 7 | | using ArturRios.Output; |
| | | 8 | | using FluentValidation; |
| | | 9 | | using Microsoft.EntityFrameworkCore; |
| | | 10 | | |
| | | 11 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 12 | | |
| | | 13 | | /// <summary> |
| | | 14 | | /// Handles <see cref="UpdateScopeCommand" /> (UC-03): validates the request, verifies the scope |
| | | 15 | | /// exists and is not logically deleted (AF-03a), verifies the new name does not collide with |
| | | 16 | | /// another scope (AF-03b), then applies the changes and stamps <c>UpdatedAt</c>. All failures are |
| | | 17 | | /// returned as errors on the <see cref="DataOutput{T}" /> rather than thrown, using the canonical |
| | | 18 | | /// <see cref="ScopeMessages" /> so the response resolver can pick the matching status code. |
| | | 19 | | /// </summary> |
| | 14 | 20 | | public class UpdateScopeCommandHandler( |
| | 14 | 21 | | IValidator<UpdateScopeCommand> validator, |
| | 14 | 22 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 14 | 23 | | IAsyncRepository<Scope, long> scopeWriter) |
| | | 24 | | : ICommandHandlerAsync<UpdateScopeCommand, UpdateScopeCommandOutput> |
| | | 25 | | { |
| | | 26 | | public async Task<DataOutput<UpdateScopeCommandOutput?>> HandleAsync(UpdateScopeCommand command, CancellationToken c |
| | | 27 | | { |
| | 14 | 28 | | var output = DataOutput<UpdateScopeCommandOutput?>.New; |
| | | 29 | | |
| | | 30 | | // Step 2: validate input fields. |
| | 14 | 31 | | var validation = await validator.ValidateAsync(command); |
| | | 32 | | |
| | 14 | 33 | | if (!validation.IsValid) |
| | | 34 | | { |
| | 4 | 35 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 36 | | } |
| | | 37 | | |
| | | 38 | | // Step 3 (AF-03a): the scope must exist and not be logically deleted. |
| | 12 | 39 | | var scope = await scopeReader.Query() |
| | 12 | 40 | | .FirstOrDefaultAsync(x => x.PublicId == command.Id && !x.IsDeleted); |
| | | 41 | | |
| | 12 | 42 | | if (scope is null) |
| | | 43 | | { |
| | 4 | 44 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 45 | | } |
| | | 46 | | |
| | | 47 | | // Step 4 (AF-03b): the new name must not collide with another scope, compared |
| | | 48 | | // case-insensitively (LOWER() in SQL) so the app layer authoritatively rejects case variants |
| | | 49 | | // that the case-sensitive DB unique index on Name would not catch. Checked against all scopes |
| | | 50 | | // (deleted included), excluding this scope so an unchanged name is not a false conflict. |
| | 8 | 51 | | var nameTaken = await scopeReader.Query() |
| | 8 | 52 | | .AnyAsync(x => x.Name.ToLower() == command.Name.ToLower() && x.PublicId != command.Id); |
| | | 53 | | |
| | 8 | 54 | | if (nameTaken) |
| | | 55 | | { |
| | 3 | 56 | | return output.WithError(ScopeMessages.NameAlreadyExists); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | // Owner PublicIds for the response. A projection over the navigation (no Include needed), |
| | | 60 | | // which EF translates to a join and the in-memory fake evaluates directly. |
| | 5 | 61 | | var ownerIds = await scopeReader.Query() |
| | 5 | 62 | | .Where(x => x.PublicId == command.Id) |
| | 5 | 63 | | .SelectMany(x => x.Owners.Select(owner => owner.Person.PublicId)) |
| | 5 | 64 | | .ToListAsync(); |
| | | 65 | | |
| | | 66 | | // Step 4 (main flow): apply the updates and stamp UpdatedAt (no DB trigger maintains it). |
| | 5 | 67 | | scope.Name = command.Name; |
| | 5 | 68 | | scope.Description = command.Description; |
| | 5 | 69 | | scope.DefaultLegalBasis = command.DefaultLegalBasis; |
| | 5 | 70 | | scope.PrivacyNoticeUri = command.PrivacyNoticeUri; |
| | 5 | 71 | | scope.UpdatedAt = DateTime.UtcNow; |
| | | 72 | | |
| | 5 | 73 | | var update = await scopeWriter.UpdateAsync(scope); |
| | | 74 | | |
| | 5 | 75 | | if (!update.Success) |
| | | 76 | | { |
| | 0 | 77 | | return output.WithErrors(update.Errors); |
| | | 78 | | } |
| | | 79 | | |
| | | 80 | | // Step 5: return the updated scope. |
| | 5 | 81 | | return output |
| | 5 | 82 | | .WithData(new UpdateScopeCommandOutput |
| | 5 | 83 | | { |
| | 5 | 84 | | Id = scope.PublicId, |
| | 5 | 85 | | Name = scope.Name, |
| | 5 | 86 | | Description = scope.Description, |
| | 5 | 87 | | GoogleSignInEnabled = scope.GoogleSignInEnabled, |
| | 5 | 88 | | SelfRegistrationEnabled = scope.SelfRegistrationEnabled, |
| | 5 | 89 | | EmailVerificationUrl = scope.EmailVerificationUrl, |
| | 5 | 90 | | PasswordResetUrl = scope.PasswordResetUrl, |
| | 5 | 91 | | OwnerIds = ownerIds, |
| | 5 | 92 | | CreatedAt = scope.CreatedAt, |
| | 5 | 93 | | UpdatedAt = scope.UpdatedAt |
| | 5 | 94 | | }) |
| | 5 | 95 | | .WithMessage(ScopeMessages.ScopeUpdatedSuccessfully); |
| | 14 | 96 | | } |
| | | 97 | | } |