Skip to content
Snippets Groups Projects
onDBInsert.js 3.04 KiB
Newer Older
import("Workflow_lib");
S.Listl's avatar
S.Listl committed
import("KeywordRegistry_basic");
import("DataPrivacy_lib");
import("system.vars");
import("StandardObject_lib");
S.Listl's avatar
S.Listl committed
import("Keyword_lib");
import("Location_lib");
import("Address_lib");
S.Listl's avatar
S.Listl committed

var rowdata = vars.get("$local.rowdata");
var addrType = rowdata["ADDRESS.ADDR_TYPE"];
S.Listl's avatar
S.Listl committed
var typeParam = vars.get("$param.ContactType_param");

S.Listl's avatar
S.Listl committed
if (!typeParam && vars.exists("$param.PersonContactId_param") && vars.getString("$param.PersonContactId_param"))
S.Listl's avatar
S.Listl committed
{
S.Listl's avatar
S.Listl committed
    var isOrgType = KeywordUtils.getAttributeRelation(addrType, $KeywordRegistry.addressType(), "organisation");
    typeParam = isOrgType ? "organisation" : "contact";
S.Listl's avatar
S.Listl committed
}

var scopeType = null
if (typeParam === "contact")
    scopeType = "Person"
else if (typeParam === "organisation")
    scopeType = "Organisation"

new StandardObject("Address", vars.get("$local.uid"), scopeType, id)
    .onObjectInsert(vars.exists("$param.ReplaceStandardAddress_param") ? vars.get("$param.ReplaceStandardAddress_param") : undefined);

//adds the orgaddress as standard address to all the contact's that were created before the org had an address
if(scopeType == "Organisation") 
{
    var contacts = newSelect("CONTACTID")
                            .from("CONTACT")
                            .where("CONTACT.ORGANISATION_ID", newSelect("CONTACT.ORGANISATION_ID")
                                                                        .from("CONTACT")
                                                                        .where("CONTACT.CONTACTID", id)
                                                                        .cell())
                            .and("CONTACT.PERSON_ID is not null")
                            .arrayColumn();

    if(contacts.length > 0)
        for (var i = 0; i < contacts.length; i++) 
        {
                new StandardObject("Address", vars.get("$local.uid"), "Person", contacts[i])
                    .onObjectInsert(vars.exists("$param.ReplaceStandardAddress_param") ? vars.get("$param.ReplaceStandardAddress_param") : undefined);
        }
}
if(vars.exists("$context.PushDataPrivacyNotification") && vars.get("$context.PushDataPrivacyNotification") == "false")
{
    DataPrivacyUtils.notifyNeedDataPrivacyUpdate(rowdata["COMMUNICATION.CONTACT_ID"], vars.get("$param.ShowDsgvoMessage_param"));
    vars.set("$context.PushDataPrivacyNotification", "true");
}
var address = new AddressObject(rowdata["ADDRESS.ADDRESS"], rowdata["ADDRESS.BUILDINGNO"], rowdata["ADDRESS.ZIP"], rowdata["ADDRESS.CITY"], rowdata["ADDRESS.COUNTRY"], rowdata["ADDRESS.STATE"]);
var addressLocation = new LocationFinder().getGeoLocation(address);
if (addressLocation)
{
    let fieldsValues = {
        "LAT": addressLocation.lat,
        "LON": addressLocation.lon
    };
    
    if (rowdata["ADDRESS.VALIDATION_RESULT"] === null)
    {
        fieldsValues["VALIDATION_RESULT"] = addressLocation.requestResult;
    }
    
    newWhere("ADDRESS.ADDRESSID", "$local.uid")
}

WorkflowSignalSender.inserted();