Common und Service API Send API Mails

This commit is contained in:
Developer01 2025-05-07 16:50:46 +02:00
parent 4f95a1eed4
commit 6fc0c32c46
7 changed files with 260 additions and 11 deletions

View File

@ -281,6 +281,7 @@
<Compile Include="Entities\ElementStatus.vb" />
<Compile Include="Entities\EmailData.vb" />
<Compile Include="Entities\EmailTemplate.vb" />
<Compile Include="Jobs\APIBackendJobs\SendInvitationMailJob.vb" />
<Compile Include="Jobs\FinalizeDocument\PDFBurnerParams.vb" />
<Compile Include="Services\TemplateService.vb" />
<Compile Include="Entities\Envelope.vb" />

View File

@ -0,0 +1,170 @@
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports DigitalData.Modules.Base
Imports GdPicture14
Imports Quartz
Imports System.Security.Cryptography
Imports System.IO
Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument.FinalizeDocumentExceptions
Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument
Imports EnvelopeGenerator.Common.Constants
Imports DevExpress.DataProcessing
Imports System.Data.SqlClient
Imports DevExpress.XtraRichEdit.Layout.Engine
Namespace Jobs
Public Class SendInvitationMailJob
Implements IJob
Private LogConfig As LogConfig
Private Logger As Logger
Private Database As MSSQLServer
Private Config As DbConfig
Private ConfigModel As ConfigModel
Private EnvelopeModel As EnvelopeModel
Private ReceiverModel As ReceiverModel
Private ActionService As ActionService
Private ReadOnly CompleteWaitTime As Integer = 1
Private myTempFiles As TempFiles
Private Class EnvelopeData
Public EnvelopeId As Integer
Public EnvelopeUUID As String
Public DocumentPath As String
End Class
Public Function Execute(pContext As IJobExecutionContext) As Task Implements IJob.Execute
LogConfig = pContext.MergedJobDataMap.Item(Constants.LOGCONFIG)
Logger = LogConfig.GetLogger()
myTempFiles = New TempFiles(LogConfig)
myTempFiles.Create()
Dim JobId = pContext.JobDetail.Key
Logger.Info("SendInvMail - Starting job {0}", JobId)
Try
Logger.Debug("SendInvMail - Loading Database..")
Database = GetDatabase(pContext, LogConfig)
Logger.Debug("SendInvMail - Loading Models & Services")
Dim oState = GetState()
InitializeModels(oState)
Logger.Debug("SendInvMail - Loading Configuration..")
Config = ConfigModel.LoadConfiguration()
oState.DbConfig = Config
InitializeServices(oState)
Config.DocumentPath = Config.DocumentPath
Logger.Debug("SendInvMail - ExportPath: [{0}]", Config.ExportPath)
Dim oCompleteStatus As Integer = Constants.EnvelopeStatus.EnvelopeCompletelySigned
Dim oSql = $"SELECT * FROM TBSIG_ENVELOPE where SOURCE = 'API' AND STATUS = 1003 order by guid"
Dim oTable = Database.GetDatatable(oSql)
Dim oEnvelopeIds As List(Of Integer) = oTable.Rows.Cast(Of DataRow).
Select(Function(r) r.Item("GUID")).
Cast(Of Integer).
ToList()
If oEnvelopeIds.Count > 0 Then
Logger.Info("SendInvMail - Found [{0}] envelopes.", oEnvelopeIds.Count)
End If
Dim oTotal As Integer = oEnvelopeIds.Count
Dim oCurrent As Integer = 1
For Each oId In oEnvelopeIds
Logger.Info("SendInvMail - Gathering Info for Envelope [{0}] ({1}/{2})", oId, oCurrent, oTotal)
Logger.Debug("SendInvMail - Loading Envelope..")
Try
Dim oEnvelope = EnvelopeModel.GetById(oId)
If oEnvelope Is Nothing Then
Logger.Warn("SendInvMail - Envelope could not be loaded for Id [{0}]!", oId)
Throw New ArgumentNullException("EnvelopeData")
End If
Logger.Debug("SendInvMail - Loading Envelope Data..")
Dim oEnvelopeData = GetEnvelopeData(oId)
oEnvelope.Receivers = ReceiverModel.ListEnvelopeReceivers(oEnvelope.Id).ToList()
Logger.Debug("SendInvMail - Created Reveivers!")
If oEnvelopeData Is Nothing Then
Logger.Warn("SendInvMail - EnvelopeData could not be loaded for Id [{0}]!", oId)
Throw New ArgumentNullException("EnvelopeData")
End If
Logger.Info("SendInvMail - Sending InvitationMails for Envelope [{0}]", oId)
If ActionService.SendEnvelope(oEnvelope) = False Then
Throw New ArgumentNullException("EnvelopeData")
End If
Catch ex As Exception
Logger.Warn(ex, $"Unhandled exception while working envelope [{oId}]")
End Try
oCurrent += 1
Logger.Info("SendInvMail - Envelope finalized!")
Next
Logger.Debug("SendInvMail - Completed job {0} successfully!", JobId)
Catch ex As Exception
Logger.Warn("SendInvMail job failed!")
Logger.Error(ex)
Finally
Logger.Info("SendInvMail execution for [{0}] ended", JobId)
End Try
Return Task.FromResult(True)
End Function
Private Sub InitializeModels(pState As State)
ConfigModel = New ConfigModel(pState)
EnvelopeModel = New EnvelopeModel(pState)
ReceiverModel = New ReceiverModel(pState)
End Sub
Private Sub InitializeServices(pState As State)
ActionService = New ActionService(pState)
End Sub
Private Function GetDatabase(pContext As IJobExecutionContext, pLogConfig As LogConfig) As MSSQLServer
Dim oConnectionString As String = pContext.MergedJobDataMap.Item(Constants.DATABASE)
Dim Database = New MSSQLServer(pLogConfig, MSSQLServer.DecryptConnectionString(oConnectionString))
Return Database
End Function
Private Function GetEnvelopeData(pEnvelopeId As Integer) As EnvelopeData
Dim oSql = $"SELECT T.GUID, T.ENVELOPE_UUID,T2.FILEPATH, T2.BYTE_DATA FROM [dbo].[TBSIG_ENVELOPE] T
JOIN TBSIG_ENVELOPE_DOCUMENT T2 ON T.GUID = T2.ENVELOPE_ID
WHERE T.GUID = {pEnvelopeId}"
Dim oTable As DataTable = Database.GetDatatable(oSql)
Dim oRow As DataRow = oTable.Rows.Cast(Of DataRow).SingleOrDefault()
If oRow Is Nothing Then
Return Nothing
End If
Dim oData As New EnvelopeData With {
.EnvelopeId = pEnvelopeId,
.DocumentPath = oRow.ItemEx("FILEPATH", ""),
.EnvelopeUUID = oRow.ItemEx("ENVELOPE_UUID", "")
}
Logger.Debug("Document path: [{0}]", oData.DocumentPath)
Return oData
End Function
Private Function GetState() As State
Return New State With {
.LogConfig = LogConfig,
.Database = Database,
.UserId = 0,
.Config = Nothing,
.DbConfig = Nothing
}
End Function
End Class
End Namespace

