From 2898da380d12d9ba6b153bba2bca590c7b9f5b3d Mon Sep 17 00:00:00 2001 From: pitzm Date: Wed, 24 Apr 2024 11:19:41 +0200 Subject: [PATCH] EMailProfiler Common: Neue Ablehnungsmail-Logik --- .../EmailProfiler.Common.vbproj | 1 + App/EmailProfiler.Common/EmailStrings.vb | 34 +--- App/EmailProfiler.Common/ErrorCode.vb | 5 + App/EmailProfiler.Common/clsWorkEmail.vb | 150 ++++++++++++++++-- App/EmailProfiler.Common/clsWorker.vb | 5 +- 5 files changed, 153 insertions(+), 42 deletions(-) create mode 100644 App/EmailProfiler.Common/ErrorCode.vb diff --git a/App/EmailProfiler.Common/EmailProfiler.Common.vbproj b/App/EmailProfiler.Common/EmailProfiler.Common.vbproj index 11410d7..339c489 100644 --- a/App/EmailProfiler.Common/EmailProfiler.Common.vbproj +++ b/App/EmailProfiler.Common/EmailProfiler.Common.vbproj @@ -118,6 +118,7 @@ + diff --git a/App/EmailProfiler.Common/EmailStrings.vb b/App/EmailProfiler.Common/EmailStrings.vb index a7831b2..b1c8a64 100644 --- a/App/EmailProfiler.Common/EmailStrings.vb +++ b/App/EmailProfiler.Common/EmailStrings.vb @@ -5,36 +5,12 @@ Public Const EMAIL_SUBJECT_TEXT = "

Der Betreff der Original-Email war: {0}

" Public Const EMAIL_SUBJECT_REJECTED = "Beleg abgelehnt" - Public Const EMAIL_SUBJECT_EXCEPTION = "Unbehandelte Ausnahme" - - Public Const EMAIL_UNHANDLED_EXCEPTION = """ -

Beim Verarbeiten der Datei mit der Message ID '{0}' ist ein schwerer Fehler aufgetreten.

-

Fehlerbeschreibung: {1}

