UNPKG

@petarmihaylov/node-raas

Version:

A tiny library and CLI for interacting with the Reports as a Service - RAAS - API from UKG - Ultimate Kronos Group. This project is maintained by the team behind RaasTastic and the community. It provides a balanced set of features that should suit a broad

278 lines (277 loc) 10.7 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.logOff = exports.retrieveReport = exports.executeReport = exports.getReportParameters = exports.login = exports.addActionHeader = exports.addAddressingHeader = exports.config = exports.RaasMethods = void 0; const tslib_1 = require("tslib"); /* eslint-disable new-cap */ const soap = tslib_1.__importStar(require("soap")); const uuid = tslib_1.__importStar(require("uuid")); const __1 = require(".."); var RaasMethods; (function (RaasMethods) { RaasMethods["LogOn"] = "LogOn"; RaasMethods["GetReportParameters"] = "GetReportParameters"; RaasMethods["ExecuteReport"] = "ExecuteReport"; RaasMethods["RetrieveReport"] = "RetrieveReport"; RaasMethods["LogOff"] = "LogOff"; })(RaasMethods = exports.RaasMethods || (exports.RaasMethods = {})); async function config(baseEndpoint) { const url = `https://${baseEndpoint}/services/BIDataService`; const executeClient = await soap.createClientAsync(url, { forceSoap12Headers: true, escapeXML: false, // Required to ensure that when using reeport paths, the quotes for folder and report names are not escapepd }); executeClient.addHttpHeader('Content-Type', 'application/soap+xml'); // ╔═╗┌┬┐┬─┐┌─┐┌─┐┌┬┐┬┌┐┌┌─┐ ╔═╗┬ ┬┌─┐┌┐┌┌┬┐ ╔═╗┌─┐┌┐┌┌─┐┬┌─┐ // ╚═╗ │ ├┬┘├┤ ├─┤││││││││ ┬ ║ │ │├┤ │││ │ ║ │ ││││├┤ ││ ┬ // ╚═╝ ┴ ┴└─└─┘┴ ┴┴ ┴┴┘└┘└─┘ ╚═╝┴─┘┴└─┘┘└┘ ┴ ╚═╝└─┘┘└┘└ ┴└─┘ const streamingServiceUrl = `https://${baseEndpoint}/services/BIStreamingService`; const streamClient = await soap.createClientAsync(streamingServiceUrl, { forceSoap12Headers: true, escapeXML: false, // Required to ensure that when using reeport paths, the quotes for folder and report names are not escapepd }); streamClient.addHttpHeader('Content-Type', 'application/soap+xml'); streamClient.wsdl.xmlnsInEnvelope += ' xmlns:h="http://www.ultipro.com/dataservices/bistream/2"'; return { executeClient, streamClient, }; } exports.config = config; function addAddressingHeader(soapClient) { if (soapClient.wsdl.xmlnsInEnvelope.search('xmlns:a="http://www.w3.org/2005/08/addressing"') === -1) { soapClient.wsdl.xmlnsInEnvelope += ' xmlns:a="http://www.w3.org/2005/08/addressing"'; } } exports.addAddressingHeader = addAddressingHeader; function addActionHeader(clients, method) { switch (method) { case RaasMethods.LogOn: case RaasMethods.GetReportParameters: case RaasMethods.ExecuteReport: case RaasMethods.LogOff: clients.executeClient.clearSoapHeaders(); // This is a required header for RaaS clients.executeClient.addSoapHeader({ 'a:Action': `http://www.ultipro.com/dataservices/bidata/2/IBIDataService/${method}`, }); break; case RaasMethods.RetrieveReport: clients.streamClient.clearSoapHeaders(); // This is a required header for RaaS clients.streamClient.addSoapHeader({ 'a:Action': `http://www.ultipro.com/dataservices/bistream/2/IBIStreamService/${method}`, }); } } exports.addActionHeader = addActionHeader; async function login(clients, raasCredential) { // This is a required xmlns for RaaS addAddressingHeader(clients.executeClient); addActionHeader(clients, RaasMethods.LogOn); // perform a login const logOnCorrelationId = uuid.v4(); clients.executeClient.addHttpHeader('US-CORRELATION-ID', logOnCorrelationId); const logOnResponse = await clients.executeClient .LogOnAsync({ logOnRequest: raasCredential, }, // We can pass in the generated correlationId to the client so our internal 'exchangeIds' match the // US-CORRELATION-ID passed to RaaS. { exchangeId: logOnCorrelationId, }) .then((result) => { // console.log(result); // console.log(result[0].LogOnResult); // Logon result // return result[0].LogOnResult; return result; }) .catch((error) => { throw new Error(error); }); const objToReturn = { correlationId: logOnCorrelationId, result: logOnResponse, }; if (logOnResponse[0].LogOnResult.Status === 'Ok') { return { hasErrors: false, ...objToReturn, }; } return { hasErrors: true, ...objToReturn, }; } exports.login = login; async function getReportParameters(clients, logOnResult, reportPathOrId) { var _a, _b; // This is a required xmlns for RaaS addAddressingHeader(clients.executeClient); addActionHeader(clients, RaasMethods.GetReportParameters); const getReportParametersArgs = { reportPath: reportPathOrId, context: logOnResult, }; const getReportParametersCorrelationId = uuid.v4(); clients.executeClient.addHttpHeader('US-CORRELATION-ID', getReportParametersCorrelationId); const getReportParametersRunResult = await clients.executeClient .GetReportParametersAsync(getReportParametersArgs) .then((result) => { return result; }) .catch((error) => { throw new Error(error); }); let requiredParams = []; if (getReportParametersRunResult[0].GetReportParametersResult.Status === 'Success') { requiredParams = ((_b = (_a = getReportParametersRunResult[0].GetReportParametersResult.ReportParameters) === null || _a === void 0 ? void 0 : _a.ReportParameter) === null || _b === void 0 ? void 0 : _b.filter((element) => { return element.Required === true; })) || []; } const objToReturn = { correlationId: getReportParametersCorrelationId, requiredParams, result: getReportParametersRunResult, }; return { // Indicate errors if Status !== "Success" hasErrors: getReportParametersRunResult[0].GetReportParametersResult.Status !== 'Success', // If we do have an error, report it errorMessage: getReportParametersRunResult[0].GetReportParametersResult.Status === 'Success' ? '' : getReportParametersRunResult[0].GetReportParametersResult .StatusMessage, // Indicate warnings if there are any required parameters hasWarnings: requiredParams.length > 0, // Pass along the required parameters are not supported message warningMessage: requiredParams.length > 0 ? __1.notSupported.reportWithRequiredParemeters : '', ...objToReturn, }; } exports.getReportParameters = getReportParameters; async function executeReport(clients, logOnResult, reportPathOrId) { // This is a required xmlns for RaaS addAddressingHeader(clients.executeClient); addActionHeader(clients, RaasMethods.ExecuteReport); const executeReportArgs = { request: { ReportPath: reportPathOrId, ReportParameters: [], }, context: logOnResult, }; const executeReportCorrelationId = uuid.v4(); clients.executeClient.addHttpHeader('US-CORRELATION-ID', executeReportCorrelationId); const executeReportRunResult = await clients.executeClient .ExecuteReportAsync(executeReportArgs) .then((result) => { return result; }) .catch((error) => { throw new Error(error); }); const objToReturn = { correlationId: executeReportCorrelationId, result: executeReportRunResult, }; if (executeReportRunResult[0].ExecuteReportResult.Status === 'Success') { return { hasErrors: false, ...objToReturn, }; } return { hasErrors: true, ...objToReturn, }; } exports.executeReport = executeReport; async function retrieveReport(clients, executeReportResult) { var _a; addAddressingHeader(clients.streamClient); addActionHeader(clients, RaasMethods.RetrieveReport); clients.streamClient.addSoapHeader({ 'h:ReportKey': executeReportResult.ReportKey, }); const retrieveReportCorrelationId = uuid.v4(); clients.streamClient.addHttpHeader('US-CORRELATION-ID', retrieveReportCorrelationId); const retrieveReportRunResult = await clients.streamClient .RetrieveReportAsync() .then((result) => { return result; }) .catch((error) => { throw new Error(error); }); const objToReturn = { correlationId: retrieveReportCorrelationId, result: retrieveReportRunResult, }; if (((_a = retrieveReportRunResult[2]) === null || _a === void 0 ? void 0 : _a.Status) === 'Working') { return { hasErrors: false, hasWarnings: true, warningMessage: __1.notSupported.longRunningReports, ...objToReturn, }; } if (retrieveReportRunResult[2].Status === 'Failed') { return { hasErrors: true, errorMessage: retrieveReportRunResult[2].StatusMessage, hasWarnings: false, ...objToReturn, }; } return { hasErrors: false, hasWarnings: false, ...objToReturn, }; } exports.retrieveReport = retrieveReport; async function logOff(clients, logOnResult) { addAddressingHeader(clients.executeClient); clients.executeClient.clearSoapHeaders(); clients.executeClient.addSoapHeader({ 'a:Action': 'http://www.ultipro.com/dataservices/bidata/2/IBIDataService/LogOff', }); const logOffCorrelationId = uuid.v4(); clients.executeClient.addHttpHeader('US-CORRELATION-ID', logOffCorrelationId); const logOffRunResult = await clients.executeClient .LogOffAsync({ context: logOnResult, }) .then((result) => { return result; }) .catch((error) => { throw new Error(error); }); const objToReturn = { correlationId: logOffCorrelationId, result: logOffRunResult, }; if (logOffRunResult[0].LogOffResult.Status === 'LoggedOff') { return { hasErrors: false, ...objToReturn, }; } return { hasErrors: true, ...objToReturn, }; } exports.logOff = logOff;