From 9b9c81d3286d581aa8365bb9e33c3c9a4d232bf9 Mon Sep 17 00:00:00 2001
From: "S.Listl" <s.listl@adito.de>
Date: Wed, 30 Oct 2019 13:17:09 +0100
Subject: [PATCH] Person: set person address as standard when a new person is
 inserted

---
 entity/Address_entity/Address_entity.aod         |  4 ++++
 .../recordcontainers/db/onDBInsert.js            |  5 +++--
 entity/Person_entity/Person_entity.aod           |  4 ++++
 .../replacestandardaddress_param/valueProcess.js |  8 ++++++++
 .../recordcontainers/db/onDBInsert.js            |  4 +++-
 process/StandardObject_lib/process.js            | 16 +++++++++-------
 6 files changed, 31 insertions(+), 10 deletions(-)
 create mode 100644 entity/Person_entity/entityfields/persaddresses/children/replacestandardaddress_param/valueProcess.js

diff --git a/entity/Address_entity/Address_entity.aod b/entity/Address_entity/Address_entity.aod
index 133262e806..871854a526 100644
--- a/entity/Address_entity/Address_entity.aod
+++ b/entity/Address_entity/Address_entity.aod
@@ -478,6 +478,10 @@
       <name>ContactIds_param</name>
       <expose v="true" />
     </entityParameter>
+    <entityParameter>
+      <name>ReplaceStandardAddress_param</name>
+      <expose v="true" />
+    </entityParameter>
   </entityFields>
   <recordContainers>
     <dbRecordContainer>
diff --git a/entity/Address_entity/recordcontainers/db/onDBInsert.js b/entity/Address_entity/recordcontainers/db/onDBInsert.js
index 05aad0bca1..26ad235d60 100644
--- a/entity/Address_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/Address_entity/recordcontainers/db/onDBInsert.js
@@ -1,3 +1,4 @@
+import("system.logging");
 import("KeywordRegistry_basic");
 import("DataPrivacy_lib");
 import("system.vars");
@@ -22,6 +23,6 @@ else if (typeParam === "organisation")
     scopeType = "Organisation"
 
 new StandardObject("Address", vars.get("$local.uid"), scopeType, rowdata["ADDRESS.CONTACT_ID"])
-    .onObjectInsert()
+    .onObjectInsert(vars.exists("$param.ReplaceStandardAddress_param") ? vars.get("$param.ReplaceStandardAddress_param") : undefined)
 
-DataPrivacyUtils.notifyNeedDataPrivacyUpdate(rowdata["ADDRESS.CONTACT_ID"], vars.get("$param.ShowDsgvoMessage_param"));
\ No newline at end of file
+DataPrivacyUtils.notifyNeedDataPrivacyUpdate(rowdata["ADDRESS.CONTACT_ID"], vars.get("$param.ShowDsgvoMessage_param"));
diff --git a/entity/Person_entity/Person_entity.aod b/entity/Person_entity/Person_entity.aod
index c8c9b4469f..4d104964f8 100644
--- a/entity/Person_entity/Person_entity.aod
+++ b/entity/Person_entity/Person_entity.aod
@@ -176,6 +176,10 @@ This field only calculates the Orgid from the ORGANISATION_CONTACTID to save it
           <name>ShowDsgvoMessage_param</name>
           <valueProcess>%aditoprj%/entity/Person_entity/entityfields/persaddresses/children/showdsgvomessage_param/valueProcess.js</valueProcess>
         </entityParameter>
+        <entityParameter>
+          <name>ReplaceStandardAddress_param</name>
+          <valueProcess>%aditoprj%/entity/Person_entity/entityfields/persaddresses/children/replacestandardaddress_param/valueProcess.js</valueProcess>
+        </entityParameter>
       </children>
     </entityConsumer>
     <entityConsumer>
