UNPKG

@agility/management-sdk

Version:
530 lines (529 loc) 31.1 kB
"use strict"; var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype); return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (g && (g = 0, op[0] && (_ = 0)), _) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; Object.defineProperty(exports, "__esModule", { value: true }); exports.ContentMethods = void 0; var clientInstance_1 = require("./clientInstance"); var batchMethods_1 = require("./batchMethods"); var exception_1 = require("../models/exception"); var queryString_1 = require("../util/queryString"); var batchState_1 = require("../enums/batchState."); var workflowOperationType_1 = require("../enums/workflowOperationType"); var ContentMethods = /** @class */ (function () { function ContentMethods(options) { this._options = options; this._clientInstance = new clientInstance_1.ClientInstance(this._options); this._batchMethods = new batchMethods_1.BatchMethods(this._options); } ContentMethods.prototype.getContentItem = function (contentID, guid, locale) { return __awaiter(this, void 0, void 0, function () { var apiPath, resp, err_1; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); apiPath = "".concat(locale, "/item/").concat(contentID); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); return [2 /*return*/, resp.data]; case 2: err_1 = _a.sent(); throw new exception_1.Exception("Unable to retrieve the content for id: ".concat(contentID), err_1); case 3: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.publishContent = function (contentID_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentID, guid, locale, comments, returnBatchId) { var apiPath, resp, batchID_1, batch, contentIDs_1, err_2; var _this = this; if (comments === void 0) { comments = null; } if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/").concat(contentID, "/publish?comments=").concat(comments); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); batchID_1 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_1]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_1, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_1 = []; batch.items.forEach(function (element) { return contentIDs_1.push(element.itemID); }); return [2 /*return*/, contentIDs_1]; case 3: err_2 = _a.sent(); throw new exception_1.Exception("Unable to publish the content for id: ".concat(contentID), err_2); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.unPublishContent = function (contentID_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentID, guid, locale, comments, returnBatchId) { var apiPath, resp, batchID_2, batch, contentIDs_2, err_3; var _this = this; if (comments === void 0) { comments = null; } if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/").concat(contentID, "/unpublish?comments=").concat(comments); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); batchID_2 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_2]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_2, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_2 = []; batch.items.forEach(function (element) { return contentIDs_2.push(element.itemID); }); return [2 /*return*/, contentIDs_2]; case 3: err_3 = _a.sent(); throw new exception_1.Exception("Unable to un-publish the content for id: ".concat(contentID), err_3); case 4: return [2 /*return*/]; } }); }); }; /** * Perform a batch workflow operation on multiple content items. * Supports Publish, Unpublish, Approve, Decline, and RequestApproval operations. * @param contentIDs Array of content IDs to process * @param guid The GUID of the user making the request * @param locale The locale of the content items * @param operation The workflow operation type (Publish, Unpublish, Approve, Decline, RequestApproval) * @param returnBatchId Whether to return the batch ID immediately * @returns The IDs of the processed content items */ ContentMethods.prototype.batchWorkflowContent = function (contentIDs_3, guid_1, locale_1, operation_1) { return __awaiter(this, arguments, void 0, function (contentIDs, guid, locale, operation, returnBatchId) { var contentIDsParam, operationName, apiPath, resp, batchID_3, batch, processedContentIDs_1, err_4; var _this = this; if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); contentIDsParam = contentIDs.join(','); operationName = workflowOperationType_1.WorkflowOperationType[operation]; apiPath = "".concat(locale, "/item/batch-workflow?contentIDs=").concat(contentIDsParam, "&operation=").concat(operationName); return [4 /*yield*/, this._clientInstance.executePost(apiPath, guid, this._options.token, null)]; case 1: resp = _a.sent(); batchID_3 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_3]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_3, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); if (batch.batchState === batchState_1.BatchState.Processed && batch.errorData && batch.errorData.length > 0) { throw new exception_1.Exception("Unable to batch ".concat(operationName.toLowerCase(), " content items. Batch is not completed. Error: ").concat(batch.errorData), null); } processedContentIDs_1 = []; batch.items.forEach(function (element) { return processedContentIDs_1.push(element.itemID); }); return [2 /*return*/, processedContentIDs_1]; case 3: err_4 = _a.sent(); throw new exception_1.Exception("Unable to batch ".concat(workflowOperationType_1.WorkflowOperationType[operation].toLowerCase(), " content items."), err_4); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.contentRequestApproval = function (contentID_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentID, guid, locale, comments, returnBatchId) { var apiPath, resp, batchID_4, batch, contentIDs_3, err_5; var _this = this; if (comments === void 0) { comments = null; } if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/").concat(contentID, "/request-approval?comments=").concat(comments); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); batchID_4 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_4]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_4, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_3 = []; batch.items.forEach(function (element) { return contentIDs_3.push(element.itemID); }); return [2 /*return*/, contentIDs_3]; case 3: err_5 = _a.sent(); throw new exception_1.Exception("Unable to request approval the content for id: ".concat(contentID), err_5); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.approveContent = function (contentID_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentID, guid, locale, comments, returnBatchId) { var apiPath, resp, batchID_5, batch, contentIDs_4, err_6; var _this = this; if (comments === void 0) { comments = null; } if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/").concat(contentID, "/approve?comments=").concat(comments); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); batchID_5 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_5]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_5, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_4 = []; batch.items.forEach(function (element) { return contentIDs_4.push(element.itemID); }); return [2 /*return*/, contentIDs_4]; case 3: err_6 = _a.sent(); throw new exception_1.Exception("Unable to approve the content for id: ".concat(contentID), err_6); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.declineContent = function (contentID_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentID, guid, locale, comments, returnBatchId) { var apiPath, resp, batchID_6, batch, contentIDs_5, err_7; var _this = this; if (comments === void 0) { comments = null; } if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/").concat(contentID, "/decline?comments=").concat(comments); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); batchID_6 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_6]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_6, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_5 = []; batch.items.forEach(function (element) { return contentIDs_5.push(element.itemID); }); return [2 /*return*/, contentIDs_5]; case 3: err_7 = _a.sent(); throw new exception_1.Exception("Unable to decline the content for id: ".concat(contentID), err_7); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.deleteContent = function (contentID_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentID, guid, locale, comments, returnBatchId) { var apiPath, resp, batchID_7, batch, contentIDs_6, err_8; var _this = this; if (comments === void 0) { comments = null; } if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/").concat(contentID, "?comments=").concat(comments); return [4 /*yield*/, this._clientInstance.executeDelete(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); batchID_7 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_7]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_7, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_6 = []; batch.items.forEach(function (element) { return contentIDs_6.push(element.itemID); }); return [2 /*return*/, contentIDs_6]; case 3: err_8 = _a.sent(); throw new exception_1.Exception("Unable to delete the content for id: ".concat(contentID), err_8); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.saveContentItem = function (contentItem_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentItem, guid, locale, returnBatchId) { var apiPath, resp, batchID_8, batch, contentIDs_7, err_9; var _this = this; if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item"); return [4 /*yield*/, this._clientInstance.executePost(apiPath, guid, this._options.token, contentItem)]; case 1: resp = _a.sent(); batchID_8 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_8]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_8, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_7 = []; batch.items.forEach(function (element) { return contentIDs_7.push(element.itemID); }); return [2 /*return*/, contentIDs_7]; case 3: err_9 = _a.sent(); throw new exception_1.Exception('Unable to create content.', err_9); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.saveContentItems = function (contentItems_1, guid_1, locale_1) { return __awaiter(this, arguments, void 0, function (contentItems, guid, locale, returnBatchId) { var apiPath, resp, batchID_9, batch, contentIDs_8, err_10; var _this = this; if (returnBatchId === void 0) { returnBatchId = false; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 3, , 4]); apiPath = "".concat(locale, "/item/multi"); return [4 /*yield*/, this._clientInstance.executePost(apiPath, guid, this._options.token, contentItems)]; case 1: resp = _a.sent(); batchID_9 = resp.data; // If user wants batchID immediately, return it for custom polling if (returnBatchId) { return [2 /*return*/, [batchID_9]]; } return [4 /*yield*/, this._batchMethods.Retry(function () { return __awaiter(_this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, this._batchMethods.getBatch(batchID_9, guid)]; case 1: return [2 /*return*/, _a.sent()]; } }); }); })]; case 2: batch = _a.sent(); contentIDs_8 = []; batch.items.forEach(function (element) { return contentIDs_8.push(element.itemID); }); return [2 /*return*/, contentIDs_8]; case 3: err_10 = _a.sent(); throw new exception_1.Exception('Unable to create contents.', err_10); case 4: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.getContentItems = function (referenceName, guid, locale, listParams) { return __awaiter(this, void 0, void 0, function () { var queryParams, apiPath, resp, err_11; return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); queryParams = (0, queryString_1.buildQueryString)({ filter: listParams.filter, fields: listParams.fields, sortDirection: listParams.sortDirection, sortField: listParams.sortField, take: listParams.take, skip: listParams.skip }); apiPath = "".concat(locale, "/list/").concat(referenceName).concat(queryParams); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); return [2 /*return*/, resp.data]; case 2: err_11 = _a.sent(); throw new exception_1.Exception("Unable retrieve the content details for reference name: ".concat(referenceName), err_11); case 3: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.getContentList = function (referenceName_1, guid_1, locale_1, listParams_1) { return __awaiter(this, arguments, void 0, function (referenceName, guid, locale, listParams, filterObject) { var queryParams, apiPath, resp, err_12; if (filterObject === void 0) { filterObject = null; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); queryParams = (0, queryString_1.buildQueryString)({ fields: listParams.fields, sortDirection: listParams.sortDirection, sortField: listParams.sortField, take: listParams.take, skip: listParams.skip, showDeleted: listParams.showDeleted }); apiPath = "".concat(locale, "/list/").concat(referenceName).concat(queryParams); return [4 /*yield*/, this._clientInstance.executePost(apiPath, guid, this._options.token, filterObject)]; case 1: resp = _a.sent(); return [2 /*return*/, resp.data]; case 2: err_12 = _a.sent(); throw new exception_1.Exception("Unable retrieve the content details for list with reference name: ".concat(referenceName), err_12); case 3: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.getContentHistory = function (locale_1, guid_1, contentID_1) { return __awaiter(this, arguments, void 0, function (locale, guid, contentID, take, skip) { var apiPath, resp, err_13; if (take === void 0) { take = 50; } if (skip === void 0) { skip = 0; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); apiPath = "".concat(locale, "/item/").concat(contentID, "/history?take=").concat(take, "&skip=").concat(skip); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); return [2 /*return*/, resp.data]; case 2: err_13 = _a.sent(); throw new exception_1.Exception("Unable to retrieve history for contentID: ".concat(contentID)); case 3: return [2 /*return*/]; } }); }); }; ContentMethods.prototype.getContentComments = function (locale_1, guid_1, contentID_1) { return __awaiter(this, arguments, void 0, function (locale, guid, contentID, take, skip) { var apiPath, resp, err_14; if (take === void 0) { take = 50; } if (skip === void 0) { skip = 0; } return __generator(this, function (_a) { switch (_a.label) { case 0: _a.trys.push([0, 2, , 3]); apiPath = "".concat(locale, "/item/").concat(contentID, "/comments?take=").concat(take, "&skip=").concat(skip); return [4 /*yield*/, this._clientInstance.executeGet(apiPath, guid, this._options.token)]; case 1: resp = _a.sent(); return [2 /*return*/, resp.data]; case 2: err_14 = _a.sent(); throw new exception_1.Exception("Unable to retrieve comments for contentID: ".concat(contentID)); case 3: return [2 /*return*/]; } }); }); }; return ContentMethods; }()); exports.ContentMethods = ContentMethods;