116 lines
4.4 KiB
C#
116 lines
4.4 KiB
C#
using EnvelopeGenerator.CommonServices;
|
|
using EnvelopeGenerator.Web.Services;
|
|
using Microsoft.AspNetCore.Authorization;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using System.Text.Encodings.Web;
|
|
using EnvelopeGenerator.Extensions;
|
|
using EnvelopeGenerator.Application.Interfaces.Services;
|
|
using static EnvelopeGenerator.Domain.Constants;
|
|
using EnvelopeGenerator.Domain.Entities;
|
|
using EnvelopeGenerator.Domain;
|
|
using DigitalData.Core.Abstraction.Application.DTO;
|
|
using EnvelopeGenerator.Web.Extensions;
|
|
using MediatR;
|
|
|
|
namespace EnvelopeGenerator.Web.Controllers;
|
|
|
|
[Authorize(Roles = ReceiverRole.FullyAuth)]
|
|
[ApiController]
|
|
[Route("api/[controller]")]
|
|
public class EnvelopeController : BaseController
|
|
{
|
|
private readonly EnvelopeOldService envelopeService;
|
|
private readonly ActionService? actionService;
|
|
private readonly UrlEncoder _urlEncoder;
|
|
[Obsolete("Use MediatR")]
|
|
private readonly IEnvelopeHistoryService _histService;
|
|
[Obsolete("Use MediatR")]
|
|
private readonly IEnvelopeReceiverService _envRcvService;
|
|
|
|
private readonly IMediator _mediator;
|
|
|
|
[Obsolete("Use MediatR")]
|
|
public EnvelopeController(DatabaseService database,
|
|
EnvelopeOldService envelope,
|
|
ILogger<EnvelopeController> logger, UrlEncoder urlEncoder,
|
|
IEnvelopeHistoryService envelopeHistoryService,
|
|
IEnvelopeReceiverService envelopeReceiverService, IMediator mediator) : base(database, logger)
|
|
{
|
|
envelopeService = envelope;
|
|
actionService = database?.Services?.actionService;
|
|
_urlEncoder = urlEncoder;
|
|
_histService = envelopeHistoryService;
|
|
_envRcvService = envelopeReceiverService;
|
|
_mediator = mediator;
|
|
}
|
|
|
|
[Authorize(Roles = ReceiverRole.FullyAuth)]
|
|
[HttpPost("{envelopeKey}")]
|
|
[Obsolete("Use MediatR")]
|
|
public async Task<IActionResult> Update(string envelopeKey, int index)
|
|
{
|
|
envelopeKey = _urlEncoder.Encode(envelopeKey);
|
|
|
|
var authSignature = User.GetAuthReceiverSignature();
|
|
|
|
if (authSignature != envelopeKey.GetReceiverSignature())
|
|
return Unauthorized();
|
|
|
|
EnvelopeReceiver response = await envelopeService.LoadEnvelope(envelopeKey);
|
|
|
|
// Again check if receiver has already signed
|
|
if (envelopeService.ReceiverAlreadySigned(response.Envelope, response.Receiver.Id) == true)
|
|
{
|
|
return Problem(statusCode: 403);
|
|
}
|
|
|
|
var document = envelopeService.GetDocument(index, envelopeKey);
|
|
|
|
string? annotationData = await envelopeService.EnsureValidAnnotationData(Request);
|
|
|
|
envelopeService.InsertDocumentStatus(new Domain.Entities.DocumentStatus()
|
|
{
|
|
EnvelopeId = response.Envelope.Id,
|
|
ReceiverId = response.Receiver.Id,
|
|
Value = annotationData,
|
|
Status = Constants.DocumentStatus.Signed
|
|
});
|
|
|
|
var signResult = actionService?.SignEnvelope(response.Envelope, ReceiverVM.From(response));
|
|
|
|
return Ok(new object());
|
|
}
|
|
|
|
[Authorize(Roles = ReceiverRole.FullyAuth)]
|
|
[HttpPost("reject")]
|
|
[Obsolete("Use DigitalData.Core.Exceptions and .Middleware")]
|
|
public async Task<IActionResult> Reject([FromBody] string? reason = null)
|
|
{
|
|
var signature = User.GetAuthReceiverSignature();
|
|
var uuid = User.GetAuthEnvelopeUuid();
|
|
var mail = User.GetAuthReceiverMail();
|
|
if (uuid is null || signature is null || mail is null)
|
|
{
|
|
_logger.LogEnvelopeError(uuid: uuid, signature: signature,
|
|
message: @$"Unauthorized POST request in api\envelope\reject. One of claims, Envelope, signature or mail ({mail}) is null.");
|
|
return Unauthorized();
|
|
}
|
|
|
|
var envRcvRes = await _envRcvService.ReadByUuidSignatureAsync(uuid: uuid, signature: signature);
|
|
|
|
if (envRcvRes.IsFailed)
|
|
{
|
|
_logger.LogNotice(envRcvRes.Notices);
|
|
return Unauthorized("you are not authirized");
|
|
}
|
|
|
|
return await _histService.RecordAsync(envRcvRes.Data.EnvelopeId, userReference: mail, EnvelopeStatus.DocumentRejected, comment: reason).ThenAsync(
|
|
Success: id => NoContent(),
|
|
Fail: IActionResult (mssg, ntc) =>
|
|
{
|
|
_logger.LogEnvelopeError(uuid: uuid, signature: signature, message: "Unexpected error happend in api/envelope/reject");
|
|
_logger.LogNotice(ntc);
|
|
return this.ViewInnerServiceError();
|
|
});
|
|
}
|
|
} |