76 lines
2.5 KiB
C#
76 lines
2.5 KiB
C#
using DigitalData.Modules.Logging;
|
|
using EnvelopeGenerator.Common;
|
|
using EnvelopeGenerator.Web.Services;
|
|
using Microsoft.AspNetCore.Mvc;
|
|
using Microsoft.Extensions.Primitives;
|
|
using static EnvelopeGenerator.Common.Constants;
|
|
|
|
namespace EnvelopeGenerator.Web.Controllers
|
|
{
|
|
public class HistoryController : Controller
|
|
{
|
|
private readonly DatabaseService database;
|
|
private readonly LoggingService logging;
|
|
private readonly Logger logger;
|
|
private readonly ApiService api;
|
|
|
|
public HistoryController(DatabaseService database, LoggingService logging, ApiService api)
|
|
{
|
|
this.database = database;
|
|
this.logging = logging;
|
|
this.logger = logging.LogConfig.GetLoggerFor(GetType().Name);
|
|
this.api = api;
|
|
}
|
|
|
|
[HttpPost]
|
|
[Route("api/history/{envelopeKey}")]
|
|
[IgnoreAntiforgeryToken]
|
|
public IActionResult Create(string envelopeKey, [FromBody] ActionObject action)
|
|
{
|
|
try
|
|
{
|
|
var Request = ControllerContext.HttpContext.Request;
|
|
|
|
api.EnsureValidEnvelopeKey(envelopeKey);
|
|
EnvelopeResponse r = database.LoadEnvelope(envelopeKey);
|
|
var receiver = r.Envelope.Receivers.Where(receiver => receiver.Id == r.ReceiverId).SingleOrDefault();
|
|
|
|
if (receiver == null)
|
|
{
|
|
return BadRequest();
|
|
}
|
|
|
|
string actionTypeString = action.actionType;
|
|
string actionDescription = action.actionDescription;
|
|
|
|
if (!Enum.TryParse<EnvelopeHistoryActionType>(actionTypeString, out var actionType))
|
|
{
|
|
return BadRequest();
|
|
};
|
|
|
|
database.InsertHistoryEntry(new EnvelopeHistoryEntry()
|
|
{
|
|
ActionDescription = actionDescription,
|
|
ActionDate = DateTime.Now,
|
|
ActionType = actionType,
|
|
EnvelopeId = r.Envelope.Id,
|
|
UserReference = receiver.Email
|
|
});
|
|
|
|
return Ok();
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error(e);
|
|
return Problem(statusCode: 500);
|
|
}
|
|
}
|
|
|
|
public class ActionObject
|
|
{
|
|
public string actionType { get; set; }
|
|
public string actionDescription { get; set; }
|
|
}
|
|
}
|
|
}
|