| | | 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.Enums; |
| | | 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="CreateScopeCommand" /> (UC-01): validates the request, verifies the scope |
| | | 16 | | /// name is unique and every owner is an existing, non-logically-deleted administrator — a |
| | | 17 | | /// <c>ScopeAdmin</c> or a <c>SystemAdmin</c> (FR-SC-08) — then |
| | | 18 | | /// persists the scope together with a <c>SCOPE_OWNER</c> row for each owner. All failures are |
| | | 19 | | /// returned as errors on the <see cref="DataOutput{T}" /> rather than thrown, using the canonical |
| | | 20 | | /// <see cref="ScopeMessages" /> so the response resolver can pick the matching status code. |
| | | 21 | | /// </summary> |
| | 15 | 22 | | public class CreateScopeCommandHandler( |
| | 15 | 23 | | IValidator<CreateScopeCommand> validator, |
| | 15 | 24 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 15 | 25 | | IAsyncReadOnlyRepository<Person, long> personReader, |
| | 15 | 26 | | IAsyncRepository<Scope, long> scopeWriter) |
| | | 27 | | : ICommandHandlerAsync<CreateScopeCommand, CreateScopeCommandOutput> |
| | | 28 | | { |
| | | 29 | | public async Task<DataOutput<CreateScopeCommandOutput?>> HandleAsync(CreateScopeCommand command, CancellationToken c |
| | | 30 | | { |
| | 15 | 31 | | var output = DataOutput<CreateScopeCommandOutput?>.New; |
| | | 32 | | |
| | | 33 | | // Step 2 (AF-01b): validate input fields. |
| | 15 | 34 | | var validation = await validator.ValidateAsync(command); |
| | | 35 | | |
| | 15 | 36 | | if (!validation.IsValid) |
| | | 37 | | { |
| | 8 | 38 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 39 | | } |
| | | 40 | | |
| | 11 | 41 | | var ownerIds = command.OwnerIds.Distinct().ToList(); |
| | | 42 | | |
| | | 43 | | // Step 3 (AF-01a): the scope name must be unique, compared case-insensitively (LOWER() in SQL). |
| | 11 | 44 | | var nameAlreadyExists = await scopeReader.Query() |
| | 11 | 45 | | .AnyAsync(x => x.Name.ToLower() == command.Name.ToLower()); |
| | | 46 | | |
| | 11 | 47 | | if (nameAlreadyExists) |
| | | 48 | | { |
| | 4 | 49 | | output.AddError(ScopeMessages.NameAlreadyExists); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // Step 4 (AF-01d): every owner must be an existing, non-logically-deleted administrator — |
| | | 53 | | // a ScopeAdmin, or a SystemAdmin, who may own a scope as well as govern it (FR-SC-08). The |
| | | 54 | | // roles are identified by their Roles enum values, which DatabaseSeeder writes as the role |
| | | 55 | | // rows' ids on every startup — the same way every other handler resolves a role. |
| | 11 | 56 | | var owners = await personReader.Query() |
| | 11 | 57 | | .Where(x => ownerIds.Contains(x.PublicId) && !x.IsDeleted && |
| | 11 | 58 | | (x.RoleId == (long)Roles.ScopeAdmin || x.RoleId == (long)Roles.SystemAdmin)) |
| | 11 | 59 | | .ToListAsync(); |
| | | 60 | | |
| | 11 | 61 | | if (owners.Count != ownerIds.Count) |
| | | 62 | | { |
| | 3 | 63 | | output.AddError(ScopeMessages.OwnerNotValidScopeAdmin); |
| | | 64 | | } |
| | | 65 | | |
| | 11 | 66 | | if (!output.Success) |
| | | 67 | | { |
| | 7 | 68 | | return output; |
| | | 69 | | } |
| | | 70 | | |
| | | 71 | | // Step 5: create the scope with a SCOPE_OWNER row for each owner (inserted atomically). |
| | 4 | 72 | | var scope = new Scope |
| | 4 | 73 | | { |
| | 4 | 74 | | Name = command.Name, |
| | 4 | 75 | | Description = command.Description, |
| | 4 | 76 | | Owners = [.. owners.Select(owner => new ScopeOwner { PersonId = owner.Id })] |
| | 4 | 77 | | }; |
| | | 78 | | |
| | 4 | 79 | | var creation = await scopeWriter.CreateAsync(scope); |
| | | 80 | | |
| | 4 | 81 | | if (!creation.Success) |
| | | 82 | | { |
| | 0 | 83 | | return output.WithErrors(creation.Errors); |
| | | 84 | | } |
| | | 85 | | |
| | | 86 | | // Step 6: return the created scope. |
| | 4 | 87 | | return output |
| | 4 | 88 | | .WithData(new CreateScopeCommandOutput |
| | 4 | 89 | | { |
| | 4 | 90 | | Id = scope.PublicId, |
| | 4 | 91 | | Name = scope.Name, |
| | 4 | 92 | | Description = scope.Description, |
| | 4 | 93 | | GoogleSignInEnabled = scope.GoogleSignInEnabled, |
| | 4 | 94 | | SelfRegistrationEnabled = scope.SelfRegistrationEnabled, |
| | 4 | 95 | | EmailVerificationUrl = scope.EmailVerificationUrl, |
| | 4 | 96 | | PasswordResetUrl = scope.PasswordResetUrl, |
| | 4 | 97 | | OwnerIds = [.. owners.Select(owner => owner.PublicId)], |
| | 4 | 98 | | CreatedAt = scope.CreatedAt |
| | 4 | 99 | | }) |
| | 4 | 100 | | .WithMessage(ScopeMessages.ScopeCreatedSuccessfully); |
| | 15 | 101 | | } |
| | | 102 | | } |