From 8d5e24c6a72bba14d331af396f10582f6b022581 Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Wed, 11 Oct 2023 11:10:38 +0200 Subject: [PATCH] Add documentstatus --- EnvelopeGenerator.Common/Constants.vb | 5 ++ .../Entities/DocumentStatus.vb | 10 +++ .../EnvelopeGenerator.Common.vbproj | 2 + .../Models/DocumentStatusModel.vb | 89 +++++++++++++++++++ EnvelopeGenerator.Web/Handler/FileHandler.cs | 45 +++++++--- EnvelopeGenerator.Web/Program.cs | 11 ++- 6 files changed, 144 insertions(+), 18 deletions(-) create mode 100644 EnvelopeGenerator.Common/Entities/DocumentStatus.vb create mode 100644 EnvelopeGenerator.Common/Models/DocumentStatusModel.vb diff --git a/EnvelopeGenerator.Common/Constants.vb b/EnvelopeGenerator.Common/Constants.vb index b75839f6..b12efdde 100644 --- a/EnvelopeGenerator.Common/Constants.vb +++ b/EnvelopeGenerator.Common/Constants.vb @@ -14,6 +14,11 @@ Created = 0 End Enum + Public Enum DocumentStatus + Created = 0 + Signed = 1 + End Enum + Public Enum EnvelopeHistoryActionType Created = 0 Saved = 1 diff --git a/EnvelopeGenerator.Common/Entities/DocumentStatus.vb b/EnvelopeGenerator.Common/Entities/DocumentStatus.vb new file mode 100644 index 00000000..8ed602b9 --- /dev/null +++ b/EnvelopeGenerator.Common/Entities/DocumentStatus.vb @@ -0,0 +1,10 @@ +Public Class DocumentStatus + + Public Property Id As Integer + Public Property EnvelopeId As Integer + Public Property ReceiverId As Integer + Public Property Value As String + Public Property Status As Constants.DocumentStatus = Constants.DocumentStatus.Created + Public Property StatusChangedWhen As Date + +End Class diff --git a/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj b/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj index 8524dff2..511b5ba1 100644 --- a/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj +++ b/EnvelopeGenerator.Common/EnvelopeGenerator.Common.vbproj @@ -95,6 +95,7 @@ + @@ -112,6 +113,7 @@ + diff --git a/EnvelopeGenerator.Common/Models/DocumentStatusModel.vb b/EnvelopeGenerator.Common/Models/DocumentStatusModel.vb new file mode 100644 index 00000000..d3b673f5 --- /dev/null +++ b/EnvelopeGenerator.Common/Models/DocumentStatusModel.vb @@ -0,0 +1,89 @@ +Imports System.Data.SqlClient + +Public Class DocumentStatusModel + Inherits BaseModel + + Public Sub New(pState As State) + MyBase.New(pState) + End Sub + + Public Function InsertOrUpdate(pDocumentStatus As DocumentStatus) As Boolean + If pDocumentStatus.Id = 0 Then + Return Insert(pDocumentStatus) + Else + Return Update(pDocumentStatus) + End If + End Function + + Public Function Insert(pDocumentStatus As DocumentStatus) As Boolean + Try + Dim oSql = "INSERT INTO [dbo].[TBSIG_DOCUMENT_STATUS] + ([ENVELOPE_ID] + ,[RECEIVER_ID] + ,[STATUS] + ,[STATUS_CHANGED_WHEN] + ,[VALUE]) + VALUES + (@ENVELOPE_ID + ,@RECEIVER_ID + ,@STATUS + ,@STATUS_CHANGED_WHEN + ,@VALUE)" + + Dim oCommand As New SqlCommand(oSql) + oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.Int).Value = pDocumentStatus.EnvelopeId + oCommand.Parameters.Add("RECEIVER_ID", SqlDbType.Int).Value = pDocumentStatus.ReceiverId + oCommand.Parameters.Add("STATUS", SqlDbType.NVarChar).Value = pDocumentStatus.Status.ToString() + oCommand.Parameters.Add("STATUS_CHANGED_WHEN", SqlDbType.DateTime).Value = Now() + oCommand.Parameters.Add("VALUE", SqlDbType.NVarChar).Value = pDocumentStatus.Value + + If Database.ExecuteNonQuery(oCommand) Then + pDocumentStatus.Id = GetElementId(pDocumentStatus) + Return True + Else + Return False + End If + Catch ex As Exception + Logger.Error(ex) + Return False + End Try + End Function + + Public Function Update(pDocumentStatus As DocumentStatus) As Boolean + Try + Dim oSql = "UPDATE [dbo].[TBSIG_DOCUMENT_STATUS] + SET [STATUS] = @STATUS + ,[STATUS_CHANGED_WHEN] = @STATUS_CHANGED_WHEN + ,[CHANGED_WHEN] = @CHANGED_WHEN + ,[VALUE] = @VALUE + WHERE GUID = @GUID" + + Dim oCommand As New SqlCommand(oSql) + oCommand.Parameters.Add("GUID", SqlDbType.Int).Value = pDocumentStatus.Id + oCommand.Parameters.Add("STATUS", SqlDbType.NVarChar).Value = pDocumentStatus.Status.ToString() + oCommand.Parameters.Add("STATUS_CHANGED_WHEN", SqlDbType.DateTime).Value = Now() + oCommand.Parameters.Add("CHANGED_WHEN", SqlDbType.DateTime).Value = Now() + oCommand.Parameters.Add("VALUE", SqlDbType.NVarChar).Value = pDocumentStatus.Value + + If Database.ExecuteNonQuery(oCommand) Then + Return True + Else + Return False + End If + Catch ex As Exception + Logger.Error(ex) + Return False + End Try + End Function + + Private Function GetElementId(pDocument As DocumentStatus) As Integer + Try + Return Database.GetScalarValue($"SELECT MAX(GUID) FROM TBSIG_DOCUMENT_STATUS + WHERE ENVELOPE_ID = {pDocument.EnvelopeId} AND RECEIVER_ID = {pDocument.ReceiverId}") + + Catch ex As Exception + Logger.Error(ex) + Return Nothing + End Try + End Function +End Class diff --git a/EnvelopeGenerator.Web/Handler/FileHandler.cs b/EnvelopeGenerator.Web/Handler/FileHandler.cs index 6df57df8..96743adf 100644 --- a/EnvelopeGenerator.Web/Handler/FileHandler.cs +++ b/EnvelopeGenerator.Web/Handler/FileHandler.cs @@ -1,4 +1,5 @@ -using DigitalData.Modules.Logging; +using DigitalData.Modules.Database; +using DigitalData.Modules.Logging; using EnvelopeGenerator.Common; using EnvelopeGenerator.Common.My.Resources; using EnvelopeGenerator.Web.Services; @@ -9,7 +10,7 @@ using System.Text; namespace EnvelopeGenerator.Web.Handler { - public class FileHandler + public class FileHandler { /// /// URL: GET /api/envelope/{envelopeKey} @@ -17,7 +18,7 @@ namespace EnvelopeGenerator.Web.Handler /// Returns a /// /// - public static IResult HandleGetEnvelope(HttpContext ctx, DatabaseService database, LoggingService logging) + public IResult HandleGetEnvelope(HttpContext ctx, DatabaseService database, LoggingService logging) { var logger = logging.LogConfig.GetLogger("FileHandler"); @@ -48,7 +49,7 @@ namespace EnvelopeGenerator.Web.Handler /// Returns a document for the supplied EnvelopeKey and Document Id / Index /// /// file buffer of the requested document - public async static Task HandleGetDocument(HttpContext ctx, DatabaseService database, LoggingService logging) + public async Task HandleGetDocument(HttpContext ctx, DatabaseService database, LoggingService logging) { var logger = logging.LogConfig.GetLogger("FileHandler"); @@ -80,7 +81,7 @@ namespace EnvelopeGenerator.Web.Handler } } - public async static Task HandlePostDocument(HttpContext ctx, DatabaseService database, LoggingService logging) + public async Task HandlePostDocument(HttpContext ctx, DatabaseService database, LoggingService logging) { var logger = logging.LogConfig.GetLogger("FileHandler"); @@ -113,7 +114,7 @@ namespace EnvelopeGenerator.Web.Handler - public async static Task HandlePostEnvelope(HttpContext ctx, DatabaseService database, LoggingService logging) + public async Task HandlePostEnvelope(HttpContext ctx, DatabaseService database, LoggingService logging) { var logger = logging.LogConfig.GetLogger("FileHandler"); @@ -129,14 +130,23 @@ namespace EnvelopeGenerator.Web.Handler int documentId = EnsureValidDocumentIndex(logger, ctx.Request); var document = GetDocument(r.Envelope, documentId); - var annotationData = EnsureValidAnnotationData(logger, ctx.Request); + string? annotationData = await EnsureValidAnnotationData(logger, ctx.Request); if (annotationData == null) { throw new ArgumentNullException("AnnotationData"); } - // TODO: Save annotations to database + State state = GetState(logging.LogConfig, database.MSSQL); + DocumentStatusModel model = new(state); + + model.InsertOrUpdate(new DocumentStatus() + { + EnvelopeId = r.Envelope.Id, + ReceiverId = r.ReceiverId, + Value = annotationData, + Status = Common.Constants.DocumentStatus.Signed + }); return Results.Ok(); } @@ -145,12 +155,19 @@ namespace EnvelopeGenerator.Web.Handler // Better error handling & reporting logger.Error(e); return Results.Problem(); - } + } + } - + private State GetState(LogConfig LogConfig, MSSQLServer Database) + { + return new State + { + LogConfig = LogConfig, + Database = Database, + }; } - private static int EnsureValidDocumentIndex(Logger logger, HttpRequest request) + private int EnsureValidDocumentIndex(Logger logger, HttpRequest request) { if (request.Query.TryGetValue("index", out StringValues documentIndexString)) { @@ -169,7 +186,7 @@ namespace EnvelopeGenerator.Web.Handler } } - private static string EnsureValidEnvelopeKey(Logger logger, HttpRequest request) + private string EnsureValidEnvelopeKey(Logger logger, HttpRequest request) { logger.Debug("Parsing EnvelopeKey.."); var envelopeKey = request.RouteValues["envelopeKey"] as string; @@ -190,7 +207,7 @@ namespace EnvelopeGenerator.Web.Handler return envelopeKey; } - private static async Task EnsureValidAnnotationData(Logger logger, HttpRequest request) + private async Task EnsureValidAnnotationData(Logger logger, HttpRequest request) { logger.Debug("Parsing AnnotationData.."); @@ -210,7 +227,7 @@ namespace EnvelopeGenerator.Web.Handler } - private static EnvelopeDocument GetDocument(Common.Envelope envelope, int documentId) + private EnvelopeDocument GetDocument(Common.Envelope envelope, int documentId) { var document = envelope.Documents. Where(d => d.Id == documentId). diff --git a/EnvelopeGenerator.Web/Program.cs b/EnvelopeGenerator.Web/Program.cs index 3281d6e6..abd647d7 100644 --- a/EnvelopeGenerator.Web/Program.cs +++ b/EnvelopeGenerator.Web/Program.cs @@ -1,5 +1,6 @@ using EnvelopeGenerator.Web.Handler; using EnvelopeGenerator.Web.Services; +using Microsoft.AspNetCore.Builder; using Microsoft.AspNetCore.Components; using Microsoft.AspNetCore.Components.Web; @@ -35,10 +36,12 @@ app.UseStaticFiles(); app.UseRouting(); // Add file download endpoint -app.MapGet("/api/document/{envelopeKey}", FileHandler.HandleGetDocument); -app.MapPost("/api/document/{envelopeKey}", FileHandler.HandlePostDocument); -app.MapGet("/api/envelope/{envelopeKey}", FileHandler.HandleGetEnvelope); -app.MapPost("/api/envelope/{envelopeKey}", FileHandler.HandlePostEnvelope); +FileHandler handler = new(); + +app.MapGet("/api/document/{envelopeKey}", handler.HandleGetDocument); +app.MapPost("/api/document/{envelopeKey}", handler.HandlePostDocument); +app.MapGet("/api/envelope/{envelopeKey}", handler.HandleGetEnvelope); +app.MapPost("/api/envelope/{envelopeKey}", handler.HandlePostEnvelope); // Blazor plumbing app.MapBlazorHub();