From 8bf6a67979fdb6761d16e59c62c0ff135743c181 Mon Sep 17 00:00:00 2001
From: "m.groppe" <m.groppe@adito.de>
Date: Thu, 1 Jul 2021 13:25:06 +0200
Subject: [PATCH] =?UTF-8?q?[Projekt:=20xRM-Marketing][TicketNr.:=201083402?=
 =?UTF-8?q?][Bounces:=20Review=20und=20Einrichtung=20Demoumgebung=20f?=
 =?UTF-8?q?=C3=BCr=20Schmalz]?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 process/EmailFilterHandling_lib/process.js    | 51 ++++++++++++++++++-
 process/JditoFilter_lib/process.js            | 12 ++---
 .../process.js                                |  4 +-
 3 files changed, 58 insertions(+), 9 deletions(-)

diff --git a/process/EmailFilterHandling_lib/process.js b/process/EmailFilterHandling_lib/process.js
index 1ef6f1da890..c1a75508a4b 100644
--- a/process/EmailFilterHandling_lib/process.js
+++ b/process/EmailFilterHandling_lib/process.js
@@ -35,18 +35,67 @@ IncomingEmailFilterProcessor.prototype.loadFilters = function ()
  */
 IncomingEmailFilterProcessor.prototype.process = function (pEmail)
 {
+    var mailLogId = this.checkBodyForMailLogId(pEmail);
     var bouncedStatus = null;
     this.emailFilters.forEach(function (emailFilter)
     {
         if (emailFilter.checkEmail(pEmail))
         {
-            emailFilter.startWorkflow();
+            if(mailLogId)
+            {
+                var contactId = newSelect("CONTACT_ID")
+                    .from("MAIL_LOG")
+                    .where("MAIL_LOG.MAIL_LOGID", mailLogId)
+                    .cell();
+
+                var recipientEmail = newSelect("RECIPIENT_EMAIL")
+                    .from("MAIL_LOG")
+                    .where("MAIL_LOG.MAIL_LOGID", mailLogId)
+                    .cell();
+
+                emailFilter.startWorkflow(contactId,recipientEmail);
+            }
+
             bouncedStatus = emailFilter.type;
         }
     });
+    if(bouncedStatus && mailLogId)
+    {
+        this.updateMailLog(bouncedStatus, mailLogId);
+    }
     return bouncedStatus;
 }
 
+/*
+ *Checks the Email for a maillogid to document the possible bounce
+*/
+IncomingEmailFilterProcessor.prototype.checkBodyForMailLogId = function (pEmail)
+{
+    var body = pEmail[mail.MAIL_TEXT];
+    
+    var mailLogMatch = body.match(/log=([0-9a-fA-F]{8}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{4}\b-[0-9a-fA-F]{12})/);
+    
+    return mailLogMatch?mailLogMatch[1]:null;
+}
+/*
+ *Updates the matched maillog entry to document the bounce
+ **/
+IncomingEmailFilterProcessor.prototype.updateMailLog = function (pBounceType, pMailLogId)
+{
+    var recipientStatus;
+    switch(pBounceType)
+    {
+        case $KeywordRegistry.emailFilterType$bounceHard():
+            recipientStatus = $KeywordRegistry.bulkMailRecipientStatus$hardBounce();
+            break;
+        case $KeywordRegistry.emailFilterType$bounceSoft():
+            recipientStatus= $KeywordRegistry.bulkMailRecipientStatus$softBounce();
+            break;
+    }
+  
+    newWhere("MAIL_LOG.MAIL_LOGID", pMailLogId).updateFields({"STATUS":recipientStatus});
+}
+
 IncomingEmailFilterProcessor.prototype.processError = function (pErrorMessage, pContactId, pEmailAddress)
 {
     var bouncedStatus = null;
diff --git a/process/JditoFilter_lib/process.js b/process/JditoFilter_lib/process.js
index c2244518ffb..e4855201cbb 100644
--- a/process/JditoFilter_lib/process.js
+++ b/process/JditoFilter_lib/process.js
@@ -303,17 +303,17 @@ JditoFilter.prototype.checkRecord = function (pRow)
         switch (pOperator)
         {
             case "CONTAINS":
-                return (new RegExp(pFilterValue, regexFlags)).test(pRowValue);
+                return (new RegExp(RegExpUtils.escapePatternStr(pFilterValue), regexFlags)).test(pRowValue);
             case "CONTAINSNOT":
-                return !(new RegExp(pFilterValue, regexFlags)).test(pRowValue);
+                return !(new RegExp(RegExpUtils.escapePatternStr(pFilterValue), regexFlags)).test(pRowValue);
             case "STARTSWITH":
-                return (new RegExp("^" + pFilterValue, regexFlags)).test(pRowValue);
+                return (new RegExp("^" + RegExpUtils.escapePatternStr(pFilterValue), regexFlags)).test(pRowValue);
             case "ENDSWITH":
-                return (new RegExp(pFilterValue + "$", regexFlags)).test(pRowValue);
+                return (new RegExp(RegExpUtils.escapePatternStr(pFilterValue) + "$", regexFlags)).test(pRowValue);
             case "EQUAL":
-                return (new RegExp("^" + pFilterValue + "$", regexFlags)).test(pRowValue);
+                return (new RegExp("^" + RegExpUtils.escapePatternStr(pFilterValue) + "$", regexFlags)).test(pRowValue);
             case "NOT_EQUAL":
-                return !(new RegExp("^" + pFilterValue + "$", regexFlags)).test(pRowValue);
+                return !(new RegExp("^" + RegExpUtils.escapePatternStr(pFilterValue) + "$", regexFlags)).test(pRowValue);
             case "LESS":
                 return pRowValue < pFilterValue;
             case "LESS_OR_EQUAL":
diff --git a/process/SetCommunicationSetting_workflowService/process.js b/process/SetCommunicationSetting_workflowService/process.js
index c755964cca8..dccc2bf4d42 100644
--- a/process/SetCommunicationSetting_workflowService/process.js
+++ b/process/SetCommunicationSetting_workflowService/process.js
@@ -52,7 +52,7 @@ else
             "MEDIUM": medium,
             "STATUS": status,
             "USER_NEW":"workflow",
-            "DATE_NEW":date
+            "DATE_NEW":currentDate
         });
     new SqlBuilder()
         .tableName("COMMUNICATIONLEGALBASE")
@@ -61,6 +61,6 @@ else
             "COMMUNICATIONSETTINGS_ID": settingsId,
             "VERSION": 1,
             "USER_NEW":"workflow",
-            "DATE_NEW":date
+            "DATE_NEW":currentDate
         });
 }
\ No newline at end of file
-- 
GitLab