@simbachain/web3-suites
Version:
common code for web3 suite plugins. Code in this repo can be used for truffle or hardhat, but is designed to be applicable to future web3 suite plugins as well.
107 lines • 5.16 kB
JavaScript
;
var __importDefault = (this && this.__importDefault) || function (mod) {
return (mod && mod.__esModule) ? mod : { "default": mod };
};
Object.defineProperty(exports, "__esModule", { value: true });
exports.deleteContractsFromPrompts = exports.deleteContractFromDesignID = void 0;
const chalk_1 = __importDefault(require("chalk"));
const prompts_1 = __importDefault(require("prompts"));
const lib_1 = require("../lib");
const lib_2 = require("../lib");
const axios_1 = __importDefault(require("axios"));
const list_1 = require("./list");
/**
* deletes contract from your organisation's design patterns, based on designID
* @param designID
* @returns
*/
async function deleteContractFromDesignID(designID) {
lib_1.SimbaConfig.log.debug(`:: ENTER : designID : ${designID}`);
const authStore = await lib_1.SimbaConfig.authStore();
if (!authStore) {
lib_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: no authStore instantiated`)}`);
lib_1.SimbaConfig.log.debug(`:: EXIT :`);
return;
}
;
const deleteEndpoint = `/v2/organisations/${lib_1.SimbaConfig.organisation.id}/contract_designs/${designID}/`;
let baseURL = lib_1.SimbaConfig.retrieveBaseAPIURL();
let url;
if (baseURL) {
url = (0, lib_2.buildURL)(baseURL, deleteEndpoint);
}
else {
const message = "unable to obtain value for baseURL/SIMBA_API_BASE_URL";
lib_1.SimbaConfig.log.error(`:: EXIT : ${message}`);
throw new Error(message);
}
lib_1.SimbaConfig.log.debug(`url : ${url}`);
try {
await authStore.doDeleteRequest(url);
const orgName = lib_1.SimbaConfig.ProjectConfigStore.get("organisation").name;
lib_1.SimbaConfig.log.info(`${chalk_1.default.cyanBright(`\nsimba: contract with design_id ${chalk_1.default.greenBright(`${designID}`)} successfully deleted from org ${chalk_1.default.greenBright(`${orgName}`)}`)}`);
lib_1.SimbaConfig.log.debug(`:: EXIT :`);
}
catch (error) {
if (axios_1.default.isAxiosError(error) && error.response) {
if (error.response.data.errors[0].status === "404") {
lib_1.SimbaConfig.log.error(`${chalk_1.default.redBright(` \nsimba: designID ${chalk_1.default.greenBright(`${designID}`)} not found. Please make sure you're using the correct designID param`)}`);
}
lib_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: EXIT : ${JSON.stringify(error.response.data)}`)}`);
return;
}
else {
lib_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: EXIT : ${JSON.stringify(error)}`)}`);
return;
}
}
}
exports.deleteContractFromDesignID = deleteContractFromDesignID;
/**
* Allows user to delete contracts from org from prompts
* @param contractDesignArray
* @returns
*/
async function deleteContractsFromPrompts(contractDesignArray) {
lib_1.SimbaConfig.log.debug(`:: ENTER : contractDesignArary: ${JSON.stringify(contractDesignArray)}`);
try {
const contractDesigns = contractDesignArray ?
contractDesignArray :
await (0, list_1.allContracts)();
const choices = [];
if (contractDesigns) {
for (let i = 0; i < contractDesigns.length; i++) {
const title = `${chalk_1.default.green(contractDesigns[i].name)} :: ${chalk_1.default.green("id")} ${contractDesigns[i].id} :: ${chalk_1.default.green("created_on")} ${contractDesigns[i].created_on} :: ${chalk_1.default.green("updated_on")} ${contractDesigns[i].updated_on}`;
const value = contractDesigns[i];
choices.push({ title: title, value: value });
}
}
else {
lib_1.SimbaConfig.log.error(`\nsimba: error obtaining contracts`);
return;
}
const chosen = await (0, prompts_1.default)({
type: 'multiselect',
name: 'contracts',
message: `${chalk_1.default.cyanBright(`Please select all contracts you want to delete from your organisation. Use the Space Bar to select or un-select a contract (You can also use -> to select a contract, and <- to un-select a contract). Hit Return/Enter when you are ready to sync your contracts. THIS WILL PERMANENTLY DELETE THESE CONTRACT DESIGNS FROM YOUR SIMBA ORGANISATION.`)}`,
choices,
});
for (let i = 0; i < chosen.contracts.length; i++) {
const contractDesign = chosen.contracts[i];
await deleteContractFromDesignID(contractDesign.id);
}
return;
}
catch (error) {
if (axios_1.default.isAxiosError(error) && error.response) {
lib_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: EXIT : ${JSON.stringify(error.response.data)}`)}`);
return;
}
else {
lib_1.SimbaConfig.log.error(`${chalk_1.default.redBright(`\nsimba: EXIT : ${JSON.stringify(error)}`)}`);
return;
}
}
}
exports.deleteContractsFromPrompts = deleteContractsFromPrompts;
//# sourceMappingURL=delete.js.map