UNPKG

@voximplant/voxengine-ci

Version:

Manage Voximplant Platform `applications`, `rules` and `scenarios` from your own environment

137 lines (136 loc) 6.96 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.VoxScenarioPlatformRepository = void 0; const Enums_1 = require("@voximplant/apiclient-nodejs/dist/Enums"); const api_error_response_type_1 = require("../types/api-error-response.type"); const log_message_generator_1 = require("../../utils/log-message-generator"); class VoxScenarioPlatformRepository { constructor(context) { this.context = context; this.lmg = log_message_generator_1.LogMessageGeneratorFactory.getInstance(); this.init = () => { console.info(this.lmg.generate('INFO__INIT_SUCCESS', this.constructor.name)); }; this.downloadScenarios = async () => { const response = await this.context.client.Scenarios.getScenarios({ count: 1, }); if ((0, api_error_response_type_1.isApiErrorResponse)(response)) { throw new Error(this.lmg.generate('ERR__VOXIMPLANT_API_ERROR', Enums_1.APIErrorCode[response.error.code])); } if (!response.totalCount) return []; const { result } = await this.context.client.Scenarios.getScenarios({ count: response.totalCount, }); console.info(this.lmg.generate('INFO__SCENARIOS_DOWNLOADED')); return result; }; this.downloadScenarioById = async (scenarioId) => { const response = await this.context.client.Scenarios.getScenarios({ scenarioId, withScript: true, }); if ((0, api_error_response_type_1.isApiErrorResponse)(response)) { throw new Error(this.lmg.generate('ERR__VOXIMPLANT_API_ERROR', Enums_1.APIErrorCode[response.error.code])); } const scenarioInfo = response.result[0]; if (!scenarioInfo) { console.error(this.lmg.generate('ERR__CANNOT_DOWNLOAD_SCENARIO_BY_ID', scenarioId.toString())); return; } console.info(this.lmg.generate('INFO__SCENARIO_BY_ID_DOWNLOADED', scenarioId.toString())); return scenarioInfo; }; this.downloadScenarioByName = async (scenarioName) => { const response = await this.context.client.Scenarios.getScenarios({ scenarioName, withScript: true, }); if ((0, api_error_response_type_1.isApiErrorResponse)(response)) { throw new Error(this.lmg.generate('ERR__VOXIMPLANT_API_ERROR', Enums_1.APIErrorCode[response.error.code])); } let scenarioInfo = response.result?.find((s) => s?.scenarioName === scenarioName); if (!scenarioInfo || !scenarioInfo.scenarioId) { console.error(this.lmg.generate('ERR__CANNOT_DOWNLOAD_SCENARIO_BY_NAME', scenarioName)); return; } if (!scenarioInfo.scenarioScript) { const scenarioInfoById = await this.downloadScenarioById(scenarioInfo.scenarioId); if (scenarioInfoById) scenarioInfo = scenarioInfoById; } console.info(this.lmg.generate('INFO__SCENARIO_BY_NAME_DOWNLOADED', scenarioName)); return scenarioInfo; }; // TODO: The 'reordering scenarios' feature need to be implemented // await this.context.client.Scenarios.reorderScenarios({...}); this.addScenario = async (scenarioName, scenarioScript) => { try { const response = await this.context.client.Scenarios.getScenarios({ scenarioName, }); if ((0, api_error_response_type_1.isApiErrorResponse)(response)) { throw new Error(this.lmg.generate('ERR__VOXIMPLANT_API_ERROR', Enums_1.APIErrorCode[response.error.code])); } const scenarioInfo = response.result?.find((s) => s?.scenarioName === scenarioName); if (scenarioInfo) { throw new Error(this.lmg.generate('ERR__SCENARIO_ALREADY_EXISTS', scenarioName)); } const { result, scenarioId } = await this.context.client.Scenarios.addScenario({ scenarioName, scenarioScript, }); if (!result) { throw new Error(this.lmg.generate('ERR__CANNOT_ADD_SCENARIO', scenarioName)); } console.info(this.lmg.generate('INFO__SCENARIO_ADDED', scenarioName)); return scenarioId; } catch (error) { console.error(this.lmg.generate('ERR__ADD_SCENARIO_FAILED', this.constructor.name)); console.error(error); } }; this.updateScenario = async (scenarioId, scenarioName, scenarioScript) => { try { const response = await this.context.client.Scenarios.setScenarioInfo({ scenarioId, requiredScenarioName: scenarioName, scenarioScript, }); if ((0, api_error_response_type_1.isApiErrorResponse)(response)) { throw new Error(this.lmg.generate('ERR__VOXIMPLANT_API_ERROR', Enums_1.APIErrorCode[response.error.code])); } const { result } = response; if (!result) { throw new Error(this.lmg.generate('ERR__CANNOT_UPDATE_SCENARIO', scenarioName, scenarioId.toString())); } console.info(this.lmg.generate('INFO__SCENARIO_UPDATED', scenarioId.toString(), scenarioName)); return result; } catch (error) { console.error(this.lmg.generate('ERR__UPDATE_SCENARIO_FAILED', this.constructor.name)); console.error(error); } }; this.bindScenarios = async (bindScenarioRequest) => { try { const response = await this.context.client.Scenarios.bindScenario(bindScenarioRequest); const { scenarioId, scenarioName, ruleName } = bindScenarioRequest; if (bindScenarioRequest.bind) { console.info(this.lmg.generate('INFO__SCENARIOS_BOUND', scenarioId.toString(), scenarioName.toString(), ruleName)); } if (!bindScenarioRequest.bind) { console.info(this.lmg.generate('INFO__SCENARIOS_DETACHED', scenarioId.toString(), scenarioName.toString(), ruleName)); } return response; } catch (error) { console.error(this.lmg.generate('ERR__BIND_SCENARIO_FAILED', this.constructor.name)); console.error(error); } }; } } exports.VoxScenarioPlatformRepository = VoxScenarioPlatformRepository;