From aa30a97f829882a951f609f6d2d5fdbaa12f991b Mon Sep 17 00:00:00 2001
From: dsg03695 <dsg03695@N170.mshome.net>
Date: Tue, 27 Oct 2020 17:18:58 +0100
Subject: [PATCH] Ticket #1066033: first audit process done after assigning a
 new district contact to a organisation the employee needs to be notified.

---
 process/District_lib/process.js  | 191 ++++++++++++++++++-------------
 process/process_audit/process.js |   8 +-
 2 files changed, 117 insertions(+), 82 deletions(-)

diff --git a/process/District_lib/process.js b/process/District_lib/process.js
index 8cf6f65ce5..9dab569dad 100644
--- a/process/District_lib/process.js
+++ b/process/District_lib/process.js
@@ -1,3 +1,7 @@
+import("system.text");
+import("system.datetime");
+import("system.notification");
+import("system.translate");
 import("system.logging");
 import("Contact_lib");
 import("KeywordRegistry_basic");
@@ -28,9 +32,9 @@ function DistrictUtils () {}
 DistrictUtils.getDistrictName = function (pDistrictId)
 {
     var districtName = newSelect("DISTRICT_NAME")
-                            .from("DISTRICT")
-                            .where("DISTRICT.DISTRICTID", pDistrictId)
-                            .cell(true);    
+    .from("DISTRICT")
+    .where("DISTRICT.DISTRICTID", pDistrictId)
+    .cell(true);    
                             
     return(districtName)
 }
@@ -46,9 +50,9 @@ DistrictUtils.getDistrictName = function (pDistrictId)
 DistrictUtils.getDataFromDistrict = function (pDistrictId)
 {    
     var districtData = newSelect("DISTRICT_FILTER, PARENTDISTRICT_DISTRICTID")
-                            .from("DISTRICT")
-                            .where("DISTRICT.DISTRICTID", pDistrictId)
-                            .arrayRow();                   
+    .from("DISTRICT")
+    .where("DISTRICT.DISTRICTID", pDistrictId)
+    .arrayRow();                   
     return(districtData);
 }
 
@@ -63,10 +67,10 @@ DistrictUtils.getDataFromDistrict = function (pDistrictId)
  */
 DistrictUtils.getDataFromDistrictContact = function (pDistrictContactId)
 {
-    var arrData = newSelect("DISTRICT_ID, ADVISER_CONTACT_ID")
-                            .from("DISTRICTCONTACT")
-                            .where("DISTRICTCONTACT.DISTRICTCONTACTID", pDistrictContactId)
-                            .arrayRow();    
+    var arrData = newSelect("DISTRICT_ID, ADVISER_CONTACT_ID, CONTACT_ID")
+    .from("DISTRICTCONTACT")
+    .where("DISTRICTCONTACT.DISTRICTCONTACTID", pDistrictContactId)
+    .arrayRow();    
                             
     return(arrData);
 }
@@ -88,12 +92,12 @@ DistrictUtils.assignDistrictOnServer = function (pArrDistrictIds, pUser)
     if (pUser === undefined)
         pUser = EmployeeUtils.getCurrentUserId(); 
     var processConfig = process.createStartAsyncConfig()
-        .setName("assignDistrict_serverProcess")
-        .setLocalVariables({
-            arrDistrictIds : pArrDistrictIds.join(";"),
-            user : pUser || ""
-        })
-        .setUser(pUser)
+    .setName("assignDistrict_serverProcess")
+    .setLocalVariables({
+        arrDistrictIds : pArrDistrictIds.join(";"),
+        user : pUser || ""
+    })
+    .setUser(pUser)
     process.startAsync(processConfig);
 }
 
