diff --git a/entity/Organisation_entity/entityfields/duplicateactions/children/integrateselectedintocurrentaction/onActionProcess.js b/entity/Organisation_entity/entityfields/duplicateactions/children/integrateselectedintocurrentaction/onActionProcess.js index 6bb5f4cde7193d3a4171b6d613cbfb0bfee097c8..5ac58579f1326724bf4e6340a751ee86b12850b4 100644 --- a/entity/Organisation_entity/entityfields/duplicateactions/children/integrateselectedintocurrentaction/onActionProcess.js +++ b/entity/Organisation_entity/entityfields/duplicateactions/children/integrateselectedintocurrentaction/onActionProcess.js @@ -1,5 +1,4 @@ import("Employee_lib"); -import("system.logging"); import("system.vars"); import("system.neon"); import("DuplicateScanner_lib"); diff --git a/process/DuplicateScanner_lib/process.js b/process/DuplicateScanner_lib/process.js index 7e057f268d3f41e78fb61588c65560fabaa9dac1..e26053edc6cb753291a842212cbfcef324124799 100644 --- a/process/DuplicateScanner_lib/process.js +++ b/process/DuplicateScanner_lib/process.js @@ -627,10 +627,12 @@ DuplicateScannerUtils.mergePerson = function(pSourceContactId, pTargetContactId) .from("CONTACT") .where("CONTACT.CONTACTID", pTargetContactId) .cell(); - + + _DuplicateScannerUtils._deleteUniqueAttributes(pSourceContactId, pTargetContactId); var isLinkedDataUpdated = _DuplicateScannerUtils._migrateLinkedContactData(pSourceContactId, pTargetContactId); var isParticipantsUpdated = _DuplicateScannerUtils._migrateParticipantsToNewContact("CAMPAIGNPARTICIPANT", "CONTACT_ID", "CAMPAIGN_ID", pSourceContactId, pTargetContactId); + _DuplicateScannerUtils._updateOtherContacts(pSourceContactId, sourcePersonId, targetPersonId); var deleteStatements = []; if (sourcePersonId != targetPersonId) @@ -672,10 +674,17 @@ DuplicateScannerUtils.mergeOrganisation = function(pSourceContactId, pTargetCont .from("CONTACT") .where("CONTACT.CONTACTID", pSourceContactId) .cell(); - + + var targetOrganisationId = newSelect("ORGANISATION_ID") + .from("CONTACT") + .where("CONTACT.CONTACTID", pTargetContactId) + .cell(); + + _DuplicateScannerUtils._deleteUniqueAttributes(pSourceContactId, pTargetContactId); _DuplicateScannerUtils._migrateLinkedContactData(pSourceContactId, pTargetContactId); _DuplicateScannerUtils._migrateParticipantsToNewContact("CAMPAIGNPARTICIPANT", "CONTACT_ID", "CAMPAIGN_ID", pSourceContactId, pTargetContactId); + _DuplicateScannerUtils._migratePersonsToNewOrganisation(sourceOrganisationId, targetOrganisationId); var deleteStatements = []; deleteStatements.push(newWhere("CONTACT.CONTACTID", pSourceContactId).buildDeleteStatement()); @@ -1281,6 +1290,7 @@ _DuplicateScannerUtils._getLinkedTableInfos = function(pTargetContactId) ]; } + /* * Returns wether or not a value should be substring'd * @@ -1302,4 +1312,68 @@ _DuplicateScannerUtils._isValueLongerThanCharsToUse = function(pValueLength, pCo _DuplicateScannerUtils._isNotNullAndANumber = function(pCountCharsOfValueToUse) { return pCountCharsOfValueToUse != null && !isNaN(pCountCharsOfValueToUse); +} + +/* + * Persons get reassigned to new organisation + * + * @returns {Boolean} If records have been updated + */ +_DuplicateScannerUtils._migratePersonsToNewOrganisation = function (pSourceOrganisationId, pTargetOrganisationId) +{ + var updateCount = newWhereIfSet(["CONTACT", "ORGANISATION_ID"], pSourceOrganisationId) + .and("PERSON_ID is not null") + .updateFields(new Map().set("ORGANISATION_ID", pTargetOrganisationId), "CONTACT"); + + var deleteCount = newWhere(["CONTACT", "ORGANISATION_ID"], pSourceOrganisationId) + .and("PERSON_ID is not null") + .deleteData(); + return updateCount > 0 || deleteCount > 0; +} + +/* + * Person and organisations keep the max_count = 1 attribute of the target and the one from the source is deleted + * + * @returns {Boolean} If records have been deleted + */ +_DuplicateScannerUtils._deleteUniqueAttributes = function (pSourceContactId, pTargetContactId) +{ + var targetAttrUnique = newSelect("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID") + .from("AB_ATTRIBUTERELATION") + .join("AB_ATTRIBUTEUSAGE on AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID = AB_ATTRIBUTEUSAGE.AB_ATTRIBUTE_ID") + .where("AB_ATTRIBUTEUSAGE.MAX_COUNT = 1") + .and("AB_ATTRIBUTERELATION.OBJECT_ROWID", pTargetContactId) + .table(); + var deleteCount = 0; + + targetAttrUnique.forEach(function(attribute){ + deleteCount += newWhereIfSet("AB_ATTRIBUTERELATION.AB_ATTRIBUTE_ID", attribute) + .and("AB_ATTRIBUTERELATION.OBJECT_ROWID", pSourceContactId) + .deleteData(); + }); + + return deleteCount > 0; +} + +/* + * Update other contacts from the source + * + * @returns {Boolean} If records have been updated + */ + _DuplicateScannerUtils._updateOtherContacts = function (pSourceContactId, sourcePersonId, targetPersonId) +{ + var otherContacts = newSelect("PERSON.PERSONID") + .from("PERSON") + .join("CONTACT", "CONTACT.PERSON_ID = PERSON.PERSONID") + .where("PERSON.PERSONID", sourcePersonId) + .and("CONTACT.CONTACTID", pSourceContactId, SqlBuilder.NOT_EQUAL()) + .table(); + var updateCount = 0; + + otherContacts.forEach(function(person){ + updateCount += newWhere("CONTACT.PERSON_ID", person) + .updateFields({"PERSON_ID" : targetPersonId}, "CONTACT"); + }); + + return updateCount > 0; } \ No newline at end of file