diff --git a/entity/Person_entity/recordcontainers/db/onDBUpdate.js b/entity/Person_entity/recordcontainers/db/onDBUpdate.js index c1aaada5945950cf44c87edc3e7688fba2a518ad..4e3598b753a7b4f0a1de96f342e9e9ecf9e992bf 100644 --- a/entity/Person_entity/recordcontainers/db/onDBUpdate.js +++ b/entity/Person_entity/recordcontainers/db/onDBUpdate.js @@ -2,6 +2,7 @@ import("system.vars"); import("Person_lib"); import("Communication_lib"); import("Entity_lib"); +import("StandardObject_lib"); // TODO: this is a workaround for missing possibility to react on changes of fields not connected to record Contqainer #1030023 FieldChanges.assimilateChangeAndDispose("$field.IMAGE", function(state, value){ @@ -20,3 +21,6 @@ FieldChanges.assimilateChangeAndDispose("$field.STANDARD_EMAIL_COMMUNICATION", f FieldChanges.assimilateChangeAndDispose("$field.STANDARD_PHONE_COMMUNICATION", function(state, value){ CommUtil.setStandardPhone(uid, value); }); + +new StandardObject("Address", vars.get("$field.ADDRESS_ID"), "Person", vars.get("$field.CONTACTID")) + .onPersonUpdate(vars.get("$field.ORGANISATION_ID")); \ No newline at end of file diff --git a/process/StandardObject_lib/process.js b/process/StandardObject_lib/process.js index 28c728cd7666a4797f19fdaffebe90ba68e032d6..52d5ba43de3bab4e62f9dd882bb9e4f165da0012 100644 --- a/process/StandardObject_lib/process.js +++ b/process/StandardObject_lib/process.js @@ -2,6 +2,7 @@ import("system.logging"); import("system.db"); import("Keyword_lib"); import("KeywordRegistry_basic"); +import("Contact_lib"); function StandardObject (pObjectType, pObjectID, pScopeType, pScopeID) { if (!this._isValidType("object", pObjectType)) @@ -114,9 +115,9 @@ StandardObject.prototype.onObjectInsert = function () { this._assertScopeIdNotNull(); if (this.objectType === StandardObject.CONST_OBJECT_ADDRESS()) { - this._onAddressInsert() + this._onAddressInsert(); } else if (this.objectType === StandardObject.CONST_OBJECT_COMMUNICATION) { - this._onCommunicationInsert() + this._onCommunicationInsert(); } } @@ -130,7 +131,21 @@ StandardObject.prototype._onAddressInsert = function () { this._assertObjectType(StandardObject.CONST_OBJECT_ADDRESS()); if (!this._hasContactStandardAddress(this.scopeID)) { - this._setContactStandardAddress(this.objectID, this.scopeID) + this._setContactStandardAddress(this.objectID, this.scopeID); + } +} + +StandardObject.prototype.onPersonUpdate = function (pOrganisationID) { + // Assert + this._assertScopeType(StandardObject.CONST_SCOPE_PERSON()); + + var isOrganisationAddress = this._isOrganisationAddress(this.scopeID); + + if (isOrganisationAddress) { + // Update to new address of org + var addressID = this._getCompanyStandardAddress(pOrganisationID); + + this._setContactStandardAddress(addressID, this.scopeID); } } @@ -199,7 +214,7 @@ StandardObject.prototype._setContactStandardAddress = function (pAddressID, pCon ["ADDRESS_ID"], db.getColumnTypes("CONTACT", ["ADDRESS_ID"]), [pAddressID], - "CONTACTID = '" + pContactID + "'") + "CONTACTID = '" + pContactID + "'"); } /** @@ -231,7 +246,7 @@ StandardObject.prototype._hasStandardCommunicationByMedium = function (pContactI var dbResult = db.array(db.COLUMN, "select CHAR_VALUE from COMMUNICATION" + " left join AB_KEYWORD_ENTRY on KEYID = MEDIUM_ID" + " left join AB_KEYWORD_ATTRIBUTERELATION on AB_KEYWORD_ENTRY_ID = AB_KEYWORD_ENTRYID and AB_KEYWORD_ATTRIBUTE_ID = '7250ff28-1d48-41cc-bb36-8c33ace341bb'" - + " where STANDARD = 1 and CONTACT_ID = '" + pContactID + "'") + + " where STANDARD = 1 and CONTACT_ID = '" + pContactID + "'"); return dbResult.indexOf(pMediumCategory) !== -1; } @@ -265,7 +280,7 @@ StandardObject.prototype._setStandardCommunication = function (pCommunicationID, ["STANDARD"], db.getColumnTypes("COMMUNICATION", ["STANDARD"]), [pValue], - "COMMUNICATIONID = '" + pCommunicationID + "'") + "COMMUNICATIONID = '" + pCommunicationID + "'"); } /** @@ -275,5 +290,16 @@ StandardObject.prototype._setStandardCommunication = function (pCommunicationID, * @return The contact ID. */ StandardObject.prototype._getContactIdByCommunication = function (pCommunicationID) { - return db.cell("select CONTACT_ID from COMMUNICATION where COMMUNICATIONID = '" + pCommunicationID + "'") + return db.cell("select CONTACT_ID from COMMUNICATION where COMMUNICATIONID = '" + pCommunicationID + "'"); +} + +StandardObject.prototype._isOrganisationAddress = function (pAddressID) { + var contactID = db.cell("select CONTACTID from CONTACT where ADDRESS_ID = '" + pAddressID + "'"); + + if (contactID === "") + return false; + + var contactType = ContactUtils.getContactTypeByContactId(contactID); + + return contactType === 1; } \ No newline at end of file