Refactor access code request logic with MediatR

Encapsulated the access code request logic in a new `SendAccessCodeCommand` using MediatR. Introduced a `SendAccessCodeResult` enum to standardize outcomes. Refactored `AuthController` to delegate access code handling to the command, simplifying the controller and reducing dependencies.

Updated `AuthService` to map HTTP responses to `SendAccessCodeResult`. Improved error handling and edge case management in `SendAccessCodeCommandHandler`. Centralized history recording and notification logic in the command handler for better maintainability and separation of concerns.
This commit is contained in:
2026-11-16 11:18:14 +01:00
parent deae4d2acc
commit fd2b21a73c
3 changed files with 123 additions and 48 deletions

View File

@@ -1,5 +1,6 @@
using System.Net;
using System.Net.Http.Json;
using EnvelopeGenerator.Application.EnvelopeReceivers.Commands;
namespace EnvelopeGenerator.Server.Client.Services;
@@ -37,14 +38,22 @@ public class AuthService(IHttpClientFactory httpClientFactory)
/// Triggers access code e-mail request flow for the receiver login page.
/// Calls POST /api/Auth/envelope-receiver/{key}/request-access-code.
/// </summary>
public async Task<bool> RequestEnvelopeReceiverAccessCodeAsync(string envelopeKey, CancellationToken cancel = default)
public async Task<SendAccessCodeResult?> RequestEnvelopeReceiverAccessCodeAsync(string envelopeKey, CancellationToken cancel = default)
{
using var http = CreateDefaultClient();
var response = await http.PostAsync(
$"/api/Auth/envelope-receiver/{Uri.EscapeDataString(envelopeKey)}/request-access-code",
null,
cancel);
return response.IsSuccessStatusCode;
return response.StatusCode switch
{
HttpStatusCode.OK => SendAccessCodeResult.Sent,
HttpStatusCode.NoContent => SendAccessCodeResult.NoAccessCodeRequired,
HttpStatusCode.Conflict => SendAccessCodeResult.AlreadySigned,
HttpStatusCode.TooManyRequests => SendAccessCodeResult.RequestLimitExceeded,
_ => null
};
}
/// <summary>