Files
EnvelopeGenerator/EnvelopeGenerator.Server/EnvelopeGenerator.Server/Controllers/ReadOnlyController.cs
TekH 224d029c18 Refactor DI and update dependencies
- Enhanced `AddAutoMapper` configuration for flexibility.
- Replaced `IEnvelopeMailService` with `IEnvelopeNotificationService`.
- Migrated from `IUserRepository` to `EGDbContext` for data access.
- Updated RabbitMQ configuration to use `AddMessagingServiceHttpClient`.
- Removed `AddUserManager` and `AddDispatcher` registrations.
- Added conditional `FluentValidation` dependencies for target frameworks.
- Upgraded `MediatR` to 14.2.0 and `AutoMapper` to 16.2.0.
- Added `HtmlSanitizer` dependency.
- Improved error handling and logging in `ReadOnlyController`.
2026-11-17 20:09:23 +01:00

108 lines
4.3 KiB
C#

using DigitalData.Core.Abstraction.Application.DTO;
using EnvelopeGenerator.Application.Common.Dto.EnvelopeReceiverReadOnly;
using EnvelopeGenerator.Application.Common.Interfaces.Services;
using EnvelopeGenerator.Application.Common.Interfaces.Services.Notification;
using EnvelopeGenerator.Application.EnvelopeReceivers.Queries;
using EnvelopeGenerator.Domain.Constants;
using MediatR;
using System.Security.Claims;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
namespace EnvelopeGenerator.Server.Controllers;
/// <summary>
/// Manages read-only envelope sharing flows.
/// </summary>
[Route("api/[controller]")]
[ApiController]
public class ReadOnlyController : ControllerBase
{
private readonly ILogger<ReadOnlyController> _logger;
private readonly IEnvelopeReceiverReadOnlyService _readOnlyService;
private readonly IEnvelopeNotificationService _notificationService;
private readonly IEnvelopeHistoryService _historyService;
/// <summary>
/// Initializes a new instance of the <see cref="ReadOnlyController"/> class.
/// </summary>
public ReadOnlyController(ILogger<ReadOnlyController> logger, IEnvelopeReceiverReadOnlyService readOnlyService, IEnvelopeNotificationService notificationService, IEnvelopeHistoryService historyService)
{
_logger = logger;
_readOnlyService = readOnlyService;
_notificationService = notificationService;
_historyService = historyService;
}
/// <summary>
/// Creates a new read-only receiver for an envelope.
/// </summary>
/// <param name="envelopeKey">Envelope key (uuid+signature encoded).</param>
/// <param name="createDto">Creation payload.</param>
[HttpPost("{envelopeKey}")]
[Authorize(Policy = AuthPolicy.SenderOrReceiver)]
[Obsolete("Use MediatR")]
public async Task<IActionResult> CreateAsync(
[FromRoute] string envelopeKey,
[FromBody] EnvelopeReceiverReadOnlyCreateDto createDto,
[FromServices] IMediator mediator,
CancellationToken cancel)
{
var envelopeReceiver = await mediator.Send(new ReadEnvelopeReceiverQuery { Key = envelopeKey }, cancel);
var authEnvelopeReceiver = envelopeReceiver.FirstOrDefault();
if (authEnvelopeReceiver?.Envelope is null)
return NotFound();
var addedWho = User.FindFirstValue(ClaimTypes.Email)
?? authEnvelopeReceiver.Receiver?.EmailAddress;
if (string.IsNullOrWhiteSpace(addedWho))
{
_logger.LogError("Receiver e-mail is missing in envelope-receiver-read-only creation process. Create DTO is:\n {dto}", JsonConvert.SerializeObject(createDto));
return StatusCode(StatusCodes.Status500InternalServerError);
}
var envelopeId = authEnvelopeReceiver.EnvelopeId;
createDto.AddedWho = addedWho;
createDto.EnvelopeId = envelopeId;
var creationRes = await _readOnlyService.CreateAsync(createDto: createDto);
if (creationRes.IsFailed)
{
_logger.LogNotice(creationRes);
return StatusCode(StatusCodes.Status500InternalServerError);
}
var readRes = await _readOnlyService.ReadByIdAsync(creationRes.Data.Id);
if (readRes.IsFailed)
{
_logger.LogNotice(creationRes);
return StatusCode(StatusCodes.Status500InternalServerError);
}
var newReadOnly = readRes.Data;
var notification = new DocumentSharedNotification
{
DocumentTitle = newReadOnly.Envelope?.Title ?? string.Empty,
NameReceiver = newReadOnly.Receiver?.LastUsedName ?? string.Empty,
Origin = new Uri($"{Request.Scheme}://{Request.Host}"),
ReadOnlyId = newReadOnly.Id
};
await _notificationService.Notify(newReadOnly.ReceiverMail, notification, cancel);
var histRes = await _historyService.RecordAsync((int)createDto.EnvelopeId, createDto.AddedWho, EnvelopeStatus.EnvelopeShared);
if (histRes.IsFailed)
{
_logger.LogError("Although the envelope was sent as read-only, the EnvelopeShared history could not be saved. Create DTO:\n{createDto}", JsonConvert.SerializeObject(createDto));
_logger.LogNotice(histRes.Notices);
}
return Ok();
}
}