diff --git a/entity/Person_entity/entityfields/persaddresses/children/replacestandardaddress_param/valueProcess.js b/entity/Person_entity/entityfields/persaddresses/children/replacestandardaddress_param/valueProcess.js
new file mode 100644
index 0000000000..41522d0615
--- /dev/null
+++ b/entity/Person_entity/entityfields/persaddresses/children/replacestandardaddress_param/valueProcess.js
@@ -0,0 +1,8 @@
+import("system.neon");
+import("system.vars");
+import("system.result");
+
+//this parameter allows the address entity to set a itself as the standard address
+//if the current standard address is the given id
+if (vars.get("$sys.recordstate") == neon.OPERATINGSTATE_NEW)
+    result.string(vars.get("$field.ADDRESS_ID"));
\ No newline at end of file
diff --git a/entity/Person_entity/recordcontainers/db/onDBInsert.js b/entity/Person_entity/recordcontainers/db/onDBInsert.js
index eb22217f25..5f069680c3 100644
--- a/entity/Person_entity/recordcontainers/db/onDBInsert.js
+++ b/entity/Person_entity/recordcontainers/db/onDBInsert.js
@@ -72,4 +72,6 @@ notification.addNotificationWith(notificationConfig);
 
 //notification.addNotification(util.getNewUUID(), null, null, null, "_____SYSTEM_NOTIFICATION_MESSAGE", notification.PRIO_NORMAL, 1, notification.STATE_UNSEEN, [EmployeeUtils.getCurrentUserId()], "message", "description");
 
-}
\ No newline at end of file
+}
+
+var persAdress
\ No newline at end of file
diff --git a/process/StandardObject_lib/process.js b/process/StandardObject_lib/process.js
index 5e8a368ff6..acd3d10fab 100644
--- a/process/StandardObject_lib/process.js
+++ b/process/StandardObject_lib/process.js
@@ -1,4 +1,5 @@
 import("Organisation_lib");
+import("Sql_lib");
 import("system.db");
 import("Keyword_lib");
 import("KeywordRegistry_basic");
@@ -111,12 +112,12 @@ StandardObject.prototype.onPersonValueChange = function (pSelectedOrganisationID
  * on a "random" basis: Which object gets first inserted will get the
  * place as standard.
  */
-StandardObject.prototype.onObjectInsert = function () {
+StandardObject.prototype.onObjectInsert = function (pIdToOverwrite) {
     this._assertObjectIdNotNull();
     this._assertScopeIdNotNull(); 
     
     if (this.objectType === StandardObject.CONST_OBJECT_ADDRESS()) {
-        this._onAddressInsert();
+        this._onAddressInsert(pIdToOverwrite);
     } else if (this.objectType === StandardObject.CONST_OBJECT_COMMUNICATION) {
         this._onCommunicationInsert();
     }
@@ -127,11 +128,11 @@ StandardObject.prototype.onObjectInsert = function () {
  * of the `Address` entity. This will set the standard address on the
  * contact if it's currently null.
  */
-StandardObject.prototype._onAddressInsert = function () {
+StandardObject.prototype._onAddressInsert = function (pIdToOverwrite) {
     // Assert
     this._assertObjectType(StandardObject.CONST_OBJECT_ADDRESS());
     
-    if (!this._hasContactStandardAddress(this.scopeID)) {
+    if (!this._hasContactStandardAddress(this.scopeID, pIdToOverwrite)) {
         this._setContactStandardAddress(this.objectID, this.scopeID);
     }
 }
@@ -190,15 +191,16 @@ StandardObject.prototype.onCommunicationUpdate = function (pMediumID) {
  * it's currently working on a `Address` object.
  * 
  * @param {String} pContactID Contact ID to check.
+ * @param {String} [pIdToIgnore] if this address ID is found, act as if it was empty
  * @return {Boolean} If the contact ID has standard address.
  */
-StandardObject.prototype._hasContactStandardAddress = function (pContactID) {
+StandardObject.prototype._hasContactStandardAddress = function (pContactID, pIdToIgnore) {
     this._assertObjectType(StandardObject.CONST_OBJECT_ADDRESS());
     
     var databaseResult = db.cell("select ADDRESS_ID from CONTACT"
         + " where CONTACTID = '" + pContactID + "'");
-    
-    return databaseResult !== "";
+        
+    return databaseResult !== "" && databaseResult !== pIdToIgnore;
 }
 
 /**
-- 
GitLab