Updated ReceiverModel and EnvelopeEditorController to utilize ReceiverVM instead of Receiver in multiple function signatures. This change enhances the structure and flexibility of receiver data handling. Adjusted related methods in frmEnvelopeEditor to align with the new view model approach.
285 lines
11 KiB
VB.net
285 lines
11 KiB
VB.net
Imports System.Data.SqlClient
|
|
Imports DigitalData.Modules.Base
|
|
Imports EnvelopeGenerator.Domain.Constants
|
|
Imports EnvelopeGenerator.Domain.Entities
|
|
|
|
Public Class ReceiverModel
|
|
Inherits BaseModel
|
|
|
|
Public Sub New(pState As State)
|
|
MyBase.New(pState)
|
|
End Sub
|
|
|
|
Private Function ToReceiver(pRow As DataRow, pColorIndex As Integer) As ReceiverVM
|
|
Dim EmailAdress As String = pRow.ItemEx("EMAIL_ADDRESS", "")
|
|
Dim EnvelopeId As Integer = pRow.ItemEx("ENVELOPE_ID", 0)
|
|
Dim SignedDate As DateTime = DateTime.MinValue
|
|
Dim ReceiverSignedStatus As ReceiverStatus = ReceiverStatus.Unsigned
|
|
|
|
If String.IsNullOrEmpty(EmailAdress) = False Then
|
|
SignedDate = GetSignedDate(EmailAdress, EnvelopeId)
|
|
If SignedDate <> DateTime.MinValue Then
|
|
ReceiverSignedStatus = ReceiverStatus.Signed
|
|
End If
|
|
End If
|
|
|
|
Return New ReceiverVM() With {
|
|
.Id = pRow.ItemEx("GUID", 0),
|
|
.EmailAddress = pRow.ItemEx("EMAIL_ADDRESS", ""),
|
|
.Name = pRow.ItemEx("NAME", ""),
|
|
.Sequence = pRow.ItemEx("SEQUENCE", 0),
|
|
.Signature = pRow.ItemEx("SIGNATURE", ""),
|
|
.Status = ReceiverSignedStatus,
|
|
.ColorType = DirectCast(pColorIndex + 1, ColorType),
|
|
.AccessCode = pRow.ItemEx("ACCESS_CODE", ""),
|
|
.SignedDate = SignedDate,
|
|
.PhoneNumber = pRow.ItemEx("PHONE_NUMBER", "")
|
|
}
|
|
End Function
|
|
|
|
Public Function Insert(pReceiver As ReceiverVM, pTransaction As SqlTransaction) As Boolean
|
|
Dim oSignature As String = pReceiver.GetSignature()
|
|
Dim oCheck = $"SELECT COUNT(GUID) FROM [dbo].[TBSIG_RECEIVER] WHERE SIGNATURE = '{oSignature}'"
|
|
Dim oExists = Database.GetScalarValue(oCheck)
|
|
|
|
If oExists = 0 Then
|
|
Dim oSql As String = $"INSERT INTO [dbo].[TBSIG_RECEIVER]
|
|
([EMAIL_ADDRESS]
|
|
,[SIGNATURE])
|
|
VALUES
|
|
('{pReceiver.EmailAddress}'
|
|
,'{pReceiver.GetSignature()}')"
|
|
|
|
Dim oCommand = New SqlCommand(oSql)
|
|
Dim oResult = Database.ExecuteNonQuery(oCommand)
|
|
If oResult = True Then
|
|
pReceiver.Id = GetReceiverIdByEmail(pReceiver.EmailAddress, pTransaction)
|
|
Return True
|
|
Else
|
|
Return False
|
|
End If
|
|
Else
|
|
Logger.Warn($"Receiver [{pReceiver.EmailAddress}] already existing! SignatureID: {oSignature} Check SQL {oCheck}")
|
|
Return True
|
|
End If
|
|
End Function
|
|
|
|
Public Function Unassign(pEnvelope As Envelope, pTransaction As SqlTransaction) As Boolean
|
|
Try
|
|
Return Database.ExecuteNonQuery($"DELETE FROM [dbo].[TBSIG_ENVELOPE_RECEIVER] WHERE [ENVELOPE_ID] = {pEnvelope.Id}", pTransaction)
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
Public Function Assign(pEnvelope As Envelope, pReceiver As ReceiverVM, pTransaction As SqlTransaction) As Boolean
|
|
Dim oSql = $"INSERT INTO [dbo].[TBSIG_ENVELOPE_RECEIVER]
|
|
([ENVELOPE_ID]
|
|
,[RECEIVER_ID]
|
|
,[PRIVATE_MESSAGE]
|
|
,[ACCESS_CODE]
|
|
,[NAME]
|
|
,[JOB_TITLE]
|
|
,[COMPANY_NAME]
|
|
,[SEQUENCE]
|
|
,[PHONE_NUMBER])
|
|
VALUES
|
|
('{pEnvelope.Id}'
|
|
,'{pReceiver.Id}'
|
|
,'{pReceiver.PrivateMessage}'
|
|
,'{pReceiver.AccessCode}'
|
|
,'{pReceiver.Name}'
|
|
,'{pReceiver.JobTitle}'
|
|
,'{pReceiver.CompanyName}'
|
|
,'{pReceiver.Sequence}'
|
|
,'{pReceiver.PhoneNumber}')"
|
|
|
|
Dim oCommand As New SqlCommand(oSql)
|
|
'oCommand.Parameters.Add("ENVELOPE_ID", SqlDbType.NVarChar).Value = pEnvelope.Id
|
|
'oCommand.Parameters.Add("RECEIVER_ID", SqlDbType.NVarChar).Value = pReceiver.Id
|
|
'oCommand.Parameters.Add("MESSAGE", SqlDbType.NVarChar).Value = pReceiver.PrivateMessage
|
|
'oCommand.Parameters.Add("ACCESS_CODE", SqlDbType.NVarChar).Value = pReceiver.AccessCode
|
|
'oCommand.Parameters.Add("NAME", SqlDbType.NVarChar).Value = pReceiver.Name
|
|
'oCommand.Parameters.Add("JOB", SqlDbType.NVarChar).Value = pReceiver.JobTitle
|
|
'oCommand.Parameters.Add("COMPANY", SqlDbType.NVarChar).Value = pReceiver.CompanyName
|
|
'oCommand.Parameters.Add("SEQUENCE", SqlDbType.NVarChar).Value = pReceiver.Sequence
|
|
|
|
Return Database.ExecuteNonQuery(oCommand, pTransaction)
|
|
End Function
|
|
|
|
Public Function ListEnvelopeReceivers(pEnvelopeId As Integer) As IEnumerable(Of ReceiverVM)
|
|
Try
|
|
Dim oSql = $"SELECT * FROM [dbo].[VWSIG_ENVELOPE_RECEIVERS] WHERE ENVELOPE_ID = {pEnvelopeId}"
|
|
Dim oTable = Database.GetDatatable(oSql)
|
|
|
|
Return oTable?.Rows.Cast(Of DataRow).
|
|
Select(AddressOf ToReceiver).
|
|
ToList()
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function ListAllEnvelopeReceiverAddresses(pUserId As Integer) As IEnumerable(Of String)
|
|
Try
|
|
Dim oSql = $"SELECT DISTINCT T.EMAIL_ADDRESS FROM TBSIG_RECEIVER T
|
|
JOIN TBSIG_ENVELOPE_RECEIVER T2 ON T.GUID = T2.RECEIVER_ID
|
|
JOIN TBSIG_ENVELOPE T3 ON T2.ENVELOPE_ID = T3.GUID
|
|
WHERE T3.USER_ID = {pUserId}"
|
|
Dim oTable = Database.GetDatatable(oSql)
|
|
|
|
Return oTable?.Rows.Cast(Of DataRow).
|
|
Select(Function(r) r.Item("EMAIL_ADDRESS")).
|
|
Cast(Of String).
|
|
ToList()
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return New List(Of String)
|
|
End Try
|
|
End Function
|
|
|
|
Public Function ListReceivers() As IEnumerable(Of Receiver)
|
|
Try
|
|
Dim oSql = $"SELECT * FROM [dbo].[TBSIG_RECEIVER]"
|
|
Dim oTable = Database.GetDatatable(oSql)
|
|
|
|
Return oTable?.Rows.Cast(Of DataRow).
|
|
Select(AddressOf ToReceiver).
|
|
ToList()
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function ListReceivers(pReceiversFromGrid As List(Of ReceiverVM)) As IEnumerable(Of Receiver)
|
|
Try
|
|
If pReceiversFromGrid.Count = 0 Then
|
|
Return New List(Of Receiver)
|
|
End If
|
|
|
|
Dim oAddresses = pReceiversFromGrid.Select(Function(r) $"'{r.EmailAddress}'").JoinToString(",")
|
|
|
|
Dim oSql = $"SELECT * FROM [dbo].[TBSIG_RECEIVER] WHERE EMAIL_ADDRESS IN ({oAddresses})"
|
|
Dim oTable = Database.GetDatatable(oSql)
|
|
|
|
Return oTable?.Rows.Cast(Of DataRow).
|
|
Select(AddressOf ToReceiver).
|
|
ToList()
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function Delete(pReceiverId As Integer, pEnvelopeId As Integer, pTransaction As SqlTransaction) As Boolean
|
|
Try
|
|
Dim oSql = $"DELETE FROM TBSIG_ENVELOPE_RECEIVER WHERE RECEIVER_ID = {pReceiverId} AND ENVELOPE_ID = {pEnvelopeId}"
|
|
Return Database.ExecuteNonQuery(oSql, pTransaction)
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return False
|
|
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetById(pReceiverId As Integer) As Receiver
|
|
'Try
|
|
' Dim oSql = $"SELECT * FROM [dbo].[VWSIG_ENVELOPE_RECEIVERS] WHERE RECEIVER_ID = {pReceiverId}"
|
|
' Dim oTable = Database.GetDatatable(oSql)
|
|
|
|
' Return ToReceiver(oTable)
|
|
|
|
'Catch ex As Exception
|
|
' Logger.Error(ex)
|
|
' Return Nothing
|
|
'End Try
|
|
Return Nothing
|
|
End Function
|
|
|
|
Public Function GetReceiverIdBySignature(pSignature As String) As Integer
|
|
Try
|
|
Return Database.GetScalarValue($"SELECT GUID FROM TBSIG_RECEIVER WHERE SIGNATURE = '{pSignature}'")
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function GetLastUsedReceiverName(pEmailAddress As String, pUserId As Integer) As String
|
|
Try
|
|
Dim oSql As String
|
|
oSql = "SELECT TOP 1 [NAME] FROM dbo.VWSIG_ENVELOPE_RECEIVERS "
|
|
oSql += $" WHERE ENVELOPE_ID IN (SELECT GUID FROM dbo.TBSIG_ENVELOPE WHERE [USER_ID] = {pUserId}) "
|
|
oSql += $" AND EMAIL_ADDRESS = '{pEmailAddress}' "
|
|
oSql += " AND Len([NAME]) > 0 "
|
|
oSql += " ORDER BY [ADDED_WHEN] DESC"
|
|
|
|
Return Database.GetScalarValue(oSql)
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return String.Empty
|
|
End Try
|
|
End Function
|
|
Public Function GetLastUsedReceiverPhone(pEmailAddress As String, pUserId As Integer) As String
|
|
Try
|
|
Dim oSql As String
|
|
oSql = "SELECT TOP 1 [PHONE_NUMBER] FROM dbo.VWSIG_ENVELOPE_RECEIVERS "
|
|
oSql += $" WHERE ENV_USERID_CREATED = {pUserId} "
|
|
oSql += $" AND EMAIL_ADDRESS = '{pEmailAddress}' "
|
|
oSql += " AND Len([PHONE_NUMBER]) > 0 "
|
|
oSql += " ORDER BY [ADDED_WHEN] DESC"
|
|
|
|
Return Database.GetScalarValue(oSql)
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return String.Empty
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GetReceiverIdByEmail(pEmailAddress As String, pTransaction As SqlTransaction) As Integer
|
|
Try
|
|
Return Database.GetScalarValue($"SELECT GUID FROM TBSIG_RECEIVER WHERE EMAIL_ADDRESS = '{pEmailAddress}'", pTransaction)
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Private Function GetSignedDate(pEmailAddress As String, pEnvelopeId As Integer) As Date
|
|
Try
|
|
Dim oStatusInt As Integer = EnvelopeStatus.DocumentSigned
|
|
Return Database.GetScalarValue($"SELECT ACTION_DATE FROM [DD_ECM].[dbo].[TBSIG_ENVELOPE_HISTORY] WHERE ENVELOPE_ID = {pEnvelopeId}
|
|
And USER_REFERENCE = '{pEmailAddress}' AND [STATUS] = {oStatusInt}")
|
|
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return Nothing
|
|
End Try
|
|
End Function
|
|
|
|
Public Function AccessCodeAlreadyRequested(pEmailAddress As String, pEnvelopeId As Integer) As Boolean
|
|
Try
|
|
Dim oStatusInt As Integer = EnvelopeStatus.AccessCodeRequested
|
|
Dim oResult As Integer = Database.GetScalarValue($"SELECT COUNT(*) FROM [DD_ECM].[dbo].[TBSIG_ENVELOPE_HISTORY]
|
|
WHERE ENVELOPE_ID = {pEnvelopeId} And USER_REFERENCE = '{pEmailAddress}' AND [STATUS] = {oStatusInt}")
|
|
|
|
Return oResult > 0
|
|
Catch ex As Exception
|
|
Logger.Error(ex)
|
|
Return False
|
|
End Try
|
|
End Function
|
|
|
|
End Class
|