| | | 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="ListScopeOwnerInvitationsQuery" /> (UC-58, FR-SC-15): a scope's open owner |
| | | 16 | | /// invitations, newest first, for a System Admin or an owner of the scope. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// A missing or logically deleted scope is AF-58a; a caller who may not manage it is AF-58b. Only open |
| | | 20 | | /// invitations are listed — not ended, not expired — and not those of a logically deleted invitee, who |
| | | 21 | | /// could never accept, nor of a processing-restricted one (NFR-24). The inviter's name is shown only for |
| | | 22 | | /// a live, unrestricted inviter. The listing answers 200 with no message. |
| | | 23 | | /// </remarks> |
| | 13 | 24 | | public class ListScopeOwnerInvitationsQueryHandler( |
| | 13 | 25 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 13 | 26 | | IAsyncReadOnlyRepository<ScopeOwnerInvitation, long> invitationReader, |
| | 13 | 27 | | IScopeOwnershipChecker scopeOwnership, |
| | 13 | 28 | | IValidator<ListScopeOwnerInvitationsQuery> validator) |
| | | 29 | | : IPaginatedQueryHandlerAsync<ListScopeOwnerInvitationsQuery, ScopeOwnerInvitationOutput> |
| | | 30 | | { |
| | | 31 | | public async Task<PaginatedOutput<ScopeOwnerInvitationOutput>> HandleAsync( |
| | | 32 | | ListScopeOwnerInvitationsQuery query, CancellationToken cancellationToken = default) |
| | | 33 | | { |
| | 13 | 34 | | var output = PaginatedOutput<ScopeOwnerInvitationOutput>.New; |
| | | 35 | | |
| | | 36 | | // NFR-10: page bounds, before any query runs. |
| | 13 | 37 | | var validation = await validator.ValidateAsync(query, cancellationToken); |
| | | 38 | | |
| | 13 | 39 | | if (!validation.IsValid) |
| | | 40 | | { |
| | 2 | 41 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 42 | | } |
| | | 43 | | |
| | | 44 | | // AF-58a. |
| | 12 | 45 | | var scope = await scopeReader.Query() |
| | 12 | 46 | | .FirstOrDefaultAsync(x => x.PublicId == query.ScopeId && !x.IsDeleted, cancellationToken); |
| | | 47 | | |
| | 12 | 48 | | if (scope is null) |
| | | 49 | | { |
| | 1 | 50 | | return output.WithError(ScopeMessages.ScopeNotFound); |
| | | 51 | | } |
| | | 52 | | |
| | | 53 | | // AF-58b. |
| | 11 | 54 | | if (!await scopeOwnership.ActorMayManageScopeAsync( |
| | 11 | 55 | | query.ActingRole, query.ActingPersonId, scope.Id, cancellationToken)) |
| | | 56 | | { |
| | 2 | 57 | | return output.WithError(ScopeMessages.NotScopeOwner); |
| | | 58 | | } |
| | | 59 | | |
| | 9 | 60 | | var now = DateTime.UtcNow; |
| | | 61 | | |
| | | 62 | | // Newest first with the internal id as tiebreaker, ordered on the entity because the id never |
| | | 63 | | // reaches the output, then projected and paginated over that ordering. |
| | | 64 | | // NFR-24: a restricted identity is withheld from tenant-facing listings — the invitee's row and the |
| | | 65 | | // inviter's name alike, as ListScopeOwners does. |
| | 9 | 66 | | var projected = invitationReader.Query() |
| | 9 | 67 | | .Where(x => x.ScopeId == scope.Id && !x.Invitee.IsDeleted && x.Invitee.ProcessingRestrictedAt == null) |
| | 9 | 68 | | .Where(ScopeOwnerInvitation.OpenAt(now)) |
| | 9 | 69 | | .OrderByDescending(x => x.CreatedAt) |
| | 9 | 70 | | .ThenByDescending(x => x.Id) |
| | 9 | 71 | | .Select(x => new ScopeOwnerInvitationOutput |
| | 9 | 72 | | { |
| | 9 | 73 | | Id = x.PublicId, |
| | 9 | 74 | | InviteeId = x.Invitee.PublicId, |
| | 9 | 75 | | InviteeName = x.Invitee.Name, |
| | 9 | 76 | | InviteeEmail = x.Invitee.Email, |
| | 9 | 77 | | InvitedByName = |
| | 9 | 78 | | x.InvitedBy != null && !x.InvitedBy.IsDeleted && x.InvitedBy.ProcessingRestrictedAt == null |
| | 9 | 79 | | ? x.InvitedBy.Name |
| | 9 | 80 | | : null, |
| | 9 | 81 | | CreatedAt = x.CreatedAt, |
| | 9 | 82 | | ExpiresAt = x.ExpiresAt |
| | 9 | 83 | | }); |
| | | 84 | | |
| | 9 | 85 | | return await projected.PaginateAsync(query.PageNumber, query.PageSize, orderBy: null, cancellationToken: cancell |
| | 13 | 86 | | } |
| | | 87 | | } |