189 lines
6.6 KiB
C#
189 lines
6.6 KiB
C#
using EnvelopeGenerator.Common;
|
|
using Microsoft.Extensions.Primitives;
|
|
using System.Reflection.Metadata;
|
|
using System.Text;
|
|
using static EnvelopeGenerator.Common.Constants;
|
|
|
|
namespace EnvelopeGenerator.Web.Services
|
|
{
|
|
public class EnvelopeService : BaseService
|
|
{
|
|
private readonly ReceiverModel receiverModel;
|
|
private readonly EnvelopeModel envelopeModel;
|
|
private readonly DocumentStatusModel documentStatusModel;
|
|
|
|
public EnvelopeService(IConfiguration Config, LoggingService Logging, DatabaseService database) : base(Config, Logging)
|
|
{
|
|
logger = Logging.LogConfig.GetLogger();
|
|
|
|
if (database.Models == null)
|
|
{
|
|
throw new ArgumentNullException("Models not loaded.");
|
|
}
|
|
|
|
receiverModel = database.Models.receiverModel;
|
|
envelopeModel = database.Models.envelopeModel;
|
|
documentStatusModel = database.Models.documentStatusModel;
|
|
}
|
|
|
|
public void EnsureValidEnvelopeKey(string envelopeKey)
|
|
{
|
|
logger.Debug("Parsing EnvelopeKey..");
|
|
|
|
if (string.IsNullOrEmpty(envelopeKey))
|
|
throw new ArgumentNullException("EnvelopeKey");
|
|
|
|
Tuple<string, string> result = Helpers.DecodeEnvelopeReceiverId(envelopeKey);
|
|
logger.Debug("EnvelopeUUID: [{0}]", result.Item1);
|
|
logger.Debug("ReceiverSignature: [{0}]", result.Item2);
|
|
|
|
if (string.IsNullOrEmpty(result.Item1))
|
|
throw new ArgumentNullException("EnvelopeUUID");
|
|
|
|
if (string.IsNullOrEmpty(result.Item2))
|
|
throw new ArgumentNullException("ReceiverSignature");
|
|
}
|
|
|
|
public EnvelopeResponse LoadEnvelope(string pEnvelopeKey)
|
|
{
|
|
logger.Debug("Loading Envelope by Key [{0}]", pEnvelopeKey);
|
|
|
|
Tuple<string, string> result = Helpers.DecodeEnvelopeReceiverId(pEnvelopeKey);
|
|
var envelopeUuid = result.Item1;
|
|
var receiverSignature = result.Item2;
|
|
var receiverId = receiverModel.GetReceiverIdBySignature(receiverSignature);
|
|
|
|
Envelope? envelope = envelopeModel.GetByUuid(envelopeUuid);
|
|
|
|
if (envelope == null)
|
|
{
|
|
logger.Warn("Envelope not found");
|
|
throw new NullReferenceException("Envelope not found");
|
|
}
|
|
|
|
logger.Debug("Envelope loaded");
|
|
|
|
if (envelope.Receivers == null)
|
|
{
|
|
logger.Warn("Receivers for envelope not loaded");
|
|
throw new NullReferenceException("Receivers for envelope not loaded");
|
|
}
|
|
|
|
logger.Debug("Envelope receivers found: [{0}]", envelope.Receivers.Count);
|
|
|
|
EnvelopeReceiver? receiver = envelope.Receivers.Where(r => r.Id == receiverId).SingleOrDefault();
|
|
|
|
if (receiver == null)
|
|
{
|
|
logger.Warn("Receiver not found");
|
|
throw new NullReferenceException("Receiver not found");
|
|
}
|
|
|
|
logger.Debug("Loading documents for receiver [{0}]", receiver.Email);
|
|
|
|
envelope.Documents = FilterElementsByReceiver(envelope, receiverId);
|
|
|
|
return new()
|
|
{
|
|
Receiver = receiver,
|
|
Envelope = envelope
|
|
};
|
|
}
|
|
|
|
private static List<EnvelopeDocument> FilterElementsByReceiver(Envelope envelope, int receiverId)
|
|
{
|
|
return envelope.Documents.
|
|
Select((document) =>
|
|
{
|
|
var elements = document.Elements.Where((e) => e.ReceiverId == receiverId);
|
|
document.Elements = elements.ToList();
|
|
return document;
|
|
}).
|
|
ToList();
|
|
}
|
|
|
|
public List<Envelope> LoadEnvelopes(int pReceiverId)
|
|
{
|
|
return (List<Envelope>)envelopeModel.List(pReceiverId);
|
|
}
|
|
|
|
public async Task<string?> EnsureValidAnnotationData(HttpRequest request)
|
|
{
|
|
try
|
|
{
|
|
logger.Debug("Parsing annotation data from request..");
|
|
|
|
using MemoryStream ms = new();
|
|
await request.BodyReader.CopyToAsync(ms);
|
|
var bytes = ms.ToArray();
|
|
|
|
logger.Debug("Annotation data parsed, size: [{0}]", bytes.Length);
|
|
|
|
return Encoding.UTF8.GetString(bytes);
|
|
}
|
|
catch (Exception e)
|
|
{
|
|
logger.Error(e);
|
|
throw new ArgumentNullException("AnnotationData");
|
|
}
|
|
}
|
|
|
|
public int EnsureValidDocumentIndex(HttpRequest request)
|
|
{
|
|
if (!request.Query.TryGetValue("index", out StringValues documentIndexStringList))
|
|
{
|
|
logger.Warn("There is no query parameter called index");
|
|
throw new ArgumentNullException("DocumentIndex");
|
|
}
|
|
|
|
if (documentIndexStringList.FirstOrDefault() == null)
|
|
{
|
|
logger.Warn("There is no query parameter called index");
|
|
throw new ArgumentNullException("DocumentIndex");
|
|
}
|
|
|
|
if (!int.TryParse(documentIndexStringList.First(), out int documentIndex))
|
|
{
|
|
logger.Warn("Invalid document index [{0}]", documentIndexStringList.First());
|
|
throw new ArgumentNullException("DocumentIndex");
|
|
}
|
|
|
|
return documentIndex;
|
|
}
|
|
|
|
public EnvelopeDocument GetDocument(HttpRequest request, string envelopeKey)
|
|
{
|
|
EnvelopeResponse response = LoadEnvelope(envelopeKey);
|
|
int documentId = EnsureValidDocumentIndex(request);
|
|
|
|
logger.Debug("Loading document for Id [{0}]", documentId);
|
|
|
|
var document = response.Envelope.Documents.
|
|
Where(d => d.Id == documentId).
|
|
FirstOrDefault();
|
|
|
|
if (document == null)
|
|
throw new ArgumentException("DocumentId");
|
|
|
|
logger.Debug("Document [{0}] loaded!", documentId);
|
|
|
|
return document;
|
|
}
|
|
|
|
public bool InsertDocumentStatus(Common.DocumentStatus documentStatus)
|
|
{
|
|
logger.Debug("Saving annotation data..");
|
|
return documentStatusModel.InsertOrUpdate(documentStatus);
|
|
}
|
|
|
|
public async Task<byte[]> GetDocumentContents(EnvelopeDocument document)
|
|
{
|
|
logger.Debug("Loading file [{0}]", document.Filepath);
|
|
var bytes = await File.ReadAllBytesAsync(document.Filepath);
|
|
logger.Info("File loaded, size: [{0}]", bytes.Length);
|
|
|
|
return bytes;
|
|
}
|
|
}
|
|
}
|