From a6e86218cdd11e558bd84f94fc0ee5842bf95960 Mon Sep 17 00:00:00 2001
From: "d.buechler" <d.buechler@adito.de>
Date: Wed, 3 Jul 2019 16:57:23 +0200
Subject: [PATCH] =?UTF-8?q?Condition=20+=20Selektion=20Serienaktion=20funk?=
 =?UTF-8?q?tioniert=20nun=20auch=20bei=20ORGANISATION=20Um=20eine=20Firma?=
 =?UTF-8?q?=20ermitteln=20zu=20k=C3=B6nnen=20muss=20die=20CONTACT=20Tabell?=
 =?UTF-8?q?e=20per=20right=20join=20hinzugef=C3=BCgt=20werden.=20Dies=20is?=
 =?UTF-8?q?t=20ben=C3=B6tigt,=20weil=20eine=20Firma=20keine=20PersonenId?=
 =?UTF-8?q?=20hat,=20die=20Contacts=20aber=20=C3=BCber=20die=20PersonId=20?=
 =?UTF-8?q?gejoint=20werden.=20Bei=20normalen=20Datens=C3=A4tzen=20reicht?=
 =?UTF-8?q?=20normales=20joinen.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 .../isoperationvalid/valueProcess.js          |  9 ++++-
 .../recordcontainers/jdito/onInsert.js        |  8 +++-
 process/Campaign_lib/process.js               | 40 +++++++++++++------
 3 files changed, 41 insertions(+), 16 deletions(-)

