From 8f3acd58f5e85b28a5db3a3f4b250887aab0f47f Mon Sep 17 00:00:00 2001 From: Jonathan Jenne Date: Thu, 19 Nov 2020 13:17:25 +0100 Subject: [PATCH] Jobs: Add Subfolders per Date for error/rejected zugferd invoices --- .../EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb | 42 ++++++++++++++++--- 1 file changed, 37 insertions(+), 5 deletions(-) diff --git a/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb b/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb index c11cecab..04e3b4dd 100644 --- a/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb +++ b/Modules.Jobs/EDMI/ZUGFeRD/ImportZUGFeRDFiles.vb @@ -63,14 +63,26 @@ Public Class ImportZUGFeRDFiles Private Function MoveAndRenameEmailToRejected(Args As WorkerArgs, MessageId As String) As EmailData Dim oEmailData = _email.GetEmailDataForMessageId(MessageId) Dim oSource = _email.GetOriginalEmailPath(Args.OriginalEmailDirectory, MessageId) + Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd") Dim oDestination As String + Dim oRejectedDirectory As String = Path.Combine(Args.RejectedEmailDirectory, oDateSubDirectoryName) + + ' Create the destination directory if it does not exist + If Not Directory.Exists(oRejectedDirectory) Then + Try + Directory.CreateDirectory(oRejectedDirectory) + Catch ex As Exception + _logger.Error(ex) + End Try + End If + ' If oEmailData is Nothing, TBEDM_EMAIL_PROFILER_HISTORY for MessageId was not found. ' This only should happen when testing and db-tables are deleted frequently If oEmailData Is Nothing Then - oDestination = _email.GetEmailPathWithSubjectAsName(Args.RejectedEmailDirectory, MessageId) + oDestination = _email.GetEmailPathWithSubjectAsName(oRejectedDirectory, MessageId) Else - oDestination = _email.GetEmailPathWithSubjectAsName(Args.RejectedEmailDirectory, oEmailData.Subject) + oDestination = _email.GetEmailPathWithSubjectAsName(oRejectedDirectory, oEmailData.Subject) End If _logger.Debug("Destination for eml file is {0}", oDestination) @@ -151,6 +163,10 @@ Public Class ImportZUGFeRDFiles Dim oZUGFeRDCount As Integer = 0 ' Set the default Move Directory Dim oMoveDirectory As String = oArgs.SuccessDirectory + ' Flag to save if the whole process was a success. + ' Will be set only at the end of the function if no error occurred. + Dim oIsSuccess As Boolean = False + ' Create file lists Dim oFileGroupFiles As List(Of FileInfo) = oFileGroup.Value Dim oEmailAttachmentFiles As New List(Of FileInfo) @@ -478,6 +494,8 @@ Public Class ImportZUGFeRDFiles End Try End If + oIsSuccess = True + Catch ex As MD5HashException _logger.Error(ex) oMoveDirectory = oArgs.ErrorDirectory @@ -546,7 +564,7 @@ Public Class ImportZUGFeRDFiles ' Move all files of the current group Try - MoveFiles(oArgs, oMessageId, oFileGroupFiles, oEmailAttachmentFiles, oEmbeddedAttachmentFiles, oMoveDirectory) + MoveFiles(oArgs, oMessageId, oFileGroupFiles, oEmailAttachmentFiles, oEmbeddedAttachmentFiles, oMoveDirectory, oIsSuccess) _logger.Info("Finished processing file group {0}", oMessageId) Catch ex As Exception _logger.Warn("Could not move files!") @@ -567,9 +585,23 @@ Public Class ImportZUGFeRDFiles End Try End Sub - Private Sub MoveFiles(Args As WorkerArgs, MessageId As String, Files As List(Of FileInfo), AttachmentFiles As List(Of FileInfo), EmbeddedAttachments As List(Of PDFAttachments.AttachmentResult), MoveDirectory As String) + Private Sub MoveFiles( + Args As WorkerArgs, + MessageId As String, + Files As List(Of FileInfo), + AttachmentFiles As List(Of FileInfo), + EmbeddedAttachments As List(Of PDFAttachments.AttachmentResult), + MoveDirectory As String, + IsSuccess As Boolean) + Dim oFinalMoveDirectory As String = MoveDirectory - Dim oAttachmentDirectory As String = Path.Combine(MoveDirectory, Args.AttachmentsSubDirectory) + Dim oDateSubDirectoryName As String = Now.ToString("yyyy-MM-dd") + Dim oAttachmentDirectory As String = Path.Combine(MoveDirectory, Args.AttachmentsSubDirectory, oDateSubDirectoryName) + + ' Files will be moved to a subfolder for the current day if they are rejected + If Not IsSuccess Then + oFinalMoveDirectory = Path.Combine(oFinalMoveDirectory, oDateSubDirectoryName) + End If ' Create directories if they don't exist If Not Directory.Exists(oFinalMoveDirectory) Then