View File

@ -31,5 +31,5 @@ Imports System.Runtime.InteropServices
' indem Sie "*" wie unten gezeigt eingeben:
' <Assembly: AssemblyVersion("1.0.*")>
<Assembly: AssemblyVersion("2.5.0.0")>
<Assembly: AssemblyFileVersion("2.5.0.0")>
<Assembly: AssemblyVersion("2.6.0.0")>
<Assembly: AssemblyFileVersion("2.6.0.0")>

View File

@ -260,6 +260,7 @@
<ItemGroup>
<Compile Include="Config.vb" />
<Compile Include="LogProvider.vb" />
<Compile Include="Scheduler_API_SendEnvelopeMails.vb" />
<Compile Include="Service.Designer.vb">
<DependentUpon>Service.vb</DependentUpon>
</Compile>
@ -288,7 +289,7 @@
<DependentUpon>Settings.settings</DependentUpon>
<DesignTimeSharedInput>True</DesignTimeSharedInput>
</Compile>
<Compile Include="Scheduler.vb" />
<Compile Include="Scheduler_FinishEnvelope.vb" />
<Compile Include="TempFiles.vb" />
</ItemGroup>
<ItemGroup>

View File

@ -0,0 +1,72 @@
Imports System.Collections.Specialized
Imports DigitalData.Modules.Base
Imports DigitalData.Modules.Database
Imports DigitalData.Modules.Logging
Imports EnvelopeGenerator.Common.Jobs
Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument
Imports Quartz
Public Class Scheduler_API_SendEnvelopeMails
Inherits BaseClass
Private Scheduler As IScheduler
Private ReadOnly ConnectionString As String
Private Const JobName = "SendInvitationMailsAPI"
Public Sub New(pLogConfig As LogConfig, pConnectionString As String)
MyBase.New(pLogConfig)
ConnectionString = pConnectionString
Dim oLogProvider = New LogProvider(Logger)
Logging.LogProvider.SetCurrentLogProvider(oLogProvider)
End Sub
Public Async Function Start(pInterval As Integer) As Task
Try
Logger.Debug("Starting Scheduler SendMailsfromAPI..")
Dim oProperties As New NameValueCollection()
Scheduler = Await SchedulerBuilder.Create(oProperties).
UseDefaultThreadPool(Sub(x) x.MaxConcurrency = 5).
BuildScheduler()
Dim oJobKey = New JobKey(JobName)
Dim oJobData = New JobDataMap() From {
{Common.Constants.LOGCONFIG, LogConfig},
{Common.Constants.DATABASE, ConnectionString}
}
Logger.Debug("Initialized Job [{0}]", JobName)
Dim oJob As IJobDetail = JobBuilder.Create(Of SendInvitationMailJob).
UsingJobData(oJobData).
WithIdentity(oJobKey).
Build()
Dim oTrigger As ITrigger = TriggerBuilder.Create().
ForJob(oJobKey).
WithIdentity($"{JobName}-trigger").
WithSimpleSchedule(Sub(s) s.
RepeatForever().
WithIntervalInMinutes(pInterval)).
StartNow().
Build()
Logger.Debug($"{JobName}-trigger initialized")
Await Scheduler.ScheduleJob(oJob, oTrigger)
Logger.Debug($"{JobName}-scheduled")
Await Scheduler.Start()
Logger.Info($"{JobName}-started")
Catch ex As Exception
Logger.Error(ex)
End Try
End Function
Public Async Function [Stop]() As Task
Logger.Info("Stopping scheduler SendMailsfromAPI..")
Await Scheduler.Shutdown()
Logger.Info("Scheduler SendMailsfromAPI stopped!")
End Function
End Class

