From 5bc6462d4456c054a13f001f5718d89794030a0d Mon Sep 17 00:00:00 2001
From: "p.neub" <p.neub@adito.de>
Date: Wed, 17 Nov 2021 14:14:26 +0100
Subject: [PATCH] [Projekt: xRM-Sales][TicketNr.: 2001202][Gebietszuordnung
 legt System lahm]

---
 .../_____LANGUAGE_de/_____LANGUAGE_de.aod     |   4 +
 process/District_lib/process.js               | 185 +++++++++---------
 process/process_audit/process.js              |  11 --
 3 files changed, 93 insertions(+), 107 deletions(-)

diff --git a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
index 1efadae4992..0b5de0b220f 100644
--- a/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
+++ b/language/_____LANGUAGE_de/_____LANGUAGE_de.aod
@@ -14663,6 +14663,10 @@ Bitte Datumseingabe prüfen</value>
       <key>Valid from (as %0)</key>
       <value>Gültig von (in %0)</value>
     </entry>
+    <entry>
+      <key>You were assigned as new district contact to district %0 and %1 organisation(s).</key>
+      <value>Sie wurden Distrikt %0 und %1 Organisation(en) als neuer Distriktkontakt zugewiesen.</value>
+    </entry>
   </keyValueMap>
   <font name="Dialog" style="0" size="11" />
 </language>
diff --git a/process/District_lib/process.js b/process/District_lib/process.js
index b3efdb43075..fb5c443eae8 100644
--- a/process/District_lib/process.js
+++ b/process/District_lib/process.js
@@ -119,8 +119,12 @@ DistrictUtils.assignDistrictOnServer = function (pArrDistrictIds, pUser, pContac
  */
 DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId)
 {
-    if (pContactId == undefined)
+    if(pContactId == undefined)
+    {
         pContactId = null;
+    }
+    
+    var districtName = DistrictUtils.getDistrictName(pDistrictId);
     
     var newAssigned = 0;
     var unchanged = 0;
@@ -158,24 +162,24 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
     }
 
     var colsInsert = [
-    "DISTRICTCONTACTID",
-    "CONTACT_ID",
-    "DISTRICT_ID",
-    "ADVISER_CONTACT_ID",
-    "ADVISER_ROLE",
-    "STATUS",
-    "VALID_FROM", 
-    "VALID_UNTIL",
-    "ORIGIN",
-    "VISITFREQUENCY",
-    "USER_NEW",
-    "DATE_NEW"
+        "DISTRICTCONTACTID",
+        "CONTACT_ID",
+        "DISTRICT_ID",
+        "ADVISER_CONTACT_ID",
+        "ADVISER_ROLE",
+        "STATUS",
+        "VALID_FROM", 
+        "VALID_UNTIL",
+        "ORIGIN",
+        "VISITFREQUENCY",
+        "USER_NEW",
+        "DATE_NEW"
     ];
 
     var colsUpdate = [
-    "STATUS",
-    "USER_EDIT",
-    "DATE_EDIT"
+        "STATUS",
+        "USER_EDIT",
+        "DATE_EDIT"
     ];  
     
     //create districtContact datasets
@@ -193,8 +197,8 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
         if(!pContactId)
         {
             arrExistingEntries = new SqlBuilder()
-            .select(["DISTRICTCONTACT.DISTRICT_ID", "DISTRICTCONTACT.CONTACT_ID", "DISTRICTCONTACT.ADVISER_CONTACT_ID", "DISTRICTCONTACT.DISTRICTCONTACTID"
-                    , "DISTRICTCONTACT.STATUS", "DISTRICTCONTACT.ADVISER_ROLE", "DISTRICTCONTACT.VISITFREQUENCY", "DISTRICTCONTACT.VALID_FROM", "DISTRICTCONTACT.VALID_UNTIL"])
+            .select(["DISTRICTCONTACT.DISTRICT_ID", "DISTRICTCONTACT.CONTACT_ID", "DISTRICTCONTACT.ADVISER_CONTACT_ID", "DISTRICTCONTACT.DISTRICTCONTACTID",
+                "DISTRICTCONTACT.STATUS", "DISTRICTCONTACT.ADVISER_ROLE", "DISTRICTCONTACT.VISITFREQUENCY", "DISTRICTCONTACT.VALID_FROM", "DISTRICTCONTACT.VALID_UNTIL"])
             .from("DISTRICTCONTACT")    
             .where("DISTRICTCONTACT.DISTRICT_ID", pDistrictId)
             .and("DISTRICTCONTACT.ADVISER_CONTACT_ID", resContactId)
@@ -203,14 +207,17 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
         }
         
         //create associative object so we don't have too loop trough an array to check wheter or not exisiting Entries already exist.