@@ -126,21 +130,21 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
     if (!pContactId)
     {
         arrExistingIds = new SqlBuilder()
-                .select("DISTRICTCONTACT.DISTRICTCONTACTID, DISTRICTCONTACT.CONTACT_ID, DISTRICTCONTACT.ADVISER_CONTACT_ID")
-                .from("DISTRICTCONTACT")    
-                .where("DISTRICTCONTACT.DISTRICT_ID", pDistrictId)
-                .and ("DISTRICTCONTACT.ORIGIN", $KeywordRegistry.districtOrigin$auto())
-                .and ("DISTRICTCONTACT.STATUS", $KeywordRegistry.contactStatus$inReview(), SqlBuilder.NOT_EQUAL())
-                .table();    
+        .select("DISTRICTCONTACT.DISTRICTCONTACTID, DISTRICTCONTACT.CONTACT_ID, DISTRICTCONTACT.ADVISER_CONTACT_ID")
+        .from("DISTRICTCONTACT")    
+        .where("DISTRICTCONTACT.DISTRICT_ID", pDistrictId)
+        .and ("DISTRICTCONTACT.ORIGIN", $KeywordRegistry.districtOrigin$auto())
+        .and ("DISTRICTCONTACT.STATUS", $KeywordRegistry.contactStatus$inReview(), SqlBuilder.NOT_EQUAL())
+        .table();    
     }
     
     //Einlesen aller Betreuer, die dem übergebenen Gebiet zugeordnet sind
     var arrResponsibleIds = new SqlBuilder()
-            .select("DISTRICTRESPONSIBLE.EMPLOYEE_CONTACT_ID, DISTRICTRESPONSIBLE.ADVISER_ROLE, " +
-                    "DISTRICTRESPONSIBLE.ADVISER_STATUS, DISTRICTRESPONSIBLE.VALID_FROM, DISTRICTRESPONSIBLE.VALID_UNTIL")
-            .from("DISTRICTRESPONSIBLE")    
-            .where("DISTRICTRESPONSIBLE.DISTRICT_ID", pDistrictId)
-            .table();
+    .select("DISTRICTRESPONSIBLE.EMPLOYEE_CONTACT_ID, DISTRICTRESPONSIBLE.ADVISER_ROLE, " +
+        "DISTRICTRESPONSIBLE.ADVISER_STATUS, DISTRICTRESPONSIBLE.VALID_FROM, DISTRICTRESPONSIBLE.VALID_UNTIL")
+    .from("DISTRICTRESPONSIBLE")    
+    .where("DISTRICTRESPONSIBLE.DISTRICT_ID", pDistrictId)
+    .table();
 
     //Aufbereiten des im JSON-Format übergebenen Filters in eine SQL-Condition
     var appliedFilterCondition = JSON.parse(pAppliedFilter).filter;
@@ -150,35 +154,35 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
     logging.log("sqlCondition: " + sqlCondition)
     //Einlesen der Contact-IDs zu den Firmen, die gemäß Filter diesem Gebiet zugeordnet sind
     var arrOrgContactIds = new SqlBuilder()
-            .selectDistinct("CONTACT.CONTACTID")
-            .from("ORGANISATION")
-            .join("CONTACT", newWhere("ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID").and("CONTACT.PERSON_ID is null"))
-            .leftJoin("ADDRESS", "ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID")
-            .leftJoin("CLASSIFICATIONSTORAGE", "CLASSIFICATIONSTORAGE.OBJECT_ROWID = CONTACT.CONTACTID")
-            .whereIfSet(sqlCondition)
-            .andIfSet("CONTACT.CONTACTID", pContactId)
-            .arrayColumn();   
+    .selectDistinct("CONTACT.CONTACTID")
+    .from("ORGANISATION")
+    .join("CONTACT", newWhere("ORGANISATION.ORGANISATIONID = CONTACT.ORGANISATION_ID").and("CONTACT.PERSON_ID is null"))
+    .leftJoin("ADDRESS", "ADDRESS.ADDRESSID = CONTACT.ADDRESS_ID")
+    .leftJoin("CLASSIFICATIONSTORAGE", "CLASSIFICATIONSTORAGE.OBJECT_ROWID = CONTACT.CONTACTID")
+    .whereIfSet(sqlCondition)
+    .andIfSet("CONTACT.CONTACTID", pContactId)
+    .arrayColumn();   
     logging.log("arrOrgContactIds.length: " + arrOrgContactIds.length)
     var insertArray = [];
     var colsInsert = [
-            "DISTRICTCONTACTID",
-            "CONTACT_ID",
-            "DISTRICT_ID",
-            "ADVISER_CONTACT_ID",
-            "ADVISER_ROLE",
-            "STATUS",
-            "VALID_FROM", 
-            "VALID_UNTIL",
-            "ORIGIN",
-            "USER_NEW",
-            "DATE_NEW"
-        ];
+    "DISTRICTCONTACTID",
+    "CONTACT_ID",
+    "DISTRICT_ID",
+    "ADVISER_CONTACT_ID",
+    "ADVISER_ROLE",
+    "STATUS",
+    "VALID_FROM", 
+    "VALID_UNTIL",
+    "ORIGIN",
+    "USER_NEW",
+    "DATE_NEW"
+    ];
     var updateArray = [];
     var colsUpdate = [
-            "STATUS",
-            "USER_EDIT",
-            "DATE_EDIT"
-        ];        
+    "STATUS",
+    "USER_EDIT",
+    "DATE_EDIT"
+    ];        
 
     //Anlegen der Firmen-Betreuer-Datensätze in der Tabelle DISTRICTCONTACT
     for (var i=0; i<arrOrgContactIds.length; i++)
