From 644d0d2a57b3bb49ccc48041ac7ac42495b957e1 Mon Sep 17 00:00:00 2001 From: Johannes Hoermann <j.hoermann@adito.de> Date: Wed, 24 Apr 2019 11:52:15 +0200 Subject: [PATCH] address webservice better error handling, disable by default --- entity/Address_entity/Address_entity.aod | 4 +- .../entityfields/addr/onValidation.js | 2 +- .../_____PREFERENCES_PROJECT.aod | 12 +- process/WsValidation_lib/process.js | 114 +++++++++++++----- 4 files changed, 96 insertions(+), 36 deletions(-) diff --git a/entity/Address_entity/Address_entity.aod b/entity/Address_entity/Address_entity.aod index b7ca56c2922..6d9770c1885 100644 --- a/entity/Address_entity/Address_entity.aod +++ b/entity/Address_entity/Address_entity.aod @@ -37,7 +37,6 @@ <entityField> <name>CITY</name> <title>City</title> - <consumer>CityValidation</consumer> <mandatoryProcess>%aditoprj%/entity/Address_entity/entityfields/city/mandatoryProcess.js</mandatoryProcess> <displayValueProcess>%aditoprj%/entity/Address_entity/entityfields/city/displayValueProcess.js</displayValueProcess> <onValueChange>%aditoprj%/entity/Address_entity/entityfields/city/onValueChange.js</onValueChange> @@ -74,9 +73,8 @@ <entityField> <name>ZIP</name> <title>postcode</title> - <consumer>ZipValidation</consumer> + <consumer></consumer> <mandatoryProcess>%aditoprj%/entity/Address_entity/entityfields/zip/mandatoryProcess.js</mandatoryProcess> - <newItemsAllowed v="true" /> <displayValueProcess>%aditoprj%/entity/Address_entity/entityfields/zip/displayValueProcess.js</displayValueProcess> <onValidation>%aditoprj%/entity/Address_entity/entityfields/zip/onValidation.js</onValidation> <onValueChange>%aditoprj%/entity/Address_entity/entityfields/zip/onValueChange.js</onValueChange> diff --git a/entity/Communication_entity/entityfields/addr/onValidation.js b/entity/Communication_entity/entityfields/addr/onValidation.js index be9753504a1..54a9148c82d 100644 --- a/entity/Communication_entity/entityfields/addr/onValidation.js +++ b/entity/Communication_entity/entityfields/addr/onValidation.js @@ -23,5 +23,5 @@ if (fn != null) additional.countryCode = vars.get("$param.ContactsMainCountry_param");//TODO: try to use users language first and then the companies var res = fn.call(null, commAddr, additional); if (res != null) - result.object(res); + result.string(res); } \ No newline at end of file diff --git a/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod b/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod index 4205f7bebd3..4bbd9ff9696 100644 --- a/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod +++ b/preferences/_____PREFERENCES_PROJECT/_____PREFERENCES_PROJECT.aod @@ -2,7 +2,7 @@ <preferences xmlns="http://www.adito.de/2018/ao/Model" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" VERSION="3.1.0" xsi:schemaLocation="http://www.adito.de/2018/ao/Model adito://models/xsd/preferences/3.1.0"> <name>_____PREFERENCES_PROJECT</name> <majorModelMode>DISTRIBUTED</majorModelMode> - <projectName>basic 2019</projectName> + <projectName>xRM-Basic2019</projectName> <jditoMaxContentSize v="57671680" /> <calendarCategoriesEvent> <entry> @@ -34,6 +34,10 @@ <indexsearchIncrementingIndexerBunchSize v="250" /> <indexsearchMaximumHits v="50000" /> <customProperties> + <customBooleanProperty> + <name>addressValidation.enable</name> + <property v="false" /> + </customBooleanProperty> <customStringProperty> <name>addressValidation.url</name> <property>https://services.aditosoftware.local/services/rest/ws_checkAddress</property> @@ -50,8 +54,14 @@ <name>addressValidation.countryParamName</name> <property>country</property> </customStringProperty> + <customBooleanProperty> + <name>phoneValidation.enable</name> + <description></description> + <property v="false" /> + </customBooleanProperty> <customStringProperty> <name>phoneValidation.url</name> + <description></description> <property>https://services.aditosoftware.local/services/rest/ws_checkPhoneNumber</property> </customStringProperty> <customStringProperty> diff --git a/process/WsValidation_lib/process.js b/process/WsValidation_lib/process.js index 5a0cf4615d7..f2c51eca730 100644 --- a/process/WsValidation_lib/process.js +++ b/process/WsValidation_lib/process.js @@ -49,13 +49,23 @@ WsValidationType.get = function(pKey) function _processAddressLookup(pWsResult, pValue) { + + // add default result consisting of pValue var defaultResult = [[{}, pValue]]; defaultResult[0][0][this.paramName] = pValue; - return JSON.parse(pWsResult) - .concat(defaultResult) - .map(function(pAddress) + var resultAddresses = []; + + // if error, only add the default value else parse the body + if (!WsValidationUtils._isError(pWsResult)) + { + resultAddresses = JSON.parse(pWsResult.body); + } + + resultAddresses = resultAddresses.concat(defaultResult); + + return resultAddresses.map(function(pAddress) { var data = pAddress[0]; return [ @@ -68,12 +78,19 @@ WsValidationType.get = function(pKey) data.region, data.state, data[this.paramName]] - }, this) + }, this); } function _processCommunicationValidation(pWsResult, pValue) { - return pWsResult; + if (!WsValidationUtils._isError(pWsResult)) + { + return pWsResult.body; + } + else + { + return pValue; + } } } @@ -108,39 +125,42 @@ WsValidationUtils.validate = function(pValue, pType, pCountry) if (typeof pType == "string") pType = WsValidationType.get(pType); - // get configurations for this type - var userName = project.getPreferenceValue("custom." + pType.webserviceName + ".user"); - var pw = project.getPreferenceValue("custom." + pType.webserviceName + ".pw"); - var url = project.getPreferenceValue("custom." + pType.webserviceName + ".url"); - var countryParamName = project.getPreferenceValue("custom." + pType.webserviceName + ".countryParamName"); - - var actionType = "GET"; - - if (pValue && pType) + if (WsValidationUtils.isWsEnabled(pType)) { - // fill params - var parameters = {}; - if (pCountry) - { - parameters[countryParamName] = pCountry; - } - parameters[pType.paramName] = pValue; - - // call webservice - var ret = JSON.parse(net.callRestWebserviceBasicAuth(url, actionType, parameters, null, null, "text/plain", "text/plain", util.DATA_TEXT, util.DATA_TEXT, userName, pw, true)); - if (ret.hasHttpSuccessStatusCode && ret.httpStatusCode == 200) + var userName = project.getPreferenceValue("custom." + pType.webserviceName + ".user"); + var pw = project.getPreferenceValue("custom." + pType.webserviceName + ".pw"); + var url = project.getPreferenceValue("custom." + pType.webserviceName + ".url"); + var countryParamName = project.getPreferenceValue("custom." + pType.webserviceName + ".countryParamName"); + + var actionType = "GET"; + + if (pValue && pType) { + // fill params + var parameters = {}; + if (pCountry) + { + parameters[countryParamName] = pCountry; + } + parameters[pType.paramName] = pValue; + + // call webservice + var ret = JSON.parse(net.callRestWebserviceBasicAuth(url, actionType, parameters, null, null, "text/plain", "text/plain", util.DATA_TEXT, util.DATA_TEXT, userName, pw, true)); + + // if error, log the error. But also process the result and let the callback generate the correct default value + if (WsValidationUtils._isError(ret)) + { + logging.log(translate.withArguments("${WEBSERVICE_ERROR} url:%0 status:%1", [url, (ret.hasHttpSuccessStatusCode ? " StatusCode: " + ret.httpStatusCode : "")])); + logging.log("Params: " + parameters.toSource()); + } + // different handling of the result per type - return pType.processResultCallback(ret.body, pValue); - } - else - { - throw new Error(translate.withArguments("${WEBSERVICE_ERROR} url:%0 status:%1", [url, (ret.hasHttpSuccessStatusCode ? " StatusCode: " + ret.httpStatusCode : "")])); + return pType.processResultCallback(ret, pValue); } } - return []; + return pType.processResultCallback(null, pValue); } /** @@ -169,4 +189,36 @@ WsValidationUtils.setAddressFields = function() if (pValue) neon.setFieldValue(pField, pValue); } +} + +/** + * check if the Webservice is enabled in the project preferences + * @param {Object} pType + * + * @return {boolean} + */ +WsValidationUtils.isWsEnabled = function(pType) +{ + // get WsValidationType-Object if it is only the key + if (typeof pType == "string") + pType = WsValidationType.get(pType); + + // get configurations for this type + try + { + return JSON.parse(project.getPreferenceValue("custom." + pType.webserviceName + ".enable")); + } catch (exception) { + return false; + } +} + +/** + * check if the Webservice-result is valid or an error + * @param {Object} pWsReturn + * + * @return {boolean} + */ +WsValidationUtils._isError = function(pWsReturn) +{ + return pWsReturn == null || !pWsReturn.hasHttpSuccessStatusCode || pWsReturn.httpStatusCode != 200; } \ No newline at end of file -- GitLab