From ca341c2db4e1a2a2b7e40c5828a77556910a84d3 Mon Sep 17 00:00:00 2001 From: "j.goderbauer" <j.goderbauer@adito.de> Date: Thu, 4 Apr 2019 08:25:16 +0200 Subject: [PATCH] prevent duplicate contacts (same ORGANISATION and PERSON) (2) --- .../organisation_id/onValidation.js | 20 ++----- .../organisation_id/onValidation.js | 58 +++++-------------- process/Contact_lib/process.js | 39 +++++++++++++ 3 files changed, 59 insertions(+), 58 deletions(-) diff --git a/entity/Contact_entity/entityfields/organisation_id/onValidation.js b/entity/Contact_entity/entityfields/organisation_id/onValidation.js index 320f78dc97..414972ba58 100644 --- a/entity/Contact_entity/entityfields/organisation_id/onValidation.js +++ b/entity/Contact_entity/entityfields/organisation_id/onValidation.js @@ -1,3 +1,4 @@ +import("Contact_lib"); import("system.translate"); import("system.result"); import("system.db"); @@ -14,17 +15,8 @@ var organisationId = ProcessHandlingUtils.getOnValidationValue("$field.ORGANISAT //TODO: change the workaround behaviour when $local.value is retrieved correct organisationId = vars.getString("$field.ORGANISATION_ID"); -if (personId) -{ - if (organisationId == "") - organisationId = "0"; - var alreadyExistantContactId = db.cell(SqlCondition.begin() - .andPrepare("CONTACT.PERSON_ID", personId) - .andPrepare("CONTACT.ORGANISATION_ID", organisationId) - .buildSql("select CONTACT.CONTACTID from CONTACT")); - if (alreadyExistantContactId != "") - if (organisationId.trim() == "0") - result.string(translate.text("This private person doeas already exist and can not be created once more.")); - else - result.string(translate.text("This combination of person and organisation does already exist and can not be created once more.")); -} \ No newline at end of file +//a entry within the Contact_enity can never be edited only created (it's edited within the Person_entity) +//so no need to provide our own CONTACTID since it does not exist in the database right now => provide null instead +var validationMsg = ContactUtils.validateIfAlreadyExists(personId, organisationId, null); +if (validationMsg) + result.string(validationMsg); \ No newline at end of file diff --git a/entity/Person_entity/entityfields/organisation_id/onValidation.js b/entity/Person_entity/entityfields/organisation_id/onValidation.js index ae01ddc13b..daba90e666 100644 --- a/entity/Person_entity/entityfields/organisation_id/onValidation.js +++ b/entity/Person_entity/entityfields/organisation_id/onValidation.js @@ -1,52 +1,22 @@ -import("system.logging"); -import("system.translate"); import("system.result"); -import("system.db"); -import("system.vars"); import("system.neon"); +import("system.vars"); import("Entity_lib"); import("Contact_lib"); -import("Sql_lib"); -//TODO: comment -//TODO: into lib -//TODO: use in Contact -ContactUtils.validateOrganisationId = function(pPersonId, pOrganisationId, pOwnContactId) +if (vars.get("$sys.recordstate") != neon.OPERATINGSTATE_NEW) { - if (!pPersonId) - return null; - if (pOrganisationId == "") - pOrganisationId = "0"; - var cond = SqlCondition.begin() - .andPrepare("CONTACT.PERSON_ID", pPersonId) - .andPrepare("CONTACT.ORGANISATION_ID", pOrganisationId) - //exclude the own since we do not want a "is not valid"-message for our own entry (on EDIT-mode) - .andPrepareIfSet("CONTACT.CONTACTID", pOwnContactId, "# != ?"); - - var sql = cond.buildSql("select CONTACT.CONTACTID from CONTACT"); - var alreadyExistantContactId = db.cell(sql); - if (alreadyExistantContactId) - if (pOrganisationId.trim() == "0") - return translate.text("This private person doeas already exist and can not be created once more."); - else - return translate.text("This combination of person and organisation does already exist and can not be created once more."); + var personId = vars.getString("$field.PERSONID"); + var organisationId = ProcessHandlingUtils.getOnValidationValue("$field.ORGANISATION_ID"); + var contactId = vars.get("$field.CONTACTID");//in EDIT we have to exclude our own CONTACTID since we do not want a message for our own contactentry - return null; -}; - - -var personId = vars.getString("$field.PERSONID"); -var organisationId = ProcessHandlingUtils.getOnValidationValue("$field.ORGANISATION_ID"); -var contactId;//in EDIT we have to exclude our own CONTACTID since we do not want a message for our own contactentry -if (vars.get("$sys.recordstate") != neon.OPERATINGSTATE_NEW) - contactId = vars.get("$field.CONTACTID"); - -//workaround for organisationId: $local.value will return the name of the organisation which is not what we want -//but the field already contains the changed value; so let's load the field instead of the $local.value-variable -//this is a bug within the ADITO-kernel -//TODO: change the workaround behaviour when $local.value is retrieved correct -organisationId = vars.getString("$field.ORGANISATION_ID"); + //workaround for organisationId: $local.value will return the name of the organisation which is not what we want + //but the field already contains the changed value; so let's load the field instead of the $local.value-variable + //this is a bug within the ADITO-kernel + //TODO: change the workaround behaviour when $local.value is retrieved correct + organisationId = vars.getString("$field.ORGANISATION_ID"); -var validationMsg = ContactUtils.validateOrganisationId(personId, organisationId, contactId); -if (validationMsg) - result.string(validationMsg); \ No newline at end of file + var validationMsg = ContactUtils.validateIfAlreadyExists(personId, organisationId, contactId); + if (validationMsg) + result.string(validationMsg); +} \ No newline at end of file diff --git a/process/Contact_lib/process.js b/process/Contact_lib/process.js index 9650bbacd2..7539546b86 100644 --- a/process/Contact_lib/process.js +++ b/process/Contact_lib/process.js @@ -1,3 +1,4 @@ +import("system.translate"); import("system.neon"); import("system.vars"); import("system.result"); @@ -34,6 +35,44 @@ OrganisationUtils.getNameByOrganisationId = function(pOrganisationId) */ function ContactUtils() {} +/* + * validates if a ORGANISATION_ID in a person-contact is correct [=>does not already exist] or not [=>does already exist] + * this is done by checking the database for entires that do already exist with this combination of ORGANISATIONID and PERSONID + * gives different messages for private persons and contacts that do already exist + * + * @param {String} pPersonId the ID of the person that shall be searched in the database + * @param {String} pOrganisationId the ID of the organisation that shall be searched in the database; + * if this is an empty string it will be treated as private-dummy-organisation + * @param {String} [pOwnContactId] the CONTACTID of your current record; this is only needed when in EDIT-mode since you don't want to get a message + * for your own CONTACT; + * (if you do a lookup if a organisation-person-combination does already exist you'l get your own contact which you want to exclude) + * + * @return {String} translated text that describes whats the problem or null if there was no problem and everything is fine + * + */ +ContactUtils.validateIfAlreadyExists = function(pPersonId, pOrganisationId, pOwnContactId) +{ + if (!pPersonId) + return null; + if (pOrganisationId == "") + pOrganisationId = "0"; + var cond = SqlCondition.begin() + .andPrepare("CONTACT.PERSON_ID", pPersonId) + .andPrepare("CONTACT.ORGANISATION_ID", pOrganisationId) + //exclude the own since we do not want a "is not valid"-message for our own entry (on EDIT-mode) + .andPrepareIfSet("CONTACT.CONTACTID", pOwnContactId, "# != ?"); + + var sql = cond.buildSql("select CONTACT.CONTACTID from CONTACT"); + var alreadyExistantContactId = db.cell(sql); + if (alreadyExistantContactId) + if (pOrganisationId.trim() == "0") + return translate.text("This private person doeas already exist and can not be created once more."); + else + return translate.text("This combination of person and organisation does already exist and can not be created once more."); + + return null; +}; + /** * Get the type of contact. <br> * In recordstate NEW or EDIT it loads the person- / orgid from the db.<br> -- GitLab