| | | 1 | | using ArturRios.Data.Relational.Core.Interfaces; |
| | | 2 | | using ArturRios.Heimdall.Command.Input; |
| | | 3 | | using ArturRios.Heimdall.Command.Output; |
| | | 4 | | using ArturRios.Heimdall.Command.Services; |
| | | 5 | | using ArturRios.Heimdall.Domain.Entities; |
| | | 6 | | using ArturRios.Heimdall.Domain.Enums; |
| | | 7 | | using ArturRios.Heimdall.Domain.Persistence; |
| | | 8 | | using ArturRios.Heimdall.Shared.Messages; |
| | | 9 | | using ArturRios.Mediator.Command.Interfaces; |
| | | 10 | | using ArturRios.Output; |
| | | 11 | | using Microsoft.EntityFrameworkCore; |
| | | 12 | | |
| | | 13 | | namespace ArturRios.Heimdall.Command.Handlers; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Handles <see cref="DeclineScopeOwnerInvitationCommand" /> (UC-61, FR-SC-17): the invitee ends an open |
| | | 17 | | /// invitation as declined, and the inviter is told. Any other caller, and an ended or expired invitation, |
| | | 18 | | /// get AF-61a's single answer, as for UC-60. All failures are returned as errors on the |
| | | 19 | | /// <see cref="DataOutput{T}" /> rather than thrown. |
| | | 20 | | /// </summary> |
| | 8 | 21 | | public class DeclineScopeOwnerInvitationCommandHandler( |
| | 8 | 22 | | IAsyncReadOnlyRepository<Person, long> personReader, |
| | 8 | 23 | | IAsyncReadOnlyRepository<Scope, long> scopeReader, |
| | 8 | 24 | | IAsyncReadOnlyRepository<ScopeOwnerInvitation, long> invitationReader, |
| | 8 | 25 | | IScopeOwnerInvitations invitations, |
| | 8 | 26 | | IScopeInvitationNoticeSender notices) |
| | | 27 | | : ICommandHandlerAsync<DeclineScopeOwnerInvitationCommand, DeclineScopeOwnerInvitationCommandOutput> |
| | | 28 | | { |
| | | 29 | | public async Task<DataOutput<DeclineScopeOwnerInvitationCommandOutput?>> HandleAsync( |
| | | 30 | | DeclineScopeOwnerInvitationCommand command, CancellationToken cancellationToken = default) |
| | | 31 | | { |
| | 8 | 32 | | var output = DataOutput<DeclineScopeOwnerInvitationCommandOutput?>.New; |
| | 8 | 33 | | var now = DateTime.UtcNow; |
| | | 34 | | |
| | | 35 | | // AF-61a. |
| | 8 | 36 | | var found = await InviteeInvitationLookup.FindAsync( |
| | 8 | 37 | | personReader, invitationReader, command.ActingPersonId, command.InvitationId, now, cancellationToken); |
| | | 38 | | |
| | 8 | 39 | | if (found is null) |
| | | 40 | | { |
| | 2 | 41 | | return output.WithError(ScopeInvitationMessages.InvitationNotOpen); |
| | | 42 | | } |
| | | 43 | | |
| | 6 | 44 | | var (invitee, invitation) = found.Value; |
| | | 45 | | |
| | 6 | 46 | | if (!await invitations.TryEndAsync(invitation, ScopeOwnerInvitationOutcomes.Declined, now)) |
| | | 47 | | { |
| | 1 | 48 | | return output.WithError(ScopeInvitationMessages.InvitationNotOpen); |
| | | 49 | | } |
| | | 50 | | |
| | 5 | 51 | | var scope = await scopeReader.Query() |
| | 5 | 52 | | .FirstOrDefaultAsync(x => x.Id == invitation.ScopeId, cancellationToken); |
| | | 53 | | |
| | 5 | 54 | | if (scope is not null && |
| | 5 | 55 | | await InviteeInvitationLookup.InviterToTellAsync(personReader, invitation, cancellationToken) is { } inviter |
| | | 56 | | { |
| | 2 | 57 | | await notices.SendAnswerAsync(inviter.Email, invitee.Name, scope.Name, accepted: false); |
| | | 58 | | } |
| | | 59 | | |
| | 5 | 60 | | return output |
| | 5 | 61 | | .WithData(new DeclineScopeOwnerInvitationCommandOutput { Id = invitation.PublicId }) |
| | 5 | 62 | | .WithMessage(ScopeInvitationMessages.InvitationDeclined); |
| | 8 | 63 | | } |
| | | 64 | | } |