-        for (var aee = 0; aee < arrExistingEntries.length; aee++) {
+        for (var aee = 0; aee < arrExistingEntries.length; aee++)
+        {
             //existingEnriesObject[DISTRICT_ID + CONTACT_ID + ADVISER_CONTACT_ID] = [ID, STATUS]
-            existingEnriesObject[ arrExistingEntries[aee][0] + arrExistingEntries[aee][1] + arrExistingEntries[aee][2] ] = {"districtContactId": arrExistingEntries[aee][3]
-                                                                                                                            , "status": arrExistingEntries[aee][4]
-                                                                                                                            , "role": arrExistingEntries[aee][5]
-                                                                                                                            , "visitFrequency" : arrExistingEntries[aee][6]
-                                                                                                                            , "vlaidFrom" : arrExistingEntries[aee][7]
-                                                                                                                            , "vlaidUntil" : arrExistingEntries[aee][8]};
+            existingEnriesObject[ arrExistingEntries[aee][0] + arrExistingEntries[aee][1] + arrExistingEntries[aee][2] ] = {
+                "districtContactId": arrExistingEntries[aee][3],
+                "status": arrExistingEntries[aee][4],
+                "role": arrExistingEntries[aee][5],
+                "visitFrequency": arrExistingEntries[aee][6],
+                "vlaidFrom": arrExistingEntries[aee][7],
+                "vlaidUntil": arrExistingEntries[aee][8]
+            };
         }
         
         for (var i = 0; i < arrOrgContactIds.length; i++)
@@ -219,26 +226,27 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
             
             //insert new assignments
             var valsInsert = [
-            util.getNewUUID(),
-            arrOrgContactIds[i],
-            pDistrictId,
-            resContactId,
-            resRole,
-            newResStatus,
-            resValidFrom,
-            resValidUntil,
-            $KeywordRegistry.districtOrigin$auto(), //origin
-            resVisitFrequency,
-            vars.get("$sys.user"),
-            vars.get("$sys.date")
-            ];  
+                util.getNewUUID(),
+                arrOrgContactIds[i],
+                pDistrictId,
+                resContactId,
+                resRole,
+                newResStatus,
+                resValidFrom,
+                resValidUntil,
+                $KeywordRegistry.districtOrigin$auto(), //origin
+                resVisitFrequency,
+                vars.get("$sys.user"),
+                vars.get("$sys.date")
+            ];
 
             //association = DISTRICT_ID + CONTACT_ID + ADVISER_CONTACT_ID
             var existingEntryAssoz  = pDistrictId + arrOrgContactIds[i] + arrResponsibleIds[j][0];   
             var checkexistingEntry  = existingEnriesObject[existingEntryAssoz];     //existingEnriesObject[pDistrictId + arrOrgContactIds[i] + arrResponsibleIds[j][0]]
             
             //check associative Array
-            if (checkexistingEntry == undefined) {  //insert missing entries
+            if (checkexistingEntry == undefined) //insert missing entries
+            {
                 newAssigned++
                 insertArray.push(["DISTRICTCONTACT", colsInsert, null, valsInsert]);
             }
@@ -249,59 +257,73 @@ DistrictUtils.assignDistrict = function (pDistrictId, pAppliedFilter, pContactId
             {
                 updated++;
                 updateStatements.push(newWhere("DISTRICTCONTACT.DISTRICTCONTACTID", checkexistingEntry["districtContactId"]).buildUpdateStatement({
-                            "ADVISER_ROLE": resRole,
-                            "STATUS": newResStatus,
-                            "VALID_FROM": resValidFrom,
-                            "VALID_UNTIL": resValidUntil,
-                            "VISITFREQUENCY": resVisitFrequency,
-                            "USER_EDIT": vars.get("$sys.user"),
-                            "DATE_EDIT": vars.get("$sys.date")
-                        })
-                )
+                        "ADVISER_ROLE": resRole,
+                        "STATUS": newResStatus,
+                        "VALID_FROM": resValidFrom,
+                        "VALID_UNTIL": resValidUntil,
+                        "VISITFREQUENCY": resVisitFrequency,
+                        "USER_EDIT": vars.get("$sys.user"),
+                        "DATE_EDIT": vars.get("$sys.date")
+                    })
+                );
                 //delete from Object (we need to use association here) to sieve for invalid entries
                 delete existingEnriesObject[existingEntryAssoz];
             }
