From 625cab9a1ef484bbb1667520a46928904335d824 Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Fri, 6 Dec 2019 10:33:29 +0100
Subject: [PATCH] IncomingCall: Fix error when the incoming phone number is
 stored in an organisation-contact

---
 process/IncomingCallExecutor_lib/process.js |  9 +++----
 process/Util_lib/process.js                 | 28 +++++++++++++++++++++
 process/ctiServerEvents/process.js          |  7 +++---
 3 files changed, 36 insertions(+), 8 deletions(-)

diff --git a/process/IncomingCallExecutor_lib/process.js b/process/IncomingCallExecutor_lib/process.js
index 1a606d9a62..dd06e535e9 100644
--- a/process/IncomingCallExecutor_lib/process.js
+++ b/process/IncomingCallExecutor_lib/process.js
@@ -233,7 +233,7 @@ IncomingCallExecutor._getContactsFromNumber = function(pNumber, pContactIds)
         var patternConfig = indexsearch.createPatternConfig();
         var searchTerm = indexsearch.createTerm(pNumber).setIndexField("phone");
         patternConfig.plus(searchTerm);
-        var pattern = indexsearch.buildPatternString(patternConfig);
+        var pattern = indexsearch.buildPattern(patternConfig);
         var indexQuery = indexsearch.createIndexQuery().setPattern(pattern)
                                                        .addIndexGroups("Person", "Organisation")
                                                        .addResultIndexFields([indexsearch.FIELD_ID]);
@@ -258,9 +258,8 @@ IncomingCallExecutor._getContactsFromNumber = function(pNumber, pContactIds)
                         .from("CONTACT")
                         .join("ORGANISATION", "ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID")
                         .leftJoin("PERSON", "CONTACT.PERSON_ID = PERSON.PERSONID")
-                        .join("COMMUNICATION", "COMMUNICATION.CONTACT_ID = CONTACT.CONTACTID")
                         .where("CONTACT.STATUS", $KeywordRegistry.contactStatus$active())
-                        .and("COMMUNICATION.CONTACT_ID", pContactIds, SqlBuilder.IN())
+                        .and("CONTACT.CONTACTID", pContactIds, SqlBuilder.IN())
                         .table();
     
     //map to the result how the entities-methods would return it to have less effort later when the mentioned ticket is done
@@ -268,10 +267,10 @@ IncomingCallExecutor._getContactsFromNumber = function(pNumber, pContactIds)
         return {
             CONTACTID: e[0],
             ORGANISATION_ID: e[1], 
-            ORGANISATION_NAME: e[2],
+            ORGANISATION_NAME: e[2].trim(),
             PERSON_ID: e[3],
             LANGUAGE: e[4],
-            PERSON_FULL_NAME: e.slice(5).join(" ")
+            PERSON_FULL_NAME: e.slice(5).join(" ").trim() //quick solution until the ticket above (#1047680) is solved
         };
     });
 };
diff --git a/process/Util_lib/process.js b/process/Util_lib/process.js
index 28578e37a0..eaa63736e5 100644
--- a/process/Util_lib/process.js
+++ b/process/Util_lib/process.js
@@ -31,6 +31,13 @@ function StringUtils(){}
  *
  * @return {String} concatenated string; if all elements are empty an emtpy string is returned
  * 
+ * @example
+ * 
+ * var necessaryColumns = [];
+ * //...
+ * //code that pushes into the necessaryColumns variable here
+ * //...
+ * var title = StringUtils.concat(", ", necessaryColumns);
  */
 StringUtils.concat = function(pSeparator, pElements)
 {
@@ -40,6 +47,27 @@ StringUtils.concat = function(pSeparator, pElements)
    return res;
 };
 
+/**
+ * concats severel elements by a separator; the separator is only applied if a element is not null and not an empty string "";
+ * You can pass >=2 elements to the function and they will be all concated
+ *
+ * @param {String} pSeparator specifies how the not empty elements shall be concatenated
+ * @param {String} pElement1 first element
+ * @param {String} pElementN more elements
+ *
+ * @return {String} concatenated string; if all elements are empty an emtpy string is returned
+ * 
+ * @example
+ * var title = StringUtils.concatArgs(", ", fullPersonContactName, organisationName, additionalInfo);
+ * 
+ */
+StringUtils.concatArgs = function(pSeparator, pElement1, pElementN)
+{
+    //arguments is not an actual array but an array-like variable so work around that to slice the separator away
+    var elements = Array.prototype.slice.call(arguments, 1);
+    return StringUtils.concat(pSeparator, elements);
+};
+
 
 /**
  * converts a string to a string of always 36 chars. Whitespaces are added at the end if needed.
diff --git a/process/ctiServerEvents/process.js b/process/ctiServerEvents/process.js
index accc00a76f..52cd254be8 100644
--- a/process/ctiServerEvents/process.js
+++ b/process/ctiServerEvents/process.js
@@ -1,3 +1,4 @@
+import("Util_lib");
 import("system.tools");
 import("system.vars");
 import("Date_lib");
@@ -41,7 +42,7 @@ var ringingHandlerFn = function()
             var title, desc;
             if (this.contactsCall.length > 0)
             {
-                var contactTitle = this.contactsCall[0].PERSON_FULL_NAME + ", " + this.contactsCall[0].ORGANISATION_NAME;
+                var contactTitle = StringUtils.concatArgs(", ", this.contactsCall[0].PERSON_FULL_NAME, this.contactsCall[0].ORGANISATION_NAME);
                 title = translate.withArguments("Call from %0", [contactTitle], targetLocale);
                 desc = translate.withArguments("Incoming call from %0 (%1) to %2", [contactTitle, this.getFormattedCallAddress(), this.getFormattedLocalAddress()], targetLocale);
             }
@@ -120,7 +121,7 @@ var disconnectingHandlerFn = function()
             }
             else
             {
-                var contactTitleMissed = this.contactsCall[0].PERSON_FULL_NAME + ", " + this.contactsCall[0].ORGANISATION_NAME;
+                var contactTitleMissed = StringUtils.concatArgs(", ", this.contactsCall[0].PERSON_FULL_NAME, this.contactsCall[0].ORGANISATION_NAME);
                 title = translate.withArguments("Call from %0", [contactTitleMissed], targetLocale);
                 desc = translate.withArguments("Missed call from %0 (%1) to %2", [this.getFormattedCallAddress(), contactTitleMissed, this.getFormattedLocalAddress()], targetLocale);
             }
@@ -135,7 +136,7 @@ var disconnectingHandlerFn = function()
             }
             else
             {
-                var contactTitleAccepted = this.contactsCall[0].PERSON_FULL_NAME + ", " + this.contactsCall[0].ORGANISATION_NAME;
+                var contactTitleAccepted = StringUtils.concatArgs(", ", this.contactsCall[0].PERSON_FULL_NAME, this.contactsCall[0].ORGANISATION_NAME);
                 title = translate.withArguments("Call from %0", [contactTitleAccepted], targetLocale);
                 desc = translate.withArguments("Accepted call from %0 (%1) to %2, duration: %3", [this.getFormattedCallAddress(), contactTitleAccepted, this.getFormattedLocalAddress(), talkDuration], targetLocale);
             }
-- 
GitLab