@esri/solution-common
Version:
Provides general helper functions for @esri/solution.js.
117 lines • 5.83 kB
JavaScript
;
/** @license
* Copyright 2018 Esri
*
* 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
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.removeItems = void 0;
const tslib_1 = require("tslib");
/**
* @module removeItems
*/
const interfaces_1 = require("../interfaces");
const reportProgress = tslib_1.__importStar(require("./reportProgress"));
const hubSites = tslib_1.__importStar(require("@esri/hub-sites"));
const arcgisRestJS_1 = require("../arcgisRestJS");
const restHelpers_1 = require("../restHelpers");
const workflowHelpers = tslib_1.__importStar(require("../workflowHelpers"));
const create_hub_request_options_1 = require("../create-hub-request-options");
// ------------------------------------------------------------------------------------------------------------------ //
/**
* Removes a list of items starting from the end.
*
* @param itemIds List of ids of items to remove
* @param hubSiteItemIds List of ids in itemIds that are for Hub Sites
* @param authentication Credentials for the request
* @param percentDone Percent done in range 0 to 100
* @param progressPercentStep Amount that percentDone changes for each item deleted
* @param solutionDeletedSummary Solution summary containing items successfully deleted
* @param solutionFailureSummary Solution summary containing items that could not be deleted
* @param deleteOptions Progress reporting and deletion permanence options
* @returns Promise that will resolve with a list of two solution summaries: successful deletions
* and failed deletions. Ignored items (e.g., already deleted) will not be in either list.
*/
function removeItems(solutionSummary, hubSiteItemIds, authentication, percentDone, progressPercentStep, deleteOptions = {}) {
let solutionDeletedSummary;
let solutionFailureSummary;
const itemToDelete = solutionSummary.items.shift();
const percentDoneReport = percentDone + progressPercentStep * (solutionSummary.items.length + 1);
if (itemToDelete) {
// On to next item in list
return removeItems(solutionSummary, hubSiteItemIds, authentication, percentDone, progressPercentStep, deleteOptions)
.then((results) => {
// Done with subsequent items in list; now delete the current item
[solutionDeletedSummary, solutionFailureSummary] = results;
// Remove any delete protection on item
return (0, arcgisRestJS_1.unprotectItem)({
id: itemToDelete.id,
authentication: authentication,
});
})
.then(async () => {
// Delete the item
if (hubSiteItemIds.includes(itemToDelete.id)) {
const options = await (0, create_hub_request_options_1.createHubRequestOptions)(authentication);
return hubSites.removeSite(itemToDelete.id, options);
}
else if (itemToDelete.type === "Workflow") {
const workflowBaseUrl = await workflowHelpers.getWorkflowBaseURL(authentication);
return workflowHelpers.deleteWorkflowItem(itemToDelete.id, workflowBaseUrl, authentication);
}
else {
const permanentDelete = !deleteOptions.sendToRecycling;
return (0, restHelpers_1.removeItem)(itemToDelete.id, authentication, permanentDelete);
}
})
.then(() => {
// Successful deletion
solutionDeletedSummary.items.push(itemToDelete);
reportProgress.reportProgress(percentDoneReport, deleteOptions, itemToDelete.id, interfaces_1.EItemProgressStatus.Finished);
return [solutionDeletedSummary, solutionFailureSummary];
})
.catch((error) => {
const errorMessage = error.error?.message || error.message;
if (errorMessage && errorMessage.includes("Item does not exist or is inaccessible")) {
// Filter out errors where the item doesn't exist, such as from a previous delete attempt
reportProgress.reportProgress(percentDoneReport, deleteOptions, itemToDelete.id, interfaces_1.EItemProgressStatus.Ignored);
}
else {
// Otherwise, we have a real delete error, including where AGO simply returns "success: false"
solutionFailureSummary.items.push(itemToDelete);
reportProgress.reportProgress(percentDoneReport, deleteOptions, itemToDelete.id, interfaces_1.EItemProgressStatus.Failed);
}
return [solutionDeletedSummary, solutionFailureSummary];
});
}
else {
// We've exhausted our list of items; start building up the lists of results
solutionDeletedSummary = {
id: solutionSummary.id,
title: solutionSummary.title,
folder: solutionSummary.folder,
items: [],
groups: [],
};
solutionFailureSummary = {
id: solutionSummary.id,
title: solutionSummary.title,
folder: solutionSummary.folder,
items: [],
groups: [],
};
return Promise.resolve([solutionDeletedSummary, solutionFailureSummary]);
}
}
exports.removeItems = removeItems;
//# sourceMappingURL=removeItems.js.map