-            else if (checkexistingEntry != undefined){  //don't touch existing entries with valid status
+            else if (checkexistingEntry != undefined) //don't touch existing entries with valid status
+            {
                 unchanged++;
                 //delete from Object (we need to use association here) to sieve for invalid entries
                 delete existingEnriesObject[existingEntryAssoz];
-            }                
-        }        
+            }
+        }
 
         //invalid entries (Assignments, that where already exisitng, but don't fit into said district because of changes -> set status to "In review")
-        for (index in existingEnriesObject) {
+        for (index in existingEnriesObject)
+        {
             invalid++;
             existingEntryId = existingEnriesObject[index]["districtContactId"];
             
             updateStatements.push(newWhere("DISTRICTCONTACT.DISTRICTCONTACTID", existingEntryId).buildUpdateStatement({
-                        "STATUS": $KeywordRegistry.contactStatus$inReview(),
-                        "USER_EDIT": vars.get("$sys.user"),
-                        "DATE_EDIT": vars.get("$sys.date")
-                    })
-            )
+                    "STATUS": $KeywordRegistry.contactStatus$inReview(),
+                    "USER_EDIT": vars.get("$sys.user"),
+                    "DATE_EDIT": vars.get("$sys.date")
+                })
+            );
         }
-        
+
         var pageSize = 5000;//paging to prevent timeouts
-    
+
         while(insertArray.length > pageSize)
-        {  
+        {
             db.inserts(insertArray.splice(0, pageSize), "Data_alias", 6 * datetime.ONE_MINUTE);
-        } 
+        }
         db.inserts(insertArray);
         
         while(updateStatements.length > pageSize)
         { 
             db.execute(updateStatements.splice(0, pageSize), "Data_alias", 6 * datetime.ONE_MINUTE);
-        } 
+        }
         db.execute(updateStatements);
-    }
         
+        // notifications
+        var user = tools.getUserByAttribute(tools.CONTACTID, resContactId, tools.PROFILE_DEFAULT);
+        if(user)
+        {
+            var message = translate.text("Assignment to a new district/company!");
+            var description = translate.withArguments("You were assigned as new district contact to district %0 and %1 organisation(s).", [districtName, arrOrgContactIds.length]);
+            notification.addNotification(
+                util.getNewUUID(), text.encodeMS(["District", pDistrictId]), null, null, "DistrictAssigned",
+                notification.PRIO_NORMAL, 2, notification.STATE_UNSEEN, [user[tools.NAME]], message, description
+            );
+        }
+    }
+
     return {
-        newAssigned : newAssigned,
-        invalid : invalid,
-        unchanged : unchanged, 
+        newAssigned: newAssigned,
+        invalid: invalid,
+        unchanged: unchanged,
         updated: updated
-    };    
+    };
 }
 
 /**
@@ -446,32 +468,3 @@ DistrictUtils.setDistrictContactStatus = function (pArrIds, pStatus)
     }
     db.updates(updateArray);
 }
-
-
-/**
- * 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){
-    message = translate.text("Assignment to a new district/company!");
-
-    //[0]: DISTRICT_ID, [1]: ADVISER_CONTACT_ID, [2]: CONTACT_ID
-    var arrayDistrictContact = DistrictUtils.getDataFromDistrictContact(pDistrictContactId);
-    //get corresponding profile information of the systemalias
-    var user = tools.getUserByAttribute(tools.CONTACTID, arrayDistrictContact[1], tools.PROFILE_DEFAULT);
-    
-    //You can only send a notification, if the user is an employee
-    if (user)
-    {
-        var userName = user[tools.NAME];
-        var districtName = DistrictUtils.getDistrictName(arrayDistrictContact[0]);
-        var organisationName = ContactUtils.getFullTitleByContactId(arrayDistrictContact[2]);
-        // 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, [userName], message, description);    
-    }
-}
diff --git a/process/process_audit/process.js b/process/process_audit/process.js
index 234137317a0..ec9a0b75faf 100644
--- a/process/process_audit/process.js
+++ b/process/process_audit/process.js
@@ -25,17 +25,6 @@ if (sqlAction != 'X')
     }
 }
 
-if(sqlAction == 'I' && tableName == 'DISTRICTCONTACT'){
-    try
-    {
-        DistrictUtils.notificateNewDistrictContact(id);
-    }
-    catch(err)
-    {
-        logging.log("Error while district notification:" + err);
-    }
-}
-
 try
 {
     (new AuditUpdateSyncEntries(tableName, userLogin, columns, newvalues, oldvalues, timestamp, sqlAction, id)).execute();
-- 
GitLab