@@ -207,17 +211,17 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
                     arrResponsibleIds[j][2] = $KeywordRegistry.contactStatus$active();
                 
                 var valsInsert = [
-                    util.getNewUUID(),
-                    arrOrgContactIds[i],
-                    pDistrictId,
-                    arrResponsibleIds[j][0],
-                    arrResponsibleIds[j][1],
-                    arrResponsibleIds[j][2],
-                    arrResponsibleIds[j][3],
-                    arrResponsibleIds[j][4],
-                    $KeywordRegistry.districtOrigin$auto(),
-                    vars.get("$sys.user"),
-                    vars.get("$sys.date")
+                util.getNewUUID(),
+                arrOrgContactIds[i],
+                pDistrictId,
+                arrResponsibleIds[j][0],
+                arrResponsibleIds[j][1],
+                arrResponsibleIds[j][2],
+                arrResponsibleIds[j][3],
+                arrResponsibleIds[j][4],
+                $KeywordRegistry.districtOrigin$auto(),
+                vars.get("$sys.user"),
+                vars.get("$sys.date")
                 ];                
                 insertArray.push(["DISTRICTCONTACT", colsInsert, null, valsInsert]);
             }            
@@ -233,9 +237,9 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
         for (var l=0; l<arrExistingIds.length; l++)
         {
             var valsUpdate = [
-                $KeywordRegistry.contactStatus$inReview(),
-                vars.get("$sys.user"),
-                vars.get("$sys.date")
+            $KeywordRegistry.contactStatus$inReview(),
+            vars.get("$sys.user"),
+            vars.get("$sys.date")
             ];            
             var condition = newWhere("DISTRICTCONTACT.DISTRICTCONTACTID", arrExistingIds[l][0]);
             updateArray.push(["DISTRICTCONTACT", colsUpdate, null, valsUpdate, condition.build()]);
@@ -267,12 +271,13 @@ DistrictUtils.getAppliedFilter = function (pDistrictFilter, pParentDistrict_Dist
     var parentFilter = DistrictUtils.getParentFilter(pParentDistrict_DistrictId);
 
     var appliedFilter = {
-            entity: "Organisation_entity", 
-            filter: {
-                type: "group", 
-                operator: "AND", 
-                childs: []
-    }};
+        entity: "Organisation_entity", 
+        filter: {
+            type: "group", 
+            operator: "AND", 
+            childs: []
+        }
+    };
 
     if (parentFilter)
     {
@@ -343,9 +348,9 @@ DistrictUtils._getParentFilter = function (pParentDistrict_DistrictId, pPrevious
     {
         var parentDistrictFilter, grandParentDistrict;
         [parentDistrictFilter, grandParentDistrict] = newSelect(["DISTRICT.DISTRICT_FILTER", "DISTRICT.PARENTDISTRICT_DISTRICTID"])
-                                                            .from("DISTRICT")
-                                                            .where("DISTRICT.DISTRICTID", pParentDistrict_DistrictId)
-                                                            .arrayRow();
+        .from("DISTRICT")
+        .where("DISTRICT.DISTRICTID", pParentDistrict_DistrictId)
+        .arrayRow();
 
         parentDistrictFilter = JSON.parse(parentDistrictFilter);
 
@@ -381,9 +386,9 @@ DistrictUtils.assignOrganisationToDistrict = function (pOrganisationId, pUserLog
     
     //alle Filter ermitteln, die auf auto-assignment gleich true gesetzt sind
     var allAutoDistricts = newSelect("DISTRICTID")
-                            .from("DISTRICT")
-                            .where("DISTRICT.DISTRICT_AUTOADD", 1)
-                            .arrayColumn();          
+    .from("DISTRICT")
+    .where("DISTRICT.DISTRICT_AUTOADD", 1)
+    .arrayColumn();          
                             
     //TODO: pUserLogin noch bis zum Schreiben von date_new durchschleusen
     logging.log("pUerLogin: " + pUserLogin)
@@ -404,8 +409,32 @@ DistrictUtils.assignOrganisationToDistrict = function (pOrganisationId, pUserLog
     }
 
 
-    //Variante 2: Aufruf des Server-Prozessen mit allen Gebieten mit Auto-Zuordnung gleich true
-    //Wirft den gleichen Fehler wie Variante 1
-    //TODO: Aus dem pUserLogin noch die Employee-ID ermitteln
-    //DistrictUtils.assignDistrictOnServer(allAutoDistricts, pUserLogin);
+//Variante 2: Aufruf des Server-Prozessen mit allen Gebieten mit Auto-Zuordnung gleich true
+//Wirft den gleichen Fehler wie Variante 1
+//TODO: Aus dem pUserLogin noch die Employee-ID ermitteln
+//DistrictUtils.assignDistrictOnServer(allAutoDistricts, pUserLogin);
+}
+
+/**
+ * This function will notificate the district contact that he/she was assigned
+ * 
+ * @param {String} pDistrictContactId           <p>
+ *                                              DistrictContact-ID of the new Dataset<br>                                                                                   
+ * @return <none>                               <p>
+ */
+DistrictUtils.notificateNewDistrictContact = function(pDistrictContactId){
+    logging.log(pDistrictContactId);
+    message = translate.text("Assignment to a new district/company!");
+
+    //[0]: DISTRICT_ID, [1]: ADVISER_CONTACT_ID, [2]: CONTACT_ID
+    var arrayDistrictContact = DistrictUtils.getDataFromDistrictContact(pDistrictContactId);
+    logging.log(arrayDistrictContact.length);
+    var districtName = DistrictUtils.getDistrictName(arrayDistrictContact[0]);
+    logging.log(districtName);
+    var organisationName = ContactUtils.getFullTitleByContactId(arrayDistrictContact[2]);
+    logging.log(arrayDistrictContact + districtName + organisationName);
+    // Benachrichtigung am Ende der Zuordnungen     
+    description = translate.withArguments("You were assigned as new district contact to district %0 and organisation %1.",
+        [districtName, organisationName])
+    notification.addNotification(util.getNewUUID(), text.encodeMS(["Organisation", arrayDistrictContact[2]]), null, null, "DistrictAssigned", notification.PRIO_NORMAL, 2, notification.STATE_UNSEEN, [user, arrayDistrictContact[1]], message, description);    
 }
\ No newline at end of file
diff --git a/process/process_audit/process.js b/process/process_audit/process.js
index b91b948b6d..c4b318d0b3 100644
--- a/process/process_audit/process.js
+++ b/process/process_audit/process.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("District_lib");
 import("Loghistory_lib");
 import("system.vars");
@@ -21,4 +22,9 @@ if (sqlAction != 'X')
 //if (sqlAction == 'I' && tableName == 'ORGANISATION')
 //{   
 //    DistrictUtils.assignOrganisationToDistrict(id, userLogin);
-//}
\ No newline at end of file
+//}
+
+if(sqlAction == 'I' && tableName == 'DISTRICTCONTACT'){
+    logging.log("audit");
+    DistrictUtils.notificateNewDistrictContact(id);
+}
\ No newline at end of file
-- 
GitLab