-
{2}
- """ - - Public Const EMAIL_MISSINGPROPERTIES_1 = "

Die angehängte Datei entspricht nicht dem ZUGFeRD-Format: {0}

" - Public Const EMAIL_MISSINGPROPERTIES_2 = "

Die folgenden Eigenschaften wurden als ERFORDERLICH eingestuft, wurden aber nicht gefunden:

" - - - - Public Const EMAIL_MD5_ERROR = "

Die von Ihnen gesendete Rechnung wurde bereits von unserem System verarbeitet.

" - - Public Const EMAIL_TOO_MUCH_FERDS = "

Ihre Email enthielt mehr als ein ZUGFeRD-Dokument.

" Public Const EMAIL_NO_FERDS = "

Ihre Email enthielt keine Dokumente.

" - Public Const EMAIL_FILE_SIZE_REACHED = " -

Die von Ihnen gesendete Rechnung oder einer der Rechnungs-Anhänge überschreitet die erlaubte Größe von {0} MB.

-

Die folgende Datei hat die erlaubte Größe überschritten:

    -
  • {1}
  • -

- " - - Public Const EMAIL_INVALID_DOCUMENT = " -

Ihre Email enthielt ein ZUGFeRD Dokument, welches aber inkorrekt formatiert wurde.

-

Mögliche Gründe für ein inkorrektes Format:

    -
  • Betrags-Werte weisen ungültiges Format auf (25,01 anstatt 25.01)
  • -

- " + ''' + ''' Präfix für den verwendeten ErrorCode + ''' Beispiel: EMLP_Rejection_10001 + ''' + Public Const ErrorCodePraefix = "EMLP_Rejection_" End Class diff --git a/App/EmailProfiler.Common/ErrorCode.vb b/App/EmailProfiler.Common/ErrorCode.vb new file mode 100644 index 0000000..9f9282d --- /dev/null +++ b/App/EmailProfiler.Common/ErrorCode.vb @@ -0,0 +1,5 @@ +Public Enum ErrorCode + Unknown = 0 + NoAttachments = 10001 + SenderValidationFailed = 10002 +End Enum diff --git a/App/EmailProfiler.Common/clsWorkEmail.vb b/App/EmailProfiler.Common/clsWorkEmail.vb index 67c37fc..41e75e0 100644 --- a/App/EmailProfiler.Common/clsWorkEmail.vb +++ b/App/EmailProfiler.Common/clsWorkEmail.vb @@ -41,10 +41,11 @@ Public Class clsWorkEmail Private ReadOnly _Patterns As Patterns2 Private ReadOnly _EmailAccountID As Integer = 1 + Private ReadOnly _RejectionTemplateId As Integer = 0 Private _worked_email As Boolean = False - Sub New(LogConf As LogConfig, ConStr As String, WmConStr As String, pUseWindream As Boolean, EmailAccountID As Integer, EmlProfPraefix As String) + Sub New(LogConf As LogConfig, ConStr As String, WmConStr As String, pUseWindream As Boolean, EmailAccountID As Integer, EmlProfPraefix As String, pRejectionTemplateId As Integer) Try _Logger = LogConf.GetLogger _LogConfig = LogConf @@ -52,6 +53,8 @@ Public Class clsWorkEmail _Logger.Debug("clsWorkmail _email initialized") _UseWindream = pUseWindream _Patterns = New Patterns2(LogConf) + _RejectionTemplateId = pRejectionTemplateId + _Logger.Debug($"_RejectionTemplateId: {_RejectionTemplateId}") If pUseWindream Then _windream = New clsWindream_allgemein(LogConf) @@ -162,7 +165,9 @@ Public Class clsWorkEmail 'insert history und exit InsertHistoryEntryWithStatus(CurrentMail, "REJECTED", oResult) - AddEmailToQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID) + 'AddEmailToQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID) + AddToEmailQueueMSSQL(CurrentMail.MessageId, oResult, "Email validation failed", _EmailAccountID, + _RejectionTemplateId, ErrorCode.SenderValidationFailed, "", "") ' Return early from processing eml Return True @@ -186,7 +191,9 @@ Public Class clsWorkEmail If CURRENT_ATTMT_COUNT = 0 Then _Logger.Info("### Mail contained no Attachments!! ###") Dim oBody = EmailStrings.EMAIL_NO_FERDS - If AddEmailToQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then + 'If AddEmailToQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID) = True Then + If AddToEmailQueueMSSQL(CurrentMail.MessageId, oBody, "No Attachments", _EmailAccountID, + _RejectionTemplateId, ErrorCode.NoAttachments, "", "") = True Then CURRENT_ImapObject.DeleteMessageByUID(poUID) End If InsertHistoryEntryWithStatus(CurrentMail, "REJECTED", "No Attachments") @@ -204,15 +211,136 @@ Public Class clsWorkEmail End Try End Function - Public Function AddEmailToQueueMSSQL(MessageId As String, BodyText As String, Comment As String, pEmailAccountId As Integer) As Boolean + ''' + ''' Method to decide wether we use the old or the new + ''' Rejection E-mail method. + ''' + ''' TODO we have no information about the language of the receiver at the moment + ''' + ''' E-Mail Message ID + ''' Body Text + ''' Comment + ''' Sending Profile from config + ''' ID for E-Mail-Template from config + ''' Error Code + ''' Zusätzlicher Parameter 1 + ''' Zusätzlicher Parameter 2 + Public Function AddToEmailQueueMSSQL(pMessageId As String, pBodyText As String, pComment As String, pEmailAccountId As Integer, + pTemplateId As Integer, pErrorCode As ErrorCode, pParameter1 As String, pParameter2 As String) As Boolean + + Dim useLegacyMethod = True + Dim oErrorCode As String = String.Empty + + ' ErrorCode valid? + If pErrorCode <> ErrorCode.Unknown Then + Dim intCode As Integer = DirectCast(pErrorCode, Integer) + oErrorCode = $"{EmailStrings.ErrorCodePraefix}{intCode}" + + Dim oSQL = $"SELECT COUNT(*) FROM TBDD_GUI_LANGUAGE_PHRASE WHERE TITLE = '{oErrorCode}'" + If _DB_MSSQL.GetScalarValue(oSQL) > 0 Then + useLegacyMethod = False + Else + _Logger.Warn($"Rejection reason [{oErrorCode}] not found in TBDD_GUI_LANGUAGE_PHRASE!") + End If + End If + + ' Gibt es das Template in TBDD_EMAIL_TEMPLATE? + If useLegacyMethod = False AndAlso pTemplateId > 0 Then + Try + Dim oSQL = $"SELECT COUNT(*) FROM TBDD_EMAIL_TEMPLATE WHERE GUID = {pTemplateId}" + If _DB_MSSQL.GetScalarValue(oSQL) <= 0 Then + _Logger.Warn($"EMAIL_TEMPLATE [{pTemplateId}] not found in TBDD_EMAIL_TEMPLATE!") + useLegacyMethod = True + End If + Catch ex As Exception + _Logger.Error(ex) + useLegacyMethod = True + End Try + + Else + _Logger.Debug($"RejectionTemplateId not configured!") + useLegacyMethod = True + End If + + ' Check if Stored Procedure PRDD_SEND_REJECTION_MAIL exists + If useLegacyMethod = False Then + Try + Dim oSQL = $"SELECT COUNT(*) FROM sys.objects WHERE type = 'P' AND OBJECT_ID = OBJECT_ID('dbo.PRDD_SEND_REJECTION_MAIL')" + If _DB_MSSQL.GetScalarValue(oSQL) <= 0 Then + _Logger.Warn($"Procedure ['PRDD_SEND_REJECTION_MAIL'] not found in Database!") + useLegacyMethod = True + End If + Catch ex As Exception + _Logger.Error(ex) + useLegacyMethod = True + End Try + End If + + If useLegacyMethod = True Then + _Logger.Warn("New rejection mail logic is not configured correctly, use legacy logic instead!") + Return AddEmailToQueueMSSQL(pMessageId, pBodyText, pComment, pEmailAccountId) + Else + _Logger.Debug("New rejection mail logic is configured!") + Return AddEmailToQueueMSSQL(pMessageId, pTemplateId, oErrorCode, pEmailAccountId, pParameter1, pParameter2) + End If + + End Function + + ''' + ''' Function calls SP PRDD_SEND_REJECTION_MAIL + ''' for sending rejection mail. + ''' + ''' E-Mail Message ID + ''' GUID for TBDD_EMAIL_TEMPLATE from config + ''' ErrorID (TBDD_GUI_LANGUAGE_PHRASE) + ''' Sending profile from config + ''' Zusätzlicher Parameter 1 + ''' Zusätzlicher Parameter 2 + Private Function AddEmailToQueueMSSQL(pMessageId As String, pTemplateId As Integer, pErrorCode As String, pEmailAccountId As Integer, + pParameter1 As String, pParameter2 As String) As Boolean + + If pParameter1.IsNullOrEmpty Then + pParameter1 = "" + Else + pParameter1 = pParameter1.Replace("'", "''") + End If + + If pParameter2.IsNullOrEmpty Then + pParameter2 = "" + Else + pParameter2 = pParameter2.Replace("'", "''") + End If + + Try + Dim oExecute = $"EXECUTE dbo.PRDD_SEND_REJECTION_MAIL + '{pMessageId}' + , 0 + , {pEmailAccountId} + , 'ZUGFeRD Service' + , {pTemplateId} + , '{pErrorCode}' + , '{pParameter1}' + , '{pParameter2}' + , 77" + + Return _DB_MSSQL.ExecuteNonQuery(oExecute) + + Catch ex As Exception + _Logger.Error(ex) + Return False + End Try + + End Function + + Public Function AddEmailToQueueMSSQL(pMessageId As String, pBodyText As String, pComment As String, pEmailAccountId As Integer) As Boolean Try - Dim oReference = MessageId + Dim oReference = pMessageId Dim oEmailTo = "" Dim oSubject = $"{SUBJECT_PRAFIX} - {EmailStrings.EMAIL_SUBJECT_REJECTED}" Dim oCreatedWho = "DDEmailProfiler" - Dim oMaskedBodyText = BodyText.Replace("'", "''") + Dim oMaskedBodyText = pBodyText.Replace("'", "''") Dim oSubjectBodyText = String.Format(EmailStrings.EMAIL_SUBJECT_TEXT, CURRENT_MAIL_SUBJECT) Dim oCompleteBodyText = oMaskedBodyText & oSubjectBodyText @@ -222,7 +350,7 @@ Public Class clsWorkEmail If IsNothing(oEmailAddress) OrElse String.IsNullOrWhiteSpace(oEmailAddress) Then - _Logger.Warn("Could not find email-address for MessageId {0}", MessageId) + _Logger.Warn("Could not find email-address for MessageId {0}", pMessageId) oEmailTo = String.Empty Else oEmailTo = oEmailAddress @@ -232,7 +360,7 @@ Public Class clsWorkEmail _Logger.Debug("To: {0}", oEmailTo) _Logger.Debug("Subject: {0}", oSubject) _Logger.Debug("Body {0}", oFinalBodyText) - Dim osql = $"Select COALESCE(MAX(GUID), 0) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{MessageId}'" + Dim osql = $"Select COALESCE(MAX(GUID), 0) FROM TBEMLP_HISTORY WHERE EMAIL_MSGID = '{pMessageId}'" Dim oHistoryID As Integer = _DB_MSSQL.GetScalarValue(osql) Dim oInsert = $"INSERT INTO [dbo].[TBEMLP_EMAIL_OUT] ( @@ -250,14 +378,14 @@ Public Class clsWorkEmail (77 ,{pEmailAccountId} ,{oHistoryID} - ,'{MessageId}' + ,'{pMessageId}' ,77 ,'{oEmailTo}' ,'{oSubject}' ,'{oFinalBodyText}' - ,'{Comment}' + ,'{pComment}' ,'{oCreatedWho}')" - Return _DB_MSSQL.ExecuteNonQuery(oInsert) + Return _DB_MSSQL.ExecuteNonQuery(oInsert) Catch ex As Exception _Logger.Error(ex) diff --git a/App/EmailProfiler.Common/clsWorker.vb b/App/EmailProfiler.Common/clsWorker.vb index 0a2947c..aeeda1e 100644 --- a/App/EmailProfiler.Common/clsWorker.vb +++ b/App/EmailProfiler.Common/clsWorker.vb @@ -24,7 +24,8 @@ Public Class clsWorker Private ReadOnly LocalEmlFile As String = "" - Sub New(pLogConfig As LogConfig, pConnectionString As String, pWindreamConnectionString As String, pPollProfileId As Integer, pUseWindream As Boolean, pEmailAccountID As Integer, pEmailPrefix As String, pEmailLimitationSender As String, Optional pLocalEML As String = "") + Sub New(pLogConfig As LogConfig, pConnectionString As String, pWindreamConnectionString As String, pPollProfileId As Integer, pUseWindream As Boolean, + pEmailAccountID As Integer, pEmailPrefix As String, pEmailLimitationSender As String, pRejectionTemplateId As Integer, Optional pLocalEML As String = "") Logger = pLogConfig.GetLogger 'ClassEmailImap = New clsEmailIMAP(pLogConfig) Fetcher = New MailFetcher(pLogConfig) @@ -42,7 +43,7 @@ Public Class clsWorker ClassWindreamIndex = New clsWindream_Index(pLogConfig) End If - ClassWorkMail = New clsWorkEmail(pLogConfig, pConnectionString, pWindreamConnectionString, pUseWindream, pEmailAccountID, pEmailPrefix) + ClassWorkMail = New clsWorkEmail(pLogConfig, pConnectionString, pWindreamConnectionString, pUseWindream, pEmailAccountID, pEmailPrefix, pRejectionTemplateId) Encryption = New clsEncryption("!35452didalog=", pLogConfig)