From c632ecad34f4a086efa3776b0f14b65e45fa4323 Mon Sep 17 00:00:00 2001
From: "j.goderbauer" <j.goderbauer@adito.de>
Date: Tue, 17 Dec 2019 15:09:08 +0100
Subject: [PATCH] cti: improved recognition of the correct CTILOG-entry

---
 process/IncomingCallExecutor_lib/process.js | 15 +++++---
 process/ctiServerEvents/process.js          | 38 ++++++++++++++++-----
 2 files changed, 40 insertions(+), 13 deletions(-)

diff --git a/process/IncomingCallExecutor_lib/process.js b/process/IncomingCallExecutor_lib/process.js
index b9ef2495f69..7348d5e682d 100644
--- a/process/IncomingCallExecutor_lib/process.js
+++ b/process/IncomingCallExecutor_lib/process.js
@@ -55,8 +55,9 @@ function IncomingCallExecutor(pCallData)
     
     //key-value pairs of states-functions
     this._handlerFunctions = {};
+    
+    this.notificationContentId = null;
 }
-
 IncomingCallExecutor.prototype.toString = function()
 {
     return JSON.stringify(this, null, " ");
@@ -118,19 +119,25 @@ IncomingCallExecutor.prototype.logData = function()
     logging.log("contactsCall>>" + JSON.stringify(this.contactsCall));
 };
 
+/**
+ * helper function that will log different callData and collected data to standard-output
+ * The function does not specify any module, importance or else
+ * Needed for live-analytics of problems and errors.
+ * 
+ * @return undefined
+ */
 IncomingCallExecutor.prototype.getNotificationBaseConfig = function(pUserName)
 {
-    if (!pUserName)
+    if (!pUserName || !this.notificationContentId)
         return null;
     
     var notificationConfig = notification.createConfig()
     .addUserWithId(pUserName)
-    .contentId(this.callData.callId)//group all notifications of one call together
+    .contentId(this.notificationContentId)//group all notifications of one call together
     .notificationType("_____SYSTEM_NOTIFICATION_PHONECALL");
                                          
     return notificationConfig;
 };
-
 IncomingCallExecutor.prototype._setHandlerFn = function(pCallState, pFunction)
 {
     this._handlerFunctions[pCallState] = pFunction;
diff --git a/process/ctiServerEvents/process.js b/process/ctiServerEvents/process.js
index 52cd254be83..84f39dc864f 100644
--- a/process/ctiServerEvents/process.js
+++ b/process/ctiServerEvents/process.js
@@ -32,7 +32,7 @@ var ringingHandlerFn = function()
         vals.push(affectedContactId);
     }
     db.insertData("AB_CTILOG", cols, null, vals);
-
+    this.notificationContentId = vals[0];
 
     if (this.callData.isIncomingCall)
     {
@@ -75,10 +75,14 @@ var talkingHandlerFn = function()
 {
     var cols = ["DATE_EDIT", "ANSWERMODE"];
     var vals = [datetime.date(), $KeywordRegistry.callAnswerMode$accepted()];
-    
-    newWhereIfSet("AB_CTILOG.CALLID", this.callData.callId)
-        .updateData(true, "AB_CTILOG", cols, null, vals);
-        
+
+    var newestCtiLogId = _getNewestCtiLogId(this.callData.callId);
+    if (newestCtiLogId)
+    {
+        this.notificationContentId = newestCtiLogId;//currently not used but you never know
+        newWhereIfSet("AB_CTILOG.AB_CTILOGID", newestCtiLogId)
+                .updateData(true, "AB_CTILOG", cols, null, vals);
+    }
     //do not notify here since the user _should_ know when he accepts a call
 };
     
@@ -102,8 +106,13 @@ var disconnectingHandlerFn = function()
         cols.push("ANSWERMODE");
         vals.push($KeywordRegistry.callAnswerMode$missed());
     }
-    db.updateData("AB_CTILOG", cols, null, vals, sqlCond.build());
-
+    var newestCtiLogId = _getNewestCtiLogId(this.callData.callId);
+    if (newestCtiLogId)
+    {
+        this.notificationContentId = newestCtiLogId;
+        db.updateData("AB_CTILOG", cols, null, vals, newWhere("AB_CTILOG.AB_CTILOGID", newestCtiLogId).buildCondition());
+    }
+    
     //do not notify the user when the call is outgoing since the user knows when he/she does initiate a call
     if (!this.callData.isIncomingCall)
         return null;
@@ -164,6 +173,17 @@ var disconnectingHandlerFn = function()
     return null;
 };
 
+function _getNewestCtiLogId(pCallid)
+{
+    //get the newewst ctilogid for a given callid since the callid may not be unique in a telephon systems
+    //therefor sort descending by DATE_NEW and return that value
+    var ctiLogId = newSelect("AB_CTILOG.AB_CTILOGID")
+        .from("AB_CTILOG")
+        .where("AB_CTILOG.CALLID", pCallid)
+        .orderBy("AB_CTILOG.DATE_NEW desc")
+        .cell();
+    return ctiLogId;
+}
 //collect all data from the various call information
 var callData = {
      action: vars.get("$local.action")
@@ -183,7 +203,7 @@ var callData = {
 
 var ic = new IncomingCallExecutor(callData);
 //ic.logData();
-ic.setHandlerRinging(ringingHandlerFn);
+ic.logData();
 ic.setHandlerTalking(talkingHandlerFn);
 ic.setHandlerDisconnect(disconnectingHandlerFn);
-ic.execute();//actually doin' something
\ No newline at end of file
+ic.execute();//actually doin' something
-- 
GitLab