From ea3f8d51c2829c6f65f0e95069dccb502a308976 Mon Sep 17 00:00:00 2001
From: "S.Listl" <S.Listl@SLISTL.aditosoftware.local>
Date: Mon, 29 Jul 2019 16:10:31 +0200
Subject: [PATCH] Campaign participant chart 'Participated' fixed

---
 .../recordcontainers/jdito/onInsert.js                     | 7 ++-----
 .../recordcontainers/jdito/contentProcess.js               | 5 ++++-
 process/Attribute_lib/process.js                           | 4 +++-
 3 files changed, 9 insertions(+), 7 deletions(-)

diff --git a/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js b/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js
index 8e30f7b718..12f66113e1 100644
--- a/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js
+++ b/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js
@@ -73,12 +73,11 @@ function _handleRowIds(pParticipantRowIds)
         
         let oldSteps = db.table(SqlCondition.begin()
             .orIn("CAMPAIGNPARTICIPANT.CAMPAIGNPARTICIPANTID", pParticipantRowIds)
-            .orIn("CAMPAIGNPARTICIPANT.CONTACT_ID", pParticipantRowIds)
             .buildSql("select CAMPAIGNPARTICIPANTID, CAMPAIGNSTEP_ID from CAMPAIGNPARTICIPANT", "1=2")
         );
         oldSteps.forEach(function (step)
         {
-            
+            this[step[0]] = step[1];
         }, oldStepIds);
     }
     else
@@ -101,9 +100,8 @@ function _handleRowIds(pParticipantRowIds)
 
         if(isUpdate == "true")
         {
-            let oldCampaignStepId = "";
+            let oldCampaignStepId = oldStepIds[pParticipantRowIds[participant]] || "";
             let updatedValues = [newCampaignStepId,
-                            oldCampaignStepId,
                             vars.get("$sys.user"),
                             vars.get("$sys.date")];
 
@@ -144,7 +142,6 @@ function _handleRowIds(pParticipantRowIds)
         }
     else
         db.inserts(statementArray);
-
     if(logArray.length > 0)
         db.inserts(logArray);
 }
diff --git a/entity/CampaignParticipantChart_entity/recordcontainers/jdito/contentProcess.js b/entity/CampaignParticipantChart_entity/recordcontainers/jdito/contentProcess.js
index 3d6816c656..c0de8da56a 100644
--- a/entity/CampaignParticipantChart_entity/recordcontainers/jdito/contentProcess.js
+++ b/entity/CampaignParticipantChart_entity/recordcontainers/jdito/contentProcess.js
@@ -9,7 +9,10 @@ var campaignId = vars.get("$param.CampaignId_param");
 var sqlSelect = "select CAMPAIGNSTEPID, CAMPAIGNSTEP.NAME, count(distinct CAMPAIGNPARTICIPANTID), MAXPARTICIPANTS, count(distinct CAMPAIGNPARTICIPANTLOG.CAMPAIGNPARTICIPANT_ID) \n\
     from CAMPAIGNSTEP \n\
     left join CAMPAIGNPARTICIPANT on CAMPAIGNSTEPID = CAMPAIGNPARTICIPANT.CAMPAIGNSTEP_ID \n\
-    left join CAMPAIGNPARTICIPANTLOG on CAMPAIGNSTEPID = CAMPAIGNPARTICIPANTLOG.PREVIOUS_CAMPAIGNSTEP_ID";
+    left join CAMPAIGNPARTICIPANTLOG on CAMPAIGNSTEPID = CAMPAIGNPARTICIPANTLOG.PREVIOUS_CAMPAIGNSTEP_ID and CAMPAIGNPARTICIPANTLOG.CAMPAIGNSTEP_ID != CAMPAIGNPARTICIPANTLOG.PREVIOUS_CAMPAIGNSTEP_ID";
+     //Participated: counts all distinct CAMPAIGNPARTICIPANT_IDs from logs where the PREVIOUS_CAMPAIGNSTEP_ID is the id of the campaign step. Logs where
+     //the new CAMPAIGNSTEP_ID is the same as the PREVIOUS_CAMPAIGNSTEP_ID are not counted. This does not check if the SORTING is correct.
+
 var sqlGroupOrderBy = "group by CAMPAIGNSTEPID, CAMPAIGNSTEP.NAME, MAXPARTICIPANTS, CAMPAIGNSTEP.SORTING order by CAMPAIGNSTEP.SORTING";
 
 var sqlQuery = SqlCondition.begin()
diff --git a/process/Attribute_lib/process.js b/process/Attribute_lib/process.js
index 7d02717f1f..3236ea76d5 100644
--- a/process/Attribute_lib/process.js
+++ b/process/Attribute_lib/process.js
@@ -873,6 +873,8 @@ function AttributeUsageUtil () {}
  */
 AttributeUsageUtil.insertChildrenUsages = function (pAttributeId, pObjectType)
 {
+    if (!pAttributeId)
+        return;
     var table = "AB_ATTRIBUTEUSAGE";
     var columns = ["AB_ATTRIBUTEUSAGEID", "AB_ATTRIBUTE_ID", "OBJECT_TYPE"];
     var types = db.getColumnTypes(table, columns);
@@ -914,7 +916,7 @@ AttributeUsageUtil.insertChildrenUsages = function (pAttributeId, pObjectType)
  */
 AttributeUsageUtil.updateChildrenUsages = function (pAttributeId, pOldObjectType, pNewObjectType)
 {
-    if (!pNewObjectType)
+    if (!pNewObjectType || !pAttributeId)
         return;
     
     var table = "AB_ATTRIBUTEUSAGE";
-- 
GitLab