UNPKG

@allangsilva/intacct-sdk

Version:

Patched fork of Intacct SDK JS with Node 24 compatibility

154 lines 6.25 kB
"use strict"; /** * @module Intacct/SDK/Xml/Response */ Object.defineProperty(exports, "__esModule", { value: true }); /** * Copyright 2022 Sage Intacct, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"). You may not * use this file except in compliance with the License. You may obtain a copy * of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * or in the "LICENSE" file accompanying this file. This file is distributed on * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either * express or implied. See the License for the specific language governing * permissions and limitations under the License. */ const util_1 = require("util"); const IntacctException_1 = require("../../Exceptions/IntacctException"); const ResultException_1 = require("../../Exceptions/ResultException"); const ErrorMessage_1 = require("./ErrorMessage"); class Result { get status() { return this._status; } get functionName() { return this._functionName; } get controlId() { return this._controlId; } get data() { return this._data; } get listType() { return this._listType; } get count() { return this._count; } get totalCount() { return this._totalCount; } get numRemaining() { return this._numRemaining; } get resultId() { return this._resultId; } get key() { return this._key; } get start() { return this._start; } get end() { return this._end; } get errors() { return this._errors; } constructor(result) { if (!Object.prototype.hasOwnProperty.call(result, "status")) { throw new IntacctException_1.default("Response result block is missing status element"); } this._status = result["status"]; if (!Object.prototype.hasOwnProperty.call(result, "function")) { throw new IntacctException_1.default("Response result block is missing function element"); } this._functionName = result["function"]; if (!Object.prototype.hasOwnProperty.call(result, "controlid")) { throw new IntacctException_1.default("Response result block is missing controlid element"); } this._controlId = result["controlid"]; if (result["status"] !== "success") { if (Object.prototype.hasOwnProperty.call(result, "errormessage")) { const errorMessage = new ErrorMessage_1.default(result["errormessage"]); this._errors = errorMessage.errors; } } else { if (Object.prototype.hasOwnProperty.call(result, "key") && result["key"] != null) { this._key = result["key"].toString(); } else if (Object.prototype.hasOwnProperty.call(result, "listtype") && result["listtype"] != null) { const listType = result["listtype"]; this._listType = result["listtype"].toString(); if (Object.prototype.hasOwnProperty.call(listType, ["$"])) { const listTypeAttr = listType["$"]; if (Object.prototype.hasOwnProperty.call(listTypeAttr, "total")) { this._totalCount = parseInt(listTypeAttr["total"], 10); } if (Object.prototype.hasOwnProperty.call(listTypeAttr, "start")) { this._start = parseInt(listTypeAttr["start"], 10); } if (Object.prototype.hasOwnProperty.call(listTypeAttr, "end")) { this._end = parseInt(listTypeAttr["end"], 10); } } } else if (Object.prototype.hasOwnProperty.call(result, "data") && Object.prototype.hasOwnProperty.call(result["data"], "$") && Object.prototype.hasOwnProperty.call(result["data"]["$"], "listtype") && result["data"]["$"]["listtype"] != null) { const dataAttr = result["data"]["$"]; if (Object.prototype.hasOwnProperty.call(dataAttr, "listtype")) { this._listType = dataAttr["listtype"].toString(); } if (Object.prototype.hasOwnProperty.call(dataAttr, "totalcount")) { this._totalCount = parseInt(dataAttr["totalcount"], 10); } if (Object.prototype.hasOwnProperty.call(dataAttr, "count")) { this._count = parseInt(dataAttr["count"], 10); } if (Object.prototype.hasOwnProperty.call(dataAttr, "numremaining")) { this._numRemaining = parseInt(dataAttr["numremaining"], 10); } if (Object.prototype.hasOwnProperty.call(dataAttr, "resultId")) { this._resultId = dataAttr["resultId"].toString(); } } if (Object.prototype.hasOwnProperty.call(result, "data")) { const data = []; for (const key in result["data"]) { if (key !== "$" && Object.prototype.hasOwnProperty.call(result["data"], key)) { if ((0, util_1.isArray)(result["data"][key])) { for (const child of result["data"][key]) { data.push(child); } } else { data.push(result["data"][key]); } } } this._data = data; } } } ensureStatusSuccess() { if (this.status !== "success") { throw new ResultException_1.default("Result status: " + this.status + " for Control ID: " + this.controlId, this._errors); } } ensureStatusNotFailure() { if (this.status === "failure") { throw new ResultException_1.default("Result status: " + this.status + " for Control ID: " + this.controlId, this._errors); } } } exports.default = Result; //# sourceMappingURL=Result.js.map