@esri/solution-common
Version:
Provides general helper functions for @esri/solution.js.
212 lines • 9.86 kB
JavaScript
;
/** @license
* Copyright 2021 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.setTrackingOptions = exports.templatizeServiceItemId = exports._setName = exports.templatizeTracker = exports.isTrackingViewGroup = exports.isTrackingViewTemplate = exports.getTackingServiceOwner = exports._validateTrackingTemplates = exports.setLocationTrackingEnabled = void 0;
const generalHelpers_1 = require("./generalHelpers");
const restHelpersGet_1 = require("./restHelpersGet");
const templatization_1 = require("./templatization");
/**
* Used by deploy to evaluate if we have everything we need to deploy tracking views.
*
* This function will update the input templateDictionary with a boolean
* indicating if tracking is enabled on the org and the user is an admin.
*
* @param portalResponse portal self response
* @param userResponse portal user response
* @param templateDictionary Hash of facts: org URL, adlib replacements, deferreds for dependencies
* @param templates the list of IItemTemplates from the solution
*
* @private
*/
function setLocationTrackingEnabled(portalResponse, userResponse, templateDictionary, templates) {
// set locationTracking...contains service url and id
const locationTracking = (0, generalHelpers_1.getProp)(portalResponse, "helperServices.locationTracking");
/* istanbul ignore else */
if (locationTracking) {
templateDictionary.locationTracking = locationTracking;
}
// verify we have location tracking service and the user is an admin
templateDictionary.locationTrackingEnabled =
templateDictionary.locationTracking && (0, generalHelpers_1.getProp)(userResponse, "role") === "org_admin" ? true : false;
if (templates) {
_validateTrackingTemplates(templates, templateDictionary);
}
}
exports.setLocationTrackingEnabled = setLocationTrackingEnabled;
/**
* Used by deploy to evaluate if we have everything we need to deploy tracking views.
*
* An error is thrown to prevent additional deployment work if we have Tracking items and tracking is
* not enabled or the deployment user is not an admin in the organization.
*
* @param templates the list of IItemTemplates from the solution
* @param templateDictionary Hash of facts: org URL, adlib replacements, deferreds for dependencies
*
* @private
*/
function _validateTrackingTemplates(templates, templateDictionary) {
/* istanbul ignore else */
if (!templateDictionary.locationTrackingEnabled &&
templates.some((template) => {
const typeKeywords = (0, generalHelpers_1.getProp)(template, "item.typeKeywords") || [];
return typeKeywords.indexOf("Location Tracking View") > -1;
})) {
console.error("Location tracking not enabled or user is not admin.");
throw new Error("Location tracking not enabled or user is not admin.");
}
}
exports._validateTrackingTemplates = _validateTrackingTemplates;
/**
* Used by deploy to determine the owner of the tracking service.
* Only one tracking service per org and all tracking views and tracking groups must be owned by the tracking service owner.
*
* This function will update the input templateDictionary with the owner as well as
* the item id for the source tracking service.
*
* @param templateDictionary Hash of facts: org URL, adlib replacements, deferreds for dependencies
* @param authentication Credentials for the requests
*
* @private
*/
function getTackingServiceOwner(templateDictionary, authentication) {
if (templateDictionary.locationTrackingEnabled) {
const locationTrackingId = templateDictionary.locationTracking.id;
return (0, restHelpersGet_1.getItemBase)(locationTrackingId, authentication).then((itemBase) => {
templateDictionary.locationTracking.owner = itemBase.owner;
templateDictionary[itemBase.id] = {
itemId: itemBase.id,
};
return Promise.resolve(itemBase && itemBase.owner === authentication.username);
}, () => Promise.resolve(false));
}
else {
return Promise.resolve(false);
}
}
exports.getTackingServiceOwner = getTackingServiceOwner;
/**
* Check key properties to understand if we are dealing with a tracking template
*
* @param itemTemplate the template to evaluate
* @param itemUpdate the item update to evaluate
*
* @private
*/
function isTrackingViewTemplate(itemTemplate, itemUpdate) {
const typeKeywords = (0, generalHelpers_1.getProp)(itemTemplate, "item.typeKeywords") || (0, generalHelpers_1.getProp)(itemUpdate, "typeKeywords");
const trackViewGroup = (0, generalHelpers_1.getProp)(itemTemplate, "item.properties.trackViewGroup") || (0, generalHelpers_1.getProp)(itemUpdate, "properties.trackViewGroup");
return typeKeywords && typeKeywords.indexOf("Location Tracking View") > -1 && trackViewGroup ? true : false;
}
exports.isTrackingViewTemplate = isTrackingViewTemplate;
/**
* Check key properties to understand if we are dealing with a tracking group template
*
* @param itemTemplate the template to evaluate
*
* @private
*/
function isTrackingViewGroup(itemTemplate) {
const typeKeywords = (0, generalHelpers_1.getProp)(itemTemplate, "item.tags");
return typeKeywords && typeKeywords.indexOf("Location Tracking Group") > -1 ? true : false;
}
exports.isTrackingViewGroup = isTrackingViewGroup;
/**
* Templatize the tracker view group id and view name for location tracking views.
* This function will update the itemTemplate that is passed in when it's a tracking view.
*
* @param itemTemplate Template for feature service item
*
* @private
*/
function templatizeTracker(itemTemplate) {
/* istanbul ignore else */
if (isTrackingViewTemplate(itemTemplate)) {
const trackViewGroup = (0, generalHelpers_1.getProp)(itemTemplate, "item.properties.trackViewGroup");
itemTemplate.groups.push(trackViewGroup);
itemTemplate.dependencies.push(trackViewGroup);
const groupIdVar = (0, templatization_1.templatizeTerm)(trackViewGroup, trackViewGroup, ".itemId");
(0, generalHelpers_1.setProp)(itemTemplate, "item.properties.trackViewGroup", groupIdVar);
_setName(itemTemplate, "item.name", trackViewGroup, groupIdVar);
_setName(itemTemplate, "properties.service.adminServiceInfo.name", trackViewGroup, groupIdVar);
const layersAndTables = (itemTemplate.properties.layers || []).concat(itemTemplate.properties.tables || []);
layersAndTables.forEach((l) => {
templatizeServiceItemId(l, "adminLayerInfo.viewLayerDefinition.sourceServiceItemId");
});
}
}
exports.templatizeTracker = templatizeTracker;
/**
* Templatize the tracker view group id and view name for location tracking views.
* This function will update the itemTemplate that is passed in when it's a tracking view.
*
* @param itemTemplate Template for the tracker view
* @param path the path to the property that stores the current name
* @param groupId the id of the associated tracker group
* @param groupIdVar the variable to replace the existing name with
*
* @private
*/
function _setName(itemTemplate, path, groupId, groupIdVar) {
const name = (0, generalHelpers_1.getProp)(itemTemplate, path);
/* istanbul ignore else */
if (name) {
(0, generalHelpers_1.setProp)(itemTemplate, path, name.replace(groupId, groupIdVar));
}
}
exports._setName = _setName;
/**
* Templatize the tracker view serviceItemId
*
* This function will update the input obj with the templatized variable
*
* @param obj the object that stores the serviceItemId
* @param path the path to the property that stores the serviceItemId
*
* @private
*/
function templatizeServiceItemId(obj, path) {
const serviceItemId = (0, generalHelpers_1.getProp)(obj, path);
/* istanbul ignore else */
if (serviceItemId) {
(0, generalHelpers_1.setProp)(obj, path, (0, templatization_1.templatizeTerm)(serviceItemId, serviceItemId, ".itemId"));
}
}
exports.templatizeServiceItemId = templatizeServiceItemId;
/**
* Used by deploy to update the request options with key details for deploying tracker views
*
* @param itemTemplate Template for feature service item
* @param options the current request options to update
* @param templateDictionary Hash of facts: org URL, adlib replacements, deferreds for dependencies
*
* @private
*/
function setTrackingOptions(itemTemplate, options, templateDictionary) {
/* istanbul ignore else */
if (isTrackingViewTemplate(itemTemplate)) {
(0, generalHelpers_1.setCreateProp)(options, "owner", templateDictionary.locationTracking.owner);
(0, generalHelpers_1.setCreateProp)(options.item, "name", itemTemplate.item.name);
(0, generalHelpers_1.setCreateProp)(options.item, "isView", true);
(0, generalHelpers_1.setCreateProp)(options.item, "owner", templateDictionary.locationTracking.owner);
(0, generalHelpers_1.setCreateProp)(options.params, "isView", true);
(0, generalHelpers_1.setCreateProp)(options.params, "outputType", "locationTrackingService");
delete options.folderId;
}
return options.item;
}
exports.setTrackingOptions = setTrackingOptions;
//# sourceMappingURL=trackingHelpers.js.map