clean up server
This commit is contained in:
192
EnvelopeGenerator.Web/Services/EnvelopeService.cs
Normal file
192
EnvelopeGenerator.Web/Services/EnvelopeService.cs
Normal file
@@ -0,0 +1,192 @@
|
||||
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 ReceiverModel receiverModel;
|
||||
private EnvelopeModel envelopeModel;
|
||||
private HistoryModel historyModel;
|
||||
private DocumentModel documentModel;
|
||||
private 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;
|
||||
historyModel = database.Models.historyModel;
|
||||
documentModel = database.Models.documentModel;
|
||||
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)
|
||||
{
|
||||
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)
|
||||
{
|
||||
throw new NullReferenceException("Envelope not found");
|
||||
}
|
||||
|
||||
if (envelope.Receivers == null)
|
||||
{
|
||||
throw new NullReferenceException("Receivers for envelope not loaded");
|
||||
}
|
||||
|
||||
EnvelopeReceiver? receiver = envelope.Receivers.Where(r => r.Id == receiverId).SingleOrDefault();
|
||||
|
||||
if (receiver == null)
|
||||
{
|
||||
throw new NullReferenceException("Receiver not found");
|
||||
}
|
||||
|
||||
return new()
|
||||
{
|
||||
Receiver = receiver,
|
||||
Envelope = envelope
|
||||
};
|
||||
}
|
||||
|
||||
public List<Envelope> LoadEnvelopes(int pReceiverId)
|
||||
{
|
||||
return (List<Envelope>)envelopeModel.List(pReceiverId);
|
||||
}
|
||||
|
||||
public bool InsertHistoryEntry(EnvelopeHistoryEntry historyEntry)
|
||||
{
|
||||
return historyModel.Insert(historyEntry);
|
||||
}
|
||||
|
||||
public bool InsertHistoryEntrySigned(EnvelopeResponse response)
|
||||
{
|
||||
return historyModel.Insert(new EnvelopeHistoryEntry()
|
||||
{
|
||||
ActionDescription = "Dokument wurde signiert",
|
||||
ActionDate = DateTime.Now,
|
||||
ActionType = EnvelopeHistoryActionType.Signed,
|
||||
EnvelopeId = response.Envelope.Id,
|
||||
UserReference = response.Receiver.Email
|
||||
});
|
||||
}
|
||||
|
||||
public bool InsertDocumentStatus(Common.DocumentStatus documentStatus)
|
||||
{
|
||||
return documentStatusModel.InsertOrUpdate(documentStatus);
|
||||
}
|
||||
|
||||
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();
|
||||
|
||||
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 async Task UpdateDocument(Stream fileStream, string filePath)
|
||||
{
|
||||
logger.Debug("Writing document to path [{0}]..", filePath);
|
||||
|
||||
using FileStream fs = new(filePath, FileMode.Open);
|
||||
await fileStream.CopyToAsync(fs);
|
||||
|
||||
logger.Debug("Document written!");
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user