UNPKG

@esri/solution-common

Version:

Provides general helper functions for @esri/solution.js.

176 lines 6.98 kB
"use strict"; /** @license * Copyright 2024 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. * * @module item-reuse */ Object.defineProperty(exports, "__esModule", { value: true }); exports.getItemHash = exports.findReusableSolutionsAndItems = exports.getIdsFromSolutionTemplates = exports.getSolutionItemsFromDeployedSolutions = exports.getDeployedSolutions = exports.getDeployedSolutionsAndItems = void 0; const arcgisRestJS_1 = require("./arcgisRestJS"); /** * Find all deployed solutions and their items from the current org for the current user * * @param authentication Credentials for the request */ function getDeployedSolutionsAndItems(authentication) { return getDeployedSolutions(authentication).then((searchResults) => { return getSolutionItemsFromDeployedSolutions(authentication, searchResults); }); } exports.getDeployedSolutionsAndItems = getDeployedSolutionsAndItems; /** * Find all deployed solutions from the current org for the current user * * @param authentication Credentials for the request */ function getDeployedSolutions(authentication) { const query = new arcgisRestJS_1.SearchQueryBuilder() .match(authentication.username) .in("owner") .and() .match("Solution") .in("type") .and() .match("Deployed") .in("typekeywords"); return (0, arcgisRestJS_1.restSearchItems)({ q: query, num: 100, authentication, }).then((searchResponse) => { // Sort the results by title and then id searchResponse.results.sort((e1, e2) => { if (e1.title !== e2.title) { return e1.title < e2.title ? -1 : 1; } else { return e1.id < e2.id ? -1 : 1; } }); return searchResponse; }); } exports.getDeployedSolutions = getDeployedSolutions; /** * Find key details for the items from each of the deployed solutions * * @param authentication Credentials for the request * @param searchResults key details of the deployed solutions */ function getSolutionItemsFromDeployedSolutions(authentication, searchResults) { const promises = []; const itemIds = []; const solutions = {}; if (searchResults.results.length > 0) { searchResults.results.forEach((r) => { const versionKeywords = r.typeKeywords.filter((v) => /solutionversion-.+/.exec(v)); const version = versionKeywords.length > 0 ? versionKeywords[0] : ""; itemIds.push(r.id); solutions[r.id] = { created: r.created, title: r.title, version, }; promises.push((0, arcgisRestJS_1.getItemData)(r.id, { authentication })); }); } return Promise.all(promises).then((results) => { return results.reduce((prev, cur, i) => { const id = itemIds[i]; prev[id] = { templates: cur.templates.map((template) => template.itemId), solutionInfo: solutions[id], }; return prev; }, {}); }); } exports.getSolutionItemsFromDeployedSolutions = getSolutionItemsFromDeployedSolutions; /** * Get the ids for each template in a solution * * @param authentication Credentials for the request */ function getIdsFromSolutionTemplates(id, authentication) { return (0, arcgisRestJS_1.getItemData)(id, { authentication }).then((data) => { return data.templates.map((t) => t.itemId); }); } exports.getIdsFromSolutionTemplates = getIdsFromSolutionTemplates; /** * Fetch key details for the solution that will be deployed and find any solutions * that leverage any of the source items that exist in the solution to be deployed. * * @param id The id of the solution that will be deployed * @param authentication Credentials for the request */ function findReusableSolutionsAndItems(id, authentication) { return getItemHash(id, authentication).then((itemHash) => { return getDeployedSolutionsAndItems(authentication).then((results) => { const sourceIds = Object.keys(itemHash); Object.keys(results).forEach((solutionId) => { const solution = results[solutionId]; sourceIds.forEach((sourceId) => { const itemKeys = Object.keys(itemHash[sourceId]); itemKeys.forEach((deployedId) => { const item = itemHash[sourceId][deployedId]; if (solution.templates.indexOf(deployedId) > -1 && Object.keys(item.solutions).indexOf(solutionId) < 0) { item.solutions[solutionId] = solution.solutionInfo; } }); }); }); return itemHash; }); }); } exports.findReusableSolutionsAndItems = findReusableSolutionsAndItems; /** * Fetch key details for the solution that will be deployed * * @param id The id of the solution that will be deployed * @param authentication Credentials for the request */ function getItemHash(id, authentication) { return getIdsFromSolutionTemplates(id, authentication).then((ids) => { // search for existing items that reference any of these ids in their typeKeywords const promises = ids.map((id) => { const q = `typekeywords:source-${id} owner:${authentication.username}`; const searchOptions = { q, authentication, }; return (0, arcgisRestJS_1.restSearchItems)(searchOptions); }); return Promise.all(promises).then((results) => { // if we have a result from the typeKeyword search we need to understand what solution it came from and what its id is return results.reduce((prev, cur, i) => { // key is source id and value is any ids for items that were deployed based on this source prev[ids[i]] = cur.results.reduce((prev, cur) => { prev[cur.id] = { created: cur.created, solutions: {}, title: cur.title, type: cur.type, }; return prev; }, {}); return prev; }, {}); }); }); } exports.getItemHash = getItemHash; //# sourceMappingURL=item-reuse.js.map