| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 3 | | using ArturRios.Heimdall.Domain.Enums; |
| | | 4 | | using ArturRios.Heimdall.Query.Input; |
| | | 5 | | using ArturRios.Heimdall.Query.Output; |
| | | 6 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 7 | | using ArturRios.Heimdall.Shared.Services; |
| | | 8 | | using ArturRios.Mediator.Query.Interfaces; |
| | | 9 | | using ArturRios.Output; |
| | | 10 | | using FluentValidation; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | using Application = ArturRios.Heimdall.Domain.Entities.Application; |
| | | 13 | | |
| | | 14 | | namespace ArturRios.Heimdall.Query.Handlers; |
| | | 15 | | |
| | | 16 | | /// <summary> |
| | | 17 | | /// Handles <see cref="ListScopeApplicationsQuery" /> (UC-17, FR-AP-05): lists a scope's |
| | | 18 | | /// applications with pagination and optional name/owner filters, excluding logically deleted |
| | | 19 | | /// applications unless explicitly requested (FR-AP-09). A System Admin sees every application in |
| | | 20 | | /// the scope; any other actor must own the scope and sees only the applications they own. A |
| | | 21 | | /// missing or logically deleted scope is AF-17a (<c>ScopeNotFound</c>); an actor who does not own |
| | | 22 | | /// the scope is AF-17b (<c>NotScopeOwner</c>). |
| | | 23 | | /// </summary> |
| | 22 | 24 | | public class ListScopeApplicationsQueryHandler( |
| | 22 | 25 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 22 | 26 | | IAsyncReadOnlyRepository<Application, long> applicationReader, |
| | 22 | 27 | | IScopeOwnershipChecker scopeOwnership, |
| | 22 | 28 | | IValidator<ListScopeApplicationsQuery> validator) |
| | | 29 | | : IPaginatedQueryHandlerAsync<ListScopeApplicationsQuery, ApplicationOutput> |
| | | 30 | | { |
| | | 31 | | public async Task<PaginatedOutput<ApplicationOutput>> HandleAsync(ListScopeApplicationsQuery query, CancellationToke |
| | | 32 | | { |
| | 22 | 33 | | var output = PaginatedOutput<ApplicationOutput>.New; |
| | | 34 | | |
| | | 35 | | // NFR-10: page number/size bounds and filter length, validated before any query runs. |
| | 22 | 36 | | var validation = await validator.ValidateAsync(query); |
| | | 37 | | |
| | 22 | 38 | | if (!validation.IsValid) |
| | | 39 | | { |
| | 0 | 40 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 41 | | } |
| | | 42 | | |
| | | 43 | | // AF-17a: the target scope must exist and not be logically deleted. |
| | 22 | 44 | | var scope = await scopeReader.Query() |
| | 22 | 45 | | .FirstOrDefaultAsync(x => x.PublicId == query.ScopeId && !x.IsDeleted); |
| | | 46 | | |
| | 22 | 47 | | if (scope is null) |
| | | 48 | | { |
| | 4 | 49 | | return output.WithError(ApplicationMessages.ScopeNotFound); |
| | | 50 | | } |
| | | 51 | | |
| | | 52 | | // AF-17b: a Scope Admin may only list a scope they own; a System Admin bypasses. The gate is |
| | | 53 | | // not redundant with the owner narrowing below: without it, an actor probing an unrelated |
| | | 54 | | // scope would get an empty 200, indistinguishable from a scope that is genuinely empty. |
| | 18 | 55 | | if (!await scopeOwnership.ActorMayManageScopeAsync(query.ActingRole, query.ActingPersonId, scope.Id)) |
| | | 56 | | { |
| | 2 | 57 | | return output.WithError(ApplicationMessages.NotScopeOwner); |
| | | 58 | | } |
| | | 59 | | |
| | 16 | 60 | | var applications = applicationReader.Query().Where(x => x.ScopeId == scope.Id); |
| | | 61 | | |
| | | 62 | | // UC-17 step 2: a System Admin sees every application in the scope; a Scope Admin sees only |
| | | 63 | | // the ones they own, even among co-owners of the same scope. |
| | 16 | 64 | | if (query.ActingRole != (int)Roles.SystemAdmin) |
| | | 65 | | { |
| | 3 | 66 | | applications = applications.Where(x => x.Owner.PublicId == query.ActingPersonId); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | // FR-AP-09: logically deleted applications are excluded unless explicitly requested. |
| | 16 | 70 | | if (!query.IncludeDeleted) |
| | | 71 | | { |
| | 14 | 72 | | applications = applications.Where(x => !x.IsDeleted); |
| | | 73 | | } |
| | | 74 | | |
| | | 75 | | // Compared case-insensitively (LOWER() in SQL), as every name filter in this codebase is. |
| | 16 | 76 | | if (!string.IsNullOrWhiteSpace(query.Name)) |
| | | 77 | | { |
| | 2 | 78 | | var name = query.Name.ToLower(); |
| | 2 | 79 | | applications = applications.Where(x => x.Name.ToLower().Contains(name)); |
| | | 80 | | } |
| | | 81 | | |
| | 16 | 82 | | if (query.OwnerId.HasValue) |
| | | 83 | | { |
| | 2 | 84 | | applications = applications.Where(x => x.Owner.PublicId == query.OwnerId.Value); |
| | | 85 | | } |
| | | 86 | | |
| | 16 | 87 | | var projected = applications.Select(x => new ApplicationOutput |
| | 16 | 88 | | { |
| | 16 | 89 | | Id = x.PublicId, |
| | 16 | 90 | | Name = x.Name, |
| | 16 | 91 | | ScopeId = x.Scope.PublicId, |
| | 16 | 92 | | OwnerId = x.Owner.PublicId, |
| | 16 | 93 | | IsDeleted = x.IsDeleted, |
| | 16 | 94 | | CreatedAt = x.CreatedAt, |
| | 16 | 95 | | UpdatedAt = x.UpdatedAt |
| | 16 | 96 | | }); |
| | | 97 | | |
| | | 98 | | // Ordered by name with the public identifier as a tiebreaker, then paginated over that |
| | | 99 | | // ordering rather than handing PaginateAsync a sort key of its own. |
| | | 100 | | // |
| | | 101 | | // Name is not unique, and PostgreSQL gives no ordering guarantee between rows whose sort key |
| | | 102 | | // ties — each page is a separate query, free to break the tie differently. Two people called |
| | | 103 | | // "Ana Silva" straddling a page boundary could therefore appear on both pages while somebody |
| | | 104 | | // else appeared on neither. The tiebreaker makes the total order deterministic, so paging |
| | | 105 | | // through a list sees every row exactly once. |
| | 16 | 106 | | var ordered = projected.OrderBy(x => x.Name).ThenBy(x => x.Id); |
| | | 107 | | |
| | 16 | 108 | | var page = await ordered.PaginateAsync(query.PageNumber, query.PageSize, orderBy: null); |
| | | 109 | | |
| | 16 | 110 | | return page.WithMessage(ApplicationMessages.ApplicationsRetrievedSuccessfully); |
| | 22 | 111 | | } |
| | | 112 | | } |