View File

@ -6,7 +6,7 @@ Imports EnvelopeGenerator.Common.Jobs
Imports EnvelopeGenerator.Common.Jobs.FinalizeDocument
Imports Quartz
Public Class Scheduler
Public Class Scheduler_FinishEnvelope
Inherits BaseClass
Private Scheduler As IScheduler
@ -28,7 +28,7 @@ Public Class Scheduler
Public Async Function Start(pInterval As Integer) As Task
Try
Logger.Debug("Starting Scheduler..")
Logger.Debug("Starting Scheduler Finish Envelope..")
Dim oProperties As New NameValueCollection()

View File

@ -10,7 +10,8 @@ Public Class Service
Private Config As Config
Private Database As MSSQLServer
Private TempFiles As TempFiles
Private Scheduler As Scheduler
Private Scheduler1 As Scheduler_FinishEnvelope
Private Scheduler2 As Scheduler_API_SendEnvelopeMails
Protected Overrides Async Sub OnStart(ByVal args() As String)
Try
@ -53,10 +54,13 @@ Public Class Service
' === Initialize Queue ===
Logger.Debug("Inititalize Quartz")
Logger.Debug("Inititalizing scheduler(s) ...")
Scheduler = New Scheduler(LogConfig, Config.ConnectionString, oKey, Config.PDFBurnerParams)
Await Scheduler.Start(Config.IntervalInMin)
Scheduler1 = New Scheduler_FinishEnvelope(LogConfig, Config.ConnectionString, oKey, Config.PDFBurnerParams)
Await Scheduler1.Start(Config.IntervalInMin)
Scheduler2 = New Scheduler_API_SendEnvelopeMails(LogConfig, Config.ConnectionString)
Await Scheduler2.Start(Config.IntervalInMin)
Logger.Info("Started [{0}] !", ServiceName)
@ -67,8 +71,9 @@ Public Class Service
Protected Overrides Async Sub OnStop()
Try
Logger.Info("Stopping [{0}] !", ServiceName)
Await Scheduler.Stop()
Logger.Info("Stopping [{0}] ...", ServiceName)
Await Scheduler1.Stop()
Await Scheduler2.Stop()
TempFiles.CleanUp()
Logger.Info("Stopped [{0}] !", ServiceName)
Catch ex As Exception