diff --git a/entity/CampaignAddParticipants_entity/entityfields/isoperationvalid/valueProcess.js b/entity/CampaignAddParticipants_entity/entityfields/isoperationvalid/valueProcess.js
index 471416a0f86..982aa7d2240 100644
--- a/entity/CampaignAddParticipants_entity/entityfields/isoperationvalid/valueProcess.js
+++ b/entity/CampaignAddParticipants_entity/entityfields/isoperationvalid/valueProcess.js
@@ -69,7 +69,12 @@ if(selectedCampaignId != '')
     {
         participantCondition = JSON.parse(vars.getString("$param.campaignParticipantsCondition_param")).condition;
         logging.log("im isOperationValid -> ");
-        countParticipantsToAdd = CampaignUtils.GetContactCountByCondition(participantCondition, isUpdate);
+        
+        let useRightJoinToGetOrgs = "false";
+        if(targetTable == "ORGANISATION")
+            useRightJoinToGetOrgs = "true";
+        
+        countParticipantsToAdd = CampaignUtils.GetContactCountByCondition(participantCondition, isUpdate, useRightJoinToGetOrgs);
                 logging.log("countParticipantsToAdd -> " + countParticipantsToAdd);
 
         /*
@@ -84,7 +89,7 @@ if(selectedCampaignId != '')
         else
             whereCondition = "CAMPAIGNPARTICIPANT.CAMPAIGN_ID = '" + selectedCampaignId + "'";
 
-        countParticipantsAlreadyInCampaign = CampaignUtils.GetParticipantsAlreadyAddedCountByCondition(whereCondition, participantCondition);
+        countParticipantsAlreadyInCampaign = CampaignUtils.GetParticipantsAlreadyAddedCountByCondition(whereCondition, participantCondition, useRightJoinToGetOrgs);
         //CAMPAIGNPARTICIPANT.CAMPAIGN_ID = '" + pCampaignId + "'"
     }
     countValidParticipantsToAdd = countParticipantsToAdd - countParticipantsAlreadyInCampaign;
diff --git a/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js b/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js
index e571d933bc0..6ae8f16c2a9 100644
--- a/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js
+++ b/entity/CampaignAddParticipants_entity/recordcontainers/jdito/onInsert.js
@@ -38,6 +38,10 @@ function _handleCondition(pCampaignId, pTargetTableName, pCondition)
 {
     let contactIdsToHandle = [];
     
+    let useRightJoinToGetOrgs = "false";
+    if(pTargetTableName == "ORGANISATION")
+        useRightJoinToGetOrgs = "true";
+        
     /*
      * If it's an update of participants, get the participants defined by the condition in the selected campaign
      * Because they already are participants, no restrictions apply to the affected IDs.
@@ -47,10 +51,10 @@ function _handleCondition(pCampaignId, pTargetTableName, pCondition)
      */
     if(isUpdate == "true")
     {
-        contactIdsToHandle = CampaignUtils.GetContactIdsInCampaignByCondition(pCampaignId, pCondition)
+        contactIdsToHandle = CampaignUtils.GetContactIdsInCampaignByCondition(pCampaignId, pCondition, useRightJoinToGetOrgs)
     }
     else
-        contactIdsToHandle = CampaignUtils.GetContactIdsNotInCampaignByCondition(pCampaignId, pCondition);
+        contactIdsToHandle = CampaignUtils.GetContactIdsNotInCampaignByCondition(pCampaignId, pCondition, useRightJoinToGetOrgs);
     
     logging.log("contactIdsToHandle -> " + contactIdsToHandle);
     _handleRowIds(contactIdsToHandle);
diff --git a/process/Campaign_lib/process.js b/process/Campaign_lib/process.js
index e16e5b948a2..d055712e833 100644
--- a/process/Campaign_lib/process.js
+++ b/process/Campaign_lib/process.js
@@ -335,10 +335,14 @@ CampaignUtils.GetParticipantsAlreadyAddedCountByRowId = function(pWhereCondition
     return db.cell(query)
 }
 
-CampaignUtils.GetParticipantsAlreadyAddedCountByCondition = function(pWhereCondition, pCondition)
+CampaignUtils.GetParticipantsAlreadyAddedCountByCondition = function(pWhereCondition, pCondition, pRightJoinContacts)
 {
     let query = "select count(*) from PERSON"
-    + " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
+    
+    if(pRightJoinContacts == "true")
+        query += " right"
+    
+    query += " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
     + " join ORGANISATION on (ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID)"
     + " left join ADDRESS on (ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID)"
     + " left join CAMPAIGNPARTICIPANT on CAMPAIGNPARTICIPANT.CONTACT_ID = CONTACT.CONTACTID"
@@ -365,10 +369,14 @@ CampaignUtils.GetContactIdsNotInCampaignByRowIds = function(pCampaignId, pPartic
     return db.array(db.COLUMN, query)
 }
 
-CampaignUtils.GetContactIdsNotInCampaignByCondition = function(pCampaignId, pCondition)
+CampaignUtils.GetContactIdsNotInCampaignByCondition = function(pCampaignId, pCondition, pRightJoinContacts)
 {
     let query = "select CONTACT.CONTACTID from PERSON"
-    + " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
+    
+    if(pRightJoinContacts == "true")
+        query += " right"
+    
+    query += " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
     + " join ORGANISATION on (ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID)"
     + " left join ADDRESS on (ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID)"
     + " where CONTACT.CONTACTID not in"
@@ -377,29 +385,37 @@ CampaignUtils.GetContactIdsNotInCampaignByCondition = function(pCampaignId, pCon
     
     if(pCondition != "")
         query += " and " + pCondition;
-    
+    logging.log("GetContactIdsNotInCampaignByCondition -> " + query);
     return db.array(db.COLUMN, query);
 }
 
-CampaignUtils.GetContactIdsInCampaignByCondition = function(pCampaignId, pCondition)
+CampaignUtils.GetContactIdsInCampaignByCondition = function(pCampaignId, pCondition, pRightJoinContacts)
 {
     let query = "select CONTACT.CONTACTID from PERSON"
-    + " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
+    
+    if(pRightJoinContacts == "true")
+        query += " right"
+    
+    query += " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
     + " join ORGANISATION on (ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID)"
     + " left join ADDRESS on (ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID)"
     + " left join CAMPAIGNPARTICIPANT on CAMPAIGNPARTICIPANT.CONTACT_ID = CONTACT.CONTACTID"
     + " where " + pCondition
     + " and CAMPAIGNPARTICIPANT.CAMPAIGN_ID = '" + pCampaignId + "'";
-
+        logging.log("GetContactIdsInCampaignByCondition -> " + GetContactIdsInCampaignByCondition);
     return db.array(db.COLUMN, query);
 }
 
-CampaignUtils.GetContactCountByCondition = function(pCondition, pLookInCampaignOnly)
+CampaignUtils.GetContactCountByCondition = function(pCondition, pLookInCampaignOnly, pRightJoinContacts)
 {
     let query = "select count(*) from PERSON"
-                + " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
-                + " join ORGANISATION on (ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID)"
-                + " left join ADDRESS on (ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID)"
+    
+    if(pRightJoinContacts == "true")
+        query += " right"
+    
+    query += " join CONTACT on (CONTACT.PERSON_ID = PERSON.PERSONID)"
+    query += " join ORGANISATION on (ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID)"
+    query += " left join ADDRESS on (ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID)"
                 
     if(pLookInCampaignOnly == "true")
         query += " left join CAMPAIGNPARTICIPANT on CAMPAIGNPARTICIPANT.CONTACT_ID = CONTACT.CONTACTID";
-- 
GitLab