264 lines
7.0 KiB
JavaScript
264 lines
7.0 KiB
JavaScript
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.addNewProvider.xsjs";
|
|
var serviceVersion = '1.0';
|
|
|
|
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;
|
|
try {
|
|
var provider = JSON.parse(inObj);
|
|
if (!provider.hasOwnProperty('Code') || !provider.hasOwnProperty('Name') || !provider.hasOwnProperty('U_ACTIVE') ||
|
|
!provider.hasOwnProperty('U_ACTIVE_FROM') || !provider.hasOwnProperty('U_ACTIVE_TO')) {
|
|
isValid = false;
|
|
}
|
|
} catch (e) {
|
|
exceptionHandler(e);
|
|
isValid = false;
|
|
}
|
|
if (!isValid) {
|
|
outMsg = {};
|
|
outMsg.method = "checkPayload";
|
|
outMsg.status = "error";
|
|
outStatus = "error";
|
|
outMsg.message = "invalid payload content";
|
|
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 createUDO(provider) {
|
|
outMsg = {};
|
|
outMsg.method = "createUDO";
|
|
outMsg.status = "error";
|
|
try {
|
|
var slProviderObj = {
|
|
Code: provider.Code,
|
|
Name: provider.Name,
|
|
U_ACTIVE: provider.U_ACTIVE,
|
|
U_ACTIVE_FROM: datetimeLib.convertToISODate(provider.U_ACTIVE_FROM),
|
|
U_ACTIVE_TO: datetimeLib.convertToISODate(provider.U_ACTIVE_TO)
|
|
};
|
|
myDebug.slProviderObj = slProviderObj;
|
|
var responseProvider = slLib.postDynamicEntity('EON_PROVIDER', JSON.stringify(slProviderObj), SESSIONID, NODEID);
|
|
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);
|
|
}
|
|
}
|
|
|
|
// main
|
|
var content = "";
|
|
if ($.request.body) {
|
|
content = $.request.body.asString();
|
|
}
|
|
if (checkPayload(content) && checkAuthorization()) {
|
|
setConnectionParams();
|
|
slLoggedIn = slLogin();
|
|
if (slLoggedIn) {
|
|
var inObj = JSON.parse(content);
|
|
createUDO(inObj);
|
|
slLib.SLLogout(SESSIONID, NODEID);
|
|
}
|
|
}
|
|
|
|
$.trace.debug("DEBUG: " + JSON.stringify(myDebug));
|
|
setOutputMessage(); |