| | | 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.Mediator.Query.Interfaces; |
| | | 6 | | using ArturRios.Output; |
| | | 7 | | using FluentValidation; |
| | | 8 | | using Microsoft.EntityFrameworkCore; |
| | | 9 | | |
| | | 10 | | namespace ArturRios.Heimdall.Query.Handlers; |
| | | 11 | | |
| | | 12 | | /// <summary> |
| | | 13 | | /// Handles <see cref="ListMyScopeInvitationsQuery" /> (UC-59, FR-SC-16): the caller's own open scope owner |
| | | 14 | | /// invitations, soonest to expire first, for any signed-in Scope Admin or System Admin. |
| | | 15 | | /// </summary> |
| | | 16 | | /// <remarks> |
| | | 17 | | /// Only open invitations, and not those of a logically deleted scope, which accepting would only void. |
| | | 18 | | /// The inviter's name is shown only for a live, unrestricted inviter (NFR-24). The listing answers 200 |
| | | 19 | | /// with no message, and an empty page when there are none. |
| | | 20 | | /// </remarks> |
| | 7 | 21 | | public class ListMyScopeInvitationsQueryHandler( |
| | 7 | 22 | | IAsyncReadOnlyRepository<Person, long> personReader, |
| | 7 | 23 | | IAsyncReadOnlyRepository<ScopeOwnerInvitation, long> invitationReader, |
| | 7 | 24 | | IValidator<ListMyScopeInvitationsQuery> validator) |
| | | 25 | | : IPaginatedQueryHandlerAsync<ListMyScopeInvitationsQuery, MyScopeInvitationOutput> |
| | | 26 | | { |
| | | 27 | | public async Task<PaginatedOutput<MyScopeInvitationOutput>> HandleAsync( |
| | | 28 | | ListMyScopeInvitationsQuery query, CancellationToken cancellationToken = default) |
| | | 29 | | { |
| | 7 | 30 | | var output = PaginatedOutput<MyScopeInvitationOutput>.New; |
| | | 31 | | |
| | | 32 | | // NFR-10: page bounds, before any query runs. |
| | 7 | 33 | | var validation = await validator.ValidateAsync(query, cancellationToken); |
| | | 34 | | |
| | 7 | 35 | | if (!validation.IsValid) |
| | | 36 | | { |
| | 2 | 37 | | return output.WithErrors(validation.Errors.Select(failure => failure.ErrorMessage)); |
| | | 38 | | } |
| | | 39 | | |
| | | 40 | | // The liveness filter has already refused a caller whose record is gone; id 0 matches no row. |
| | 6 | 41 | | var actor = await personReader.Query() |
| | 6 | 42 | | .FirstOrDefaultAsync(x => x.PublicId == query.ActingPersonId, cancellationToken); |
| | 6 | 43 | | var actorId = actor?.Id ?? 0; |
| | 6 | 44 | | var now = DateTime.UtcNow; |
| | | 45 | | |
| | | 46 | | // Soonest to expire first with the internal id as tiebreaker, ordered on the entity because the id |
| | | 47 | | // never reaches the output, then projected and paginated over that ordering. |
| | 6 | 48 | | var projected = invitationReader.Query() |
| | 6 | 49 | | .Where(x => x.InviteePersonId == actorId && !x.Scope.IsDeleted) |
| | 6 | 50 | | .Where(ScopeOwnerInvitation.OpenAt(now)) |
| | 6 | 51 | | .OrderBy(x => x.ExpiresAt) |
| | 6 | 52 | | .ThenBy(x => x.Id) |
| | 6 | 53 | | .Select(x => new MyScopeInvitationOutput |
| | 6 | 54 | | { |
| | 6 | 55 | | Id = x.PublicId, |
| | 6 | 56 | | ScopeId = x.Scope.PublicId, |
| | 6 | 57 | | ScopeName = x.Scope.Name, |
| | 6 | 58 | | // NFR-24: a removed or restricted inviter's name is withheld, as every person listing does. |
| | 6 | 59 | | InvitedByName = |
| | 6 | 60 | | x.InvitedBy != null && !x.InvitedBy.IsDeleted && x.InvitedBy.ProcessingRestrictedAt == null |
| | 6 | 61 | | ? x.InvitedBy.Name |
| | 6 | 62 | | : null, |
| | 6 | 63 | | CreatedAt = x.CreatedAt, |
| | 6 | 64 | | ExpiresAt = x.ExpiresAt |
| | 6 | 65 | | }); |
| | | 66 | | |
| | 6 | 67 | | return await projected.PaginateAsync(query.PageNumber, query.PageSize, orderBy: null, cancellationToken: cancell |
| | 7 | 68 | | } |
| | | 69 | | } |