| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 3 | | using ArturRios.Heimdall.Query.Input; |
| | | 4 | | using ArturRios.Heimdall.Query.Output; |
| | | 5 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 6 | | using ArturRios.Heimdall.Shared.Services; |
| | | 7 | | using ArturRios.Mediator.Query.Interfaces; |
| | | 8 | | using ArturRios.Output; |
| | | 9 | | using FluentValidation; |
| | | 10 | | using Microsoft.EntityFrameworkCore; |
| | | 11 | | |
| | | 12 | | namespace ArturRios.Heimdall.Query.Handlers; |
| | | 13 | | |
| | | 14 | | /// <summary> |
| | | 15 | | /// Handles <see cref="ListScopeGoogleUsersQuery" /> (UC-27, FR-GO-14): lists the Google Users of a |
| | | 16 | | /// scope with pagination and optional name/email filters, excluding logically deleted records |
| | | 17 | | /// unless explicitly requested (FR-GO-17). A missing or logically deleted scope is AF-27a |
| | | 18 | | /// (<c>ScopeNotFound</c>); an actor who does not own the scope is AF-27b (<c>NotScopeOwner</c>). |
| | | 19 | | /// A System Admin bypasses the ownership check. |
| | | 20 | | /// </summary> |
| | | 21 | | /// <remarks> |
| | | 22 | | /// The counterpart of <see cref="GetGoogleUserByIdQueryHandler" />, and deliberately stricter: the |
| | | 23 | | /// authorization matrix grants a Google User a read of *themselves*, never a listing, so there is |
| | | 24 | | /// no self-read branch here. The <c>RoleRequirement</c> on the endpoint keeps every <c>User</c> |
| | | 25 | | /// out before this runs, and this check answers for the Scope Admin the attribute cannot judge. |
| | | 26 | | /// </remarks> |
| | 16 | 27 | | public class ListScopeGoogleUsersQueryHandler( |
| | 16 | 28 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 16 | 29 | | IAsyncReadOnlyRepository<GoogleUser, long> googleUserReader, |
| | 16 | 30 | | IScopeOwnershipChecker scopeOwnership, |
| | 16 | 31 | | IValidator<ListScopeGoogleUsersQuery> validator) |
| | | 32 | | : IPaginatedQueryHandlerAsync<ListScopeGoogleUsersQuery, GoogleUserOutput> |
| | | 33 | | { |
| | | 34 | | public async Task<PaginatedOutput<GoogleUserOutput>> HandleAsync(ListScopeGoogleUsersQuery query, CancellationToken |
| | | 35 | | { |
| | 16 | 36 | | var output = PaginatedOutput<GoogleUserOutput>.New; |
| | | 37 | | |
| | | 38 | | // NFR-10: page number/size bounds and filter length, validated before any query runs. |
| | 16 | 39 | | var validation = await validator.ValidateAsync(query); |
| | | 40 | | |
| | 16 | 41 | | if (!validation.IsValid) |
| | | 42 | | { |
| | 0 | 43 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 44 | | } |
| | | 45 | | |
| | | 46 | | // AF-27a: the target scope must exist and not be logically deleted. |
| | 16 | 47 | | var scope = await scopeReader.Query() |
| | 16 | 48 | | .FirstOrDefaultAsync(x => x.PublicId == query.ScopeId && !x.IsDeleted); |
| | | 49 | | |
| | 16 | 50 | | if (scope is null) |
| | | 51 | | { |
| | 4 | 52 | | return output.WithError(GoogleUserMessages.ScopeNotFound); |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | // AF-27b: a Scope Admin may only read a scope they own; a System Admin bypasses. |
| | 12 | 56 | | if (!await scopeOwnership.ActorMayManageScopeAsync(query.ActingRole, query.ActingPersonId, scope.Id)) |
| | | 57 | | { |
| | 2 | 58 | | return output.WithError(GoogleUserMessages.NotScopeOwner); |
| | | 59 | | } |
| | | 60 | | |
| | 10 | 61 | | var googleUsers = googleUserReader.Query().Where(x => x.ScopeId == scope.Id); |
| | | 62 | | |
| | | 63 | | // UC-27 step 3 (FR-GO-17). |
| | | 64 | | // NFR-24: a restricted identity is withheld from tenant-facing listings, whatever |
| | | 65 | | // IncludeDeleted says — the two states are independent. Not hidden from everyone: the |
| | | 66 | | // subject's own export still returns it, and a System Admin resolving the dispute can still |
| | | 67 | | // reach it by id. But a scope's administrator working through their user list has no |
| | | 68 | | // business acting on a record whose processing is suspended. |
| | 10 | 69 | | googleUsers = googleUsers.Where(x => x.ProcessingRestrictedAt == null); |
| | | 70 | | |
| | 10 | 71 | | if (!query.IncludeDeleted) |
| | | 72 | | { |
| | 8 | 73 | | googleUsers = googleUsers.Where(x => !x.IsDeleted); |
| | | 74 | | } |
| | | 75 | | |
| | | 76 | | // FR-GO-14's filtering, in the vocabulary ListScopePersonsQueryHandler already uses: |
| | | 77 | | // case-insensitive substring matches, which translate to LOWER() … LIKE in SQL. |
| | 10 | 78 | | if (!string.IsNullOrWhiteSpace(query.Name)) |
| | | 79 | | { |
| | 2 | 80 | | var name = query.Name.ToLower(); |
| | 2 | 81 | | googleUsers = googleUsers.Where(x => x.Name.ToLower().Contains(name)); |
| | | 82 | | } |
| | | 83 | | |
| | 10 | 84 | | if (!string.IsNullOrWhiteSpace(query.Email)) |
| | | 85 | | { |
| | 2 | 86 | | var email = query.Email.ToLower(); |
| | 2 | 87 | | googleUsers = googleUsers.Where(x => x.Email.ToLower().Contains(email)); |
| | | 88 | | } |
| | | 89 | | |
| | 10 | 90 | | var projected = googleUsers.Select(x => new GoogleUserOutput |
| | 10 | 91 | | { |
| | 10 | 92 | | Id = x.PublicId, |
| | 10 | 93 | | GoogleId = x.GoogleId, |
| | 10 | 94 | | Name = x.Name, |
| | 10 | 95 | | Email = x.Email, |
| | 10 | 96 | | EmailVerified = x.EmailVerified, |
| | 10 | 97 | | ProfilePictureUrl = x.ProfilePictureUrl, |
| | 10 | 98 | | IsDeleted = x.IsDeleted, |
| | 10 | 99 | | ScopeId = x.Scope.PublicId, |
| | 10 | 100 | | CreatedAt = x.CreatedAt, |
| | 10 | 101 | | UpdatedAt = x.UpdatedAt |
| | 10 | 102 | | }); |
| | | 103 | | |
| | | 104 | | // Ordered by name with the public identifier as a tiebreaker, then paginated over that |
| | | 105 | | // ordering rather than handing PaginateAsync a sort key of its own. |
| | | 106 | | // |
| | | 107 | | // Name is not unique, and PostgreSQL gives no ordering guarantee between rows whose sort key |
| | | 108 | | // ties — each page is a separate query, free to break the tie differently. Two people called |
| | | 109 | | // "Ana Silva" straddling a page boundary could therefore appear on both pages while somebody |
| | | 110 | | // else appeared on neither. The tiebreaker makes the total order deterministic, so paging |
| | | 111 | | // through a list sees every row exactly once. |
| | 10 | 112 | | var ordered = projected.OrderBy(x => x.Name).ThenBy(x => x.Id); |
| | | 113 | | |
| | 10 | 114 | | var page = await ordered.PaginateAsync(query.PageNumber, query.PageSize, orderBy: null); |
| | | 115 | | |
| | | 116 | | // UC-27 step 4. |
| | 10 | 117 | | return page.WithMessage(GoogleUserMessages.GoogleUsersRetrievedSuccessfully); |
| | 16 | 118 | | } |
| | | 119 | | } |