Files

416 lines
12 KiB
JavaScript

// Prod API Documentation Url: https://virta-api-admin.api-docs.io/
var ParDebug = false;
var Billingrunmode = "EMP";
var DateFrom = "";
var DateTo = "";
var output = {};
var outMessages = [];
var outErrorMessages = [];
var outMsg = {};
var outStatus = "success";
var myDebug = {};
var destn_pkg = "uniorg.eon.virta.v1";
var dest_name = "VirtaAPI";
var serviceVersion = '1.3';
var pageSize = 100;
/*if ($.request.parameters.get("pageSize")) {
pageSize = $.request.parameters.get("pageSize");
}*/
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) {
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;
}
function getVirtaToken(ifUsername, ifPassword) {
try
{
var dest = $.net.http.readDestination(destn_pkg, dest_name);
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.POST, "/auth");
req.contentType = "application/json";
outMsg = {};
outMsg.username = ifUsername;
outMsg.password = ifPassword;
req.setBody(JSON.stringify(outMsg));
client.request(req, dest);
var response = client.getResponse();
var b1iResp = response.body.asString();
var retObj = JSON.parse(b1iResp);
return retObj.token.toString();
} catch (e) {
$.response.contentType = "text/plain";
$.response.setBody(e.message);
}
}
function getCPOList() {
var mytoken = getVirtaToken("b1_api_user", "E3qq$§U,Q3hD#K2}");
var dest = $.net.http.readDestination(destn_pkg, dest_name);
var client = new $.net.http.Client();
var req = new $.web.WebRequest($.net.http.GET, "/cpo/charges");
req.contentType = "application/json";
req.headers.set("Authorization", "Bearer" + mytoken);
req.parameters.set("perPage", pageSize.toString());
req.parameters.set("from", DateFrom);
req.parameters.set("to", DateTo);
var totalCount = 1;
myDebug.Calls = [];
for (var p = 1; p <= Math.ceil(totalCount / pageSize); ++p) {
var debugCall = {};
debugCall.request = { "page": p };
req.parameters.set("page", p.toString());
client.request(req, dest);
var response = client.getResponse();
// get all the cookies and headers from the response
var he = [];
for(var c in response.headers) {
he.push(response.headers[c]);
}
debugCall.response = { "headers": he };
var virtaBody = response.body.asString();
totalCount = response.headers.get('X-Total-Count');
var maxPageSize = Math.ceil(totalCount / pageSize);
debugCall.totalCount = totalCount;
debugCall.pagesCount = maxPageSize;
debugCall.link = response.headers.get('link');
debugCall.responseLineCount = JSON.parse(virtaBody).length;
//debugCall.virtaBody = virtaBody;
// debugCall.header = response.headers.toSting();
myDebug.Calls.push(debugCall);
writeObj2Table(virtaBody);
}
//$.response.status = $.net.http.OK;
//$.response.contentType = "application/json";
// $.response.setBody(JSON.stringify(b1iResp));
//$.response.setBody(JSON.stringify(virtaBody));
}
function getnextval() {
var conn = $.db.getConnection();
var pstmt;
var query = "select \"NICEPORTAL\".\"SEQ_CRMBP_ID\".NEXTVAL from dummy";
pstmt = conn.prepareStatement(query);
var rs = pstmt.executeQuery();
if (!rs.next()) {
outMsg = {};
outMsg.method = "getnexval from sequence error";
outMsg.status = "error";
// outMsg.message = e.message;
outMessages.push(outMsg);
} else {
return rs.getString(1);
}
rs.close();
pstmt.close();
conn.close();
}
function writeObj2Table(myOBJ) {
//var pdcontent = $.request.body.asString();
// pdcontent = pdcontent.replace(/\"([^(\")"]+)\":/g,"$1:");
var transOBJ = JSON.parse(myOBJ);
var conn = $.db.getConnection();
var pstmt;
var query;
var vals = {};
var data = [];
// var query2;
// var output = {results: [] };
//var mynextval = getnextval();
//myDebug.mynextval = mynextval;
try {
// query = "INSERT INTO \"NICEPORTAL\".\"UO_CRM_DATA_BP\" (ID,ID_MSD,TYPE,GROUP,SALUTATION,TITLE,NAME,SURNAME,PHONE,MOBILE_PHONE,EMAIL,ADDRESS_JSON,IBAN,BIC,SEPA_MANDATE_ID,SIGNATURE_DATE,ACCOUNT_OWNER,STREET,STREET_NO,ZIP_CODE,CITY,JSONRAW,CREATE_TS,UPDATE_TS) values (\"NICEPORTAL\".\"SEQ_CRMBP_ID\".NEXTVAL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
query =
"UPSERT \"VIRTAPORTAL\".\"CPO_CHARGES\" (CDRID,UUID,STATIONUUID,EMPSELLERID,STARTTIME,STOPTIME,DURATION,WATTHOUR,AMOUNT,CURRENCY,METHOD,CREATED,RFID,STATION,STATION_ID,STATION_NAME,LOCATION_ID,LOCATION_ADDRESS,LOCATION_CITY,LOCATION_TYPE,ROAMINGEMPID,EVCOID,ORGANIZATIONID,FULLEVSEID,EVSEID,PLUGTYPE,EMPSHARE,SIGNEDMETERVALUEBEGIN,SIGNEDMETERVALUEEND,FREECHARGING,CONNECTORID,METERSTART,METERSTOP,NOTES,MILEAGE,JSONRAW,UPDATE_TS,EICHRECHTTYPE) values (" +
"?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,?) WITH PRIMARY KEY";
// "INSERT INTO \"NICEPORTAL\".\"UO_CRM_DATA_BP\" (ID,ID_MSD,ID_CRM,TYPE,BPGROUP,SALUTATION,TITLE,NAME,SURNAME,PHONE,MOBILE_PHONE,EMAIL,ADDRESS_JSON,IBAN,BIC,SEPA_MANDATE_ID,SIGNATURE_DATE,ACCOUNT_OWNER,STREET,STREET_NO,ZIP_CODE,CITY,JSONRAW,CREATE_TS,UPDATE_TS) values (\"NICEPORTAL\".\"SEQ_CRMBP_ID\".NEXTVAL,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,CURRENT_TIMESTAMP,CURRENT_TIMESTAMP)";
pstmt = conn.prepareStatement(query);
pstmt.setBatchSize(transOBJ.length);
//$.trace.debug("prepareBatchStatement (" + transOBJ.length.toString() + " rows): " + query);
transOBJ.forEach(function(transRow) {
if (!isNaN(parseInt(transRow.cdrId))) {
pstmt.setInteger(1, transRow.cdrId);
} else {
pstmt.setNull(1);
}
if (transRow.UUID){
pstmt.setNString(2, transRow.UUID);
} else {
pstmt.setNull(2);
}
if (!isNaN(parseInt(transRow.locationId))) {
pstmt.setInteger(3, transRow.locationId);
} else {
pstmt.setNull(3);
}
if (!isNaN(parseInt(transRow.empSellerId))) {
pstmt.setInteger(4, transRow.empSellerId);
} else {
pstmt.setNull(4);
}
if (transRow.startTime) {
pstmt.setNString(5, transRow.startTime);
} else {
pstmt.setNull(5);
}
if (transRow.stopTime) {
pstmt.setNString(6, transRow.stopTime);
} else {
pstmt.setNull(6);
}
if (!isNaN(parseInt(transRow.duration))) {
pstmt.setInteger(7, transRow.duration);
} else {
pstmt.setNull(7);
}
if (!isNaN(parseInt(transRow.wattHour))) {
pstmt.setInteger(8, transRow.wattHour);
} else {
pstmt.setNull(8);
}
if (!isNaN(parseInt(transRow.amount))) {
pstmt.setInteger(9, transRow.amount);
} else {
pstmt.setNull(9);
}
pstmt.setNString(10, transRow.currency);
pstmt.setNString(11, transRow.method);
if (transRow.created) {
pstmt.setNString(12, transRow.created);
} else {
pstmt.setNull(12);
}
if (transRow.rfid) {
pstmt.setNString(13, transRow.rfid);
} else {
pstmt.setNull(13);
}
if (transRow.station) {
pstmt.setNClob(14, JSON.stringify(transRow.station));
pstmt.setNString(15, transRow.station.identity);
pstmt.setNString(16, transRow.station.name);
pstmt.setInteger(17, transRow.station.location.id);
pstmt.setNString(18, transRow.station.location.address);
pstmt.setNString(19, transRow.station.location.city);
pstmt.setNString(20, transRow.station.location.locationType);
} else {
pstmt.setNull(14);
pstmt.setNull(15);
pstmt.setNull(16);
pstmt.setNull(17);
pstmt.setNull(18);
pstmt.setNull(19);
pstmt.setNull(20);
}
if (transRow.roamingEmpId) {
pstmt.setNString(21, transRow.roamingEmpId);
} else {
pstmt.setNull(21);
}
pstmt.setNString(22, transRow.evcoId);
pstmt.setInteger(23, transRow.organizationId);
if (transRow.fullEvseId) {
pstmt.setNString(24, transRow.fullEvseId);
} else {
pstmt.setNull(24);
}
if (transRow.evseId) {
pstmt.setNString(25, transRow.evseId.toString());
} else {
pstmt.setNull(25);
}
pstmt.setNString(26, transRow.socketType);
if (!isNaN(parseInt(transRow.empShare))) {
pstmt.setInteger(27, transRow.empShare);
} else {
pstmt.setNull(27);
}
if (transRow.signedMeterValueBegin) {
pstmt.setNString(28, transRow.signedMeterValueBegin);
} else {
pstmt.setNull(28);
}
if (transRow.signedMeterValueEnd) {
pstmt.setNString(29, transRow.signedMeterValueEnd);
} else {
pstmt.setNull(29);
}
pstmt.setNString(30, transRow.freeCharging);
if (!isNaN(parseInt(transRow.connectorId))) {
pstmt.setInteger(31, transRow.connectorId);
} else {
pstmt.setNull(31);
}
if (!isNaN(parseInt(transRow.meterStart))) {
pstmt.setInteger(32, transRow.meterStart);
} else {
pstmt.setNull(32);
}
if (!isNaN(parseInt(transRow.meterStop))) {
pstmt.setInteger(33, transRow.meterStop);
} else {
pstmt.setNull(33);
}
pstmt.setNString(34, JSON.stringify(transRow.notes));
if (transRow.notes && transRow.notes.length > 0) {
pstmt.setInteger(35, transRow.notes[0].value);
} else {
pstmt.setNull(35);
}
//pstmt.setNString(34, "1234");
pstmt.setNString(36, JSON.stringify(transRow));
pstmt.setNString(37, transRow.eichrechtType);
pstmt.addBatch();
});
var batchResult = pstmt.executeBatch();
//$.trace.debug("BatchUpsert Result: " + batchResult.toString());
pstmt.close();
conn.commit();
conn.close();
outMsg = {};
outMsg.method = "insertDB";
outMsg.status = "success";
outMessages.push(outMsg);
} catch (e) {
$.trace.error("InsertDB ExceptionHandler: " + e.toString());
outMsg = {};
outMsg.method = "exceptionHandler";
outMsg.status = "error";
outMsg.message = e.toString();
if (ParDebug) {
outMsg.exceptionInfo = {};
outMsg.exceptionInfo.name = e.name;
outMsg.exceptionInfo.message = e.message;
outMsg.exceptionInfo.fileName = e.fileName;
outMsg.exceptionInfo.lineNumber = e.lineNumber;
outMsg.exceptionInfo.columnNumber = e.columnNumber;
outMsg.exceptionInfo.stack = e.stack;
//outMsg.exceptionInfo.source = e.toSource();
}
outMessages.push(outMsg);
vals.status = 'error';
//vals.description = e.message;
vals.messages = outMessages;
if (ParDebug) {
vals.debug = {};
vals.debug.statement = query;
}
}
}
var content = '';
if ($.request.body) {
content = $.request.body.asString();
}
ParDebug = $.request.parameters.get('debug') && $.request.parameters.get('debug').toLowerCase() === "true";
if ($.request.parameters.get('Billingrunmode') > 0) {
Billingrunmode = $.request.parameters.get('Billingrunmode');
}
if ($.request.parameters.get('DateFrom')) {
DateFrom = $.request.parameters.get('DateFrom');
}
if ($.request.parameters.get('DateTo')) {
DateTo = $.request.parameters.get('DateTo');
}
//doHTTPCallstartBillingrun("FEE","20180101","20180131");
//getVirtaToken("b1_api_user","E3qq$§U,Q3hD#K2}");
getCPOList();
setOutputMessage();