var datetimeLib = $.import("uniorg.libs", "datetime"); //var logginglib = $.import("uniorg.libs", "uologging"); //var billingEngine = $.import("uniorg.eon.emobility.billing.engine.v1", "BillingEngine"); var destLib = $.import("uniorg.destinations", "uodestlib"); var slLib = $.import("uniorg.ServiceLayer", "B1SLLogic"); var serviceName = "eon.emobility.provider.backend.setProviderLinesData.xsjs"; var serviceVersion = '1.2'; var output = {}; var outMessages = []; var outMsg = {}; var outStatus = "success"; var myDebug = {}; var companyid = 0; var UserName = ''; var UserPass = ''; var UserComp = ''; var SESSIONID = ""; var NODEID = ""; var slLoggedIn = false; var schemaName = "A19753_EON_P01"; var debugLevel = 0; switch ($.request.parameters.get("debug")) { case "true": case "yes": case "1": debugLevel = 1; break; case "full": case "2": debugLevel = 2; break; } if (debugLevel > 0) { output.requestParams = $.request.parameters; } function setOutputMessage() { output.status = outStatus; if (debugLevel > 0) { output.debugInfos = myDebug; } output.messages = outMessages; $.response.contentType = "application/json"; $.response.setBody(JSON.stringify(output)); //$.response.status = $.net.http.OK; } function exceptionHandler(ex) { if (outMsg.method && outMessages.indexOf(outMsg) === -1) { outMsg.status = "canceled"; outMessages.push(outMsg); } outMsg = {}; outMsg.method = "exceptionHandler"; outMsg.status = "error"; outMsg.message = ex.toString(); if (debugLevel > 0) { outMsg.exceptionInfo = {}; outMsg.exceptionInfo.name = ex.name; outMsg.exceptionInfo.message = ex.message; outMsg.exceptionInfo.fileName = ex.fileName; outMsg.exceptionInfo.lineNumber = ex.lineNumber; outMsg.exceptionInfo.columnNumber = ex.columnNumber; outMsg.exceptionInfo.stack = ex.stack; //outMsg.exceptionInfo.source = ex.toSource(); } outMessages.push(outMsg); outStatus = "error"; output.status = outStatus; if (debugLevel > 0) { output.debugInfos = myDebug; } output.messages = outMessages; var errbody = JSON.stringify(output); $.response.contentType = 'application/json'; $.response.setBody(errbody); $.response.status = $.net.http.INTERNAL_SERVER_ERROR; try { if (slLoggedIn) { slLib.SLLogout(SESSIONID, NODEID); } } catch (e) { } } function checkAuthorization() { var isAuthorized = true; try { $.session.assertAppPrivilege("uniorg.eon.emobility.provider::MaintainProviderData"); } catch (ex) { isAuthorized = false; outMsg = {}; outMsg.method = "checkAuthorization"; outMsg.status = "error"; outStatus = "error"; outMsg.message = "Privilege '" + ex.privilege + "' is missing"; outMessages.push(outMsg); $.response.status = $.net.http.FORBIDDEN; } return isAuthorized; } function checkPayload(inObj) { var isValid = true; var errorList = []; try { var provider = JSON.parse(inObj); if (!provider.hasOwnProperty('Code')) { isValid = false; } if (Array.isArray(provider.Lines)) { provider.Lines.forEach(function(l) { if (!l.hasOwnProperty('U_CODE') || !l.hasOwnProperty('U_FROM') || !l.hasOwnProperty('U_TO') || !l.hasOwnProperty('U_TYP') || !l.hasOwnProperty('U_OPERATOR') || !l.hasOwnProperty('U_SUBOPERATOR') || !l.hasOwnProperty('U_RELEASED')) { isValid = false; } else { var errorDetails = { U_CODE: l.U_CODE, U_FROM: l.U_FROM, U_TO: l.U_TO, U_TYP: l.U_TYP, U_OPERATOR: l.U_OPERATOR, U_SUBOPERATOR: l.U_SUBOPERATOR, U_RELEASED: l.U_RELEASED, Status: 'error' }; /*if (debugLevel > 0) { errorDetails.Price = npl.Price; errorDetails.Currency = npl.Currency; } if (typeof npl.MaterialNo !== 'string' || !npl.MaterialNo) { errorDetails.StatusText = "MaterialNo is not valid"; } else { if (!npl.MaterialNo.length > 0) { errorDetails.StatusText = "MaterialNo is empty"; } if (npl.MaterialNo.length > 18) { errorDetails.StatusText = "MaterialNo too long"; } } if (typeof npl.PriceList !== 'string' || !npl.PriceList) { errorDetails.StatusText = "PriceList is not valid"; } else { if (!npl.PriceList.length > 0) { errorDetails.StatusText = "PriceList is empty"; } if (npl.PriceList.length > 32) { errorDetails.StatusText = "PriceList too long"; } } try { var parsedDate = npl.DateFrom.startsWith('/Date(') ? new Date(parseInt(npl.DateFrom.substr(6))) : new Date(npl.DateFrom); if (debugLevel > 0) { errorDetails.ParsedDate = parsedDate; } if (!parsedDate instanceof Date || isNaN(parsedDate.valueOf())) { errorDetails.StatusText = "DateFrom is not a valid date"; } else if (!(parsedDate.getTime() >= new Date('2000-01-01').getTime() && parsedDate.getTime() <= new Date('9999-12-31').getTime())) { errorDetails.StatusText = "DateFrom is outside of valid range"; } } catch (e) { errorDetails.StatusText = "DateFrom is not a valid date"; } if (typeof npl.Price !== 'number' || isNaN(npl.Price)) { errorDetails.StatusText = "Price is not valid"; } else { var priceParts = npl.Price.toString().split('.'); var priceExp = npl.Price.toString().split('e'); if (debugLevel > 0) { errorDetails.PriceDecimalParts = priceParts; } if (npl.Price < 0) { errorDetails.StatusText = "Price must be a positive value"; } else if (priceExp.length > 1 && !isNaN(priceExp[1]) && parseInt(priceExp[1]) + priceParts[0].length > 17) { errorDetails.StatusText = "Price decimal precision too high"; } else if ((priceParts.length > 1 && priceParts[0].length + priceParts[1].length > 17) || priceParts[0].length > 17) { errorDetails.StatusText = "Price decimal precision too high"; } else if (priceParts.length > 1 && priceParts[1].length > 6) { errorDetails.StatusText = "Price decimal scale too high"; } } if (typeof npl.Currency !== 'string' || !npl.Currency) { errorDetails.StatusText = "Currency is not valid"; } else { if (!npl.Currency.length > 0) { errorDetails.StatusText = "Currency is empty"; } if (npl.Currency.length > 3) { errorDetails.StatusText = "Currency too long"; } }*/ if (errorDetails.StatusText) { errorList.push(errorDetails); isValid = false; } else if (debugLevel > 0) { errorDetails.Status = "success"; errorList.push(errorDetails); } } }); } else { isValid = false; } } catch (e) { exceptionHandler(e); isValid = false; } if (!isValid) { outMsg = {}; outMsg.method = "checkPayload"; outMsg.status = "error"; outStatus = "error"; if (errorList.length > 0) { outMsg.message = "invalid payload content"; output.results = errorList; } else { outMsg.message = "invalid payload structure"; } outMessages.push(outMsg); $.response.status = $.net.http.BAD_REQUEST; } return isValid; } function setConnectionParams() { var idValue = schemaName; var idFieldname = 'SYS_NAME'; var CompanyConfig = destLib.getCompanyCfg(idValue, idFieldname); UserComp = CompanyConfig.COMPANYNAME; UserName = CompanyConfig.USERNAME; UserPass = destLib.readSecureStore(CompanyConfig.ID); if (debugLevel > 1) { myDebug.connectionParams = CompanyConfig; } myDebug.idValue = idValue; myDebug.idFieldname = idFieldname; if (CompanyConfig.COMPANYNAME === undefined) { outMsg = {}; outMsg.method = "getCompany"; outStatus = "error"; outMsg.status = "error"; outMsg.message = "System " + idValue + " is invalid for this endpoint"; outMessages.push(outMsg); } myDebug.CompanyName = UserComp; } function slLogin() { var loginResult = false; var loginInfo = {}; loginInfo.UserName = UserName; loginInfo.Password = UserPass; loginInfo.CompanyDB = UserComp; // SL LOGIN var response = slLib.SLLogin(JSON.stringify(loginInfo), null, null); // B1SESSION and ROUTEID cookies returned by Login for (var j in response.cookies) { if (response.cookies[j].name == "B1SESSION") { SESSIONID = response.cookies[j].value; //output.SessionID = SESSIONID; } else if (response.cookies[j].name == "ROUTEID") { NODEID = response.cookies[j].value; //output.NodeID = NODEID; } } var loginResponse; try { loginResponse = JSON.parse(response.body.asString()); } catch (e) { loginResponse = response.body.asString(); } if (debugLevel > 1) { myDebug.loginInfo = loginInfo; } myDebug.loginResponse = loginResponse; if (response.status === 200) { loginResult = true; } else { outMsg = {}; outMsg.method = "login"; outMsg.status = "error"; var slError = myDebug.loginResponse.error; if (slError && slError.hasOwnProperty("code") && slError.message && slError.message.value) { outMsg.message = slError.message.value + " (Code: " + slError.code.toString() + ")"; } else { outMsg.message = JSON.stringify(myDebug.loginResponse); } outMsg.response = myDebug.loginResponse; //outMsg.message = loginResponse; outMessages.push(outMsg); outStatus = "error"; } return loginResult; } function updateUDO(provider, oldUDO) { outMsg = {}; outMsg.method = "updateUDO"; outMsg.status = "error"; try { var slProviderObj = { U_ACTIVE: provider.U_ACTIVE, U_ACTIVE_FROM: datetimeLib.convertToISODate(provider.U_ACTIVE_FROM), U_ACTIVE_TO: datetimeLib.convertToISODate(provider.U_ACTIVE_TO), EON_PROVIDER_LINESCollection: oldUDO.EON_PROVIDER_LINESCollection.map(function(ol) { var line = { Code: ol.Code, LineId: ol.LineId, Object: ol.Object }; var changedLine = false; var nl = provider.Lines.find(function(l) { return l.Code === ol.Code && l.LineId === ol.LineId; }); if (typeof nl !== "undefined") { if (nl.U_RELEASED !== ol.U_RELEASED) { //changedLine = true; line.U_RELEASED = nl.U_RELEASED; if (nl.U_RELEASED === 'Y') { line.U_RELEASE_DATE = datetimeLib.convertToISODate(new Date()); line.U_RELEASE_USER = $.session.getUsername(); } } if (datetimeLib.convertToISODate(nl.U_TO) !== ol.U_TO) { changedLine = true; line.U_TO = datetimeLib.convertToISODate(nl.U_TO); } if (nl.U_SUBOPERATOR !== ol.U_SUBOPERATOR) { changedLine = true; line.U_SUBOPERATOR = nl.U_SUBOPERATOR; } if (nl.U_VALUE !== ol.U_VALUE) { changedLine = true; line.U_VALUE = nl.U_VALUE; } if (nl.U_CURRENCY !== ol.U_CURRENCY) { changedLine = true; line.U_CURRENCY = nl.U_CURRENCY; } if (ol.U_CODE === 'MINUTES_BLOCK') { if (nl.U_BLOCK_FROM !== ol.U_BLOCK_FROM.substring(0,5)) { changedLine = true; line.U_BLOCK_FROM = nl.U_BLOCK_FROM; } if (nl.U_BLOCK_TO !== ol.U_BLOCK_TO.substring(0,5)) { changedLine = true; line.U_BLOCK_TO = nl.U_BLOCK_TO; } if (nl.U_BLOCKMAX !== ol.U_BLOCKMAX) { changedLine = true; line.U_BLOCKMAX = nl.U_BLOCKMAX; } } if (changedLine) { line.U_CHANGE_DATE = datetimeLib.convertToISODate(new Date()); line.U_CHANGE_USER = $.session.getUsername(); } } return line; /*{ Code: l.Code, LineId: l.LineId, U_CODE: l.U_CODE, U_FROM: datetimeLib.convertToISODate(l.U_FROM), U_TO: datetimeLib.convertToISODate(l.U_TO), U_OPERATOR: l.U_OPERATOR, U_TYP: l.U_TYP, U_VALUE: l.U_VALUE, U_BLOCK_FROM: l.U_BLOCK_FROM, U_BLOCK_TO: l.U_BLOCK_TO, U_BLOCKMAX: l.U_BLOCKMAX, U_RELEASED: l.U_RELEASED, U_RELEASE_DATE: l.U_RELEASED === 'Y' ? datetimeLib.convertToISODate(new Date()) : l.U_RELEASE_DATE, U_RELEASE_USER: l.U_RELEASED === 'Y' ? $.session.getUsername() : l.U_RELEASED, U_CHANGE_DATE: datetimeLib.convertToISODate(new Date()) };*/ }).concat(provider.Lines.filter(function(l) { return l.new === true; }).map(function(nl) {return { U_CODE: nl.U_CODE, U_FROM: datetimeLib.convertToISODate(nl.U_FROM), U_TO: datetimeLib.convertToISODate(nl.U_TO), U_OPERATOR: nl.U_OPERATOR, U_SUBOPERATOR: nl.U_SUBOPERATOR, U_TYP: nl.U_TYP, U_CURRENCY: nl.U_CURRENCY, U_VALUE: nl.U_VALUE, U_BLOCK_FROM: nl.U_BLOCK_FROM, U_BLOCK_TO: nl.U_BLOCK_TO, U_BLOCKMAX: nl.U_BLOCKMAX, U_RELEASED: nl.U_RELEASED, U_RELEASE_DATE: nl.U_RELEASED === 'Y' ? datetimeLib.convertToISODate(new Date()) : nl.U_RELEASE_DATE, U_RELEASE_USER: nl.U_RELEASED === 'Y' ? $.session.getUsername() : nl.U_RELEASE_USER, U_CHANGE_DATE: datetimeLib.convertToISODate(new Date()), U_CHANGE_USER: $.session.getUsername() };})) }; myDebug.slProviderObj = slProviderObj; var responseProvider = slLib.patchDynamicEntity('EON_PROVIDER', provider.Code, JSON.stringify(slProviderObj), SESSIONID, NODEID, true); if (responseProvider.status >= 200 && responseProvider.status < 300) { outMsg.status = "success"; } else { if (responseProvider.body) { var responseBodyParsed = JSON.parse(responseProvider.body.asString()); var slError = responseBodyParsed.error; if (slError && slError.hasOwnProperty("code") && slError.message && slError.message.value) { outMsg.message = slError.message.value + " (Code: " + slError.code.toString() + ")"; } else { outMsg.message = responseProvider.body.asString(); } outMsg.response = responseBodyParsed; } else { outMsg.message = JSON.stringify(responseProvider); } outStatus = "error"; } outMessages.push(outMsg); } catch (ex) { exceptionHandler(ex); } } function getUDO(Code) { outMsg = {}; outMsg.method = "getUDO"; outMsg.code = Code; outMsg.status = "error"; var result = {}; try { var responseGet = slLib.getDynamicEntity('EON_PROVIDER', Code, SESSIONID, NODEID); outMsg.statuscode = responseGet.status; if (responseGet.status >= 200 && responseGet.status < 300) { var bodyGet = JSON.parse(responseGet.body.asString()); outMsg.status = "success"; result = bodyGet; outMsg.response = bodyGet; } else { if (responseGet.body) { var responseBodyParsed = JSON.parse(responseGet.body.asString()); var slError = responseBodyParsed.error; if (slError && slError.hasOwnProperty("code") && slError.message && slError.message.value) { outMsg.message = slError.message.value + " (Code: " + slError.code.toString() + ")"; } else { outMsg.message = responseGet.body.asString(); } outMsg.response = responseBodyParsed; } else { outMsg.message = JSON.stringify(responseGet); } outStatus = "error"; } if (debugLevel > 0 || outMsg.status !== "success") { outMessages.push(outMsg); } } catch (ex) { exceptionHandler(ex); } return result; } // main var content = ""; if ($.request.body) { content = $.request.body.asString(); } if (checkPayload(content) && checkAuthorization()) { setConnectionParams(); slLoggedIn = slLogin(); if (slLoggedIn) { var inObj = JSON.parse(content); var oldUDO = getUDO(inObj.Code); if (outStatus !== "error" && oldUDO.Code) { updateUDO(inObj, oldUDO); } slLib.SLLogout(SESSIONID, NODEID); } } $.trace.debug("DEBUG: " + JSON.stringify(myDebug)); setOutputMessage();