UNPKG

salesforce-alm

Version:

This package contains tools, and APIs, for an improved salesforce.com developer experience.

129 lines (127 loc) 4.99 kB
"use strict"; /* * Copyright (c) 2020, salesforce.com, inc. * All rights reserved. * Licensed under the BSD 3-Clause license. * For full license text, see LICENSE.txt file in the repo root or https://opensource.org/licenses/BSD-3-Clause */ Object.defineProperty(exports, "__esModule", { value: true }); exports.ShapeRepresentationApi = void 0; const core_1 = require("@salesforce/core"); core_1.Messages.importMessagesDirectory(__dirname); const messages = core_1.Messages.loadMessages('salesforce-alm', 'org_shape'); /** * Shape API object, for all of your ShapeRepresentation needs * * @constructor * @param shapeOrg The org we'll be querying against */ class ShapeRepresentationApi { constructor(shapeOrg) { this.conn = shapeOrg.getConnection(); } async create(description = '') { try { return this.conn.sobject('ShapeRepresentation').create({ Description: description, }); } catch (err) { if (err.errorCode && err.errorCode === 'NOT_FOUND' && err['name'] === 'ACCESS_DENIED') { throw core_1.SfdxError.wrap(messages.getMessage('create_shape_command_no_crud_access')); } else { throw err; } } } /** * Delete all ShapeRepresentation records for the shapeOrg. * * @return List of SR IDs that were deleted */ async deleteAll() { let shapeIds = []; try { const result = await this.conn.query('SELECT Id FROM ShapeRepresentation'); shapeIds = result.records.map((shape) => shape.Id); } catch (err) { if (err.errorCode && err.errorCode === 'INVALID_TYPE') { // ShapeExportPref is not enabled, or user does not have CRUD access throw core_1.SfdxError.wrap(messages.getMessage('delete_shape_command_no_access', shapeIds)); } // non-access error throw err; } return Promise.all(shapeIds.map(async (id) => { try { const delResult = await this.conn.sobject('ShapeRepresentation').delete(id); if (delResult.success) { return delResult.id; } } catch (err) { return Promise.reject(err); } })); } /** * Find all ShapeRepresentation records with a state of Active or InProgress. * * @return SOQL response or null */ // eslint-disable-next-line @typescript-eslint/require-await async findShapesOrNull() { try { const results = await this.conn.query("SELECT Id, Status, CreatedBy.Username, CreatedDate FROM ShapeRepresentation WHERE Status IN ( 'Active', 'InProgress' )"); return results.records; } catch (err) { if (err.errorCode && err.errorCode === 'INVALID_TYPE') { // ShapeExportPref is not enabled return []; } // some important error throw err; } } /** * Check if the ShapeExportPilot preference is enabled. */ async isFeatureEnabled() { const aggregator = await core_1.ConfigAggregator.create(); if (aggregator.getInfo('apiVersion').value < 48) { return this.isFeatureEnabledBefore48(); } else { return this.isFeatureEnabledAfter48(); } } async isFeatureEnabledBefore48() { var _a, _b; const prefValue = await this.conn.tooling.query(`SELECT SettingValue FROM ${'OrganizationSettingsDetail'} WHERE SettingName = 'ShapeExportPref'`); return prefValue.totalSize > 0 && ((_b = (_a = prefValue.records) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.SettingValue); } async isFeatureEnabledAfter48() { var _a, _b; const prefValue = await this.conn.tooling.query(`SELECT IsShapeExportPrefEnabled FROM ${'DevHubSettings'}`); // no records are returned if ShapeExportPilot perm is disabled return prefValue.totalSize > 0 && ((_b = (_a = prefValue.records) === null || _a === void 0 ? void 0 : _a[0]) === null || _b === void 0 ? void 0 : _b.IsShapeExportPrefEnabled); } isShapeId(shapeId) { return (shapeId.startsWith('3SR') && shapeId.length >= 15 && shapeId.length <= 18 && /^[0-9a-zA-Z]+$/.exec(shapeId) != null); } async getShapeRepresentation(shapeId) { if (!this.isShapeId(shapeId)) { throw new core_1.SfdxError(messages.getMessage('shape_get_not_a_shape_id')); } const query = `Select Id, Status, Edition, Features, Settings from ShapeRepresentation WHERE Id = '${shapeId}`; return this.conn.singleRecordQuery(query); } } exports.ShapeRepresentationApi = ShapeRepresentationApi; //# sourceMappingURL=shapeRepApi.js.map