@voximplant/voxengine-ci
Version:
Manage Voximplant Platform `applications`, `rules` and `scenarios` from your own environment
94 lines (93 loc) • 4.54 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.VoxRulePersistentRepository = void 0;
const node_path_1 = require("node:path");
const abstract_persistent_repository_1 = require("./abstract.persistent.repository");
class VoxRulePersistentRepository extends abstract_persistent_repository_1.AbstractPersistentRepository {
constructor(context) {
super(context);
this.relativeStoragePath = 'applications';
this.basename = 'rules.config';
this.metadataBasename = 'rules.metadata.config';
this.create = async (voxRulesList, applicationName) => {
try {
const joinedPath = (0, node_path_1.join)(this.relativeStoragePath, applicationName);
await this.context.client.createFile(joinedPath, this.basename, JSON.stringify(voxRulesList));
}
catch (error) {
console.error(this.lmg.generate('ERR__CREATE_FAILED', this.constructor.name));
console.error(error);
}
};
this.createMetadata = async (voxRulesMetadataList, applicationName) => {
try {
const joinedPath = (0, node_path_1.join)(this.relativeStoragePath, applicationName);
await this.context.client.createMetadataFile(joinedPath, this.metadataBasename, JSON.stringify(voxRulesMetadataList));
}
catch (error) {
console.error(this.lmg.generate('ERR__CREATE_METADATA_FAILED', this.constructor.name));
console.error(error);
}
};
this.createOrUpdate = async (voxRulesList, applicationName) => {
try {
const joinedPath = (0, node_path_1.join)(this.relativeStoragePath, applicationName);
await this.context.client.createOrUpdateFile(joinedPath, this.basename, JSON.stringify(voxRulesList));
}
catch (error) {
console.error(this.lmg.generate('ERR__CREATE_OR_UPDATE_FAILED', this.constructor.name));
console.error(error);
}
};
this.createOrUpdateMetadata = async (voxRulesMetadataList, applicationName) => {
try {
const joinedPath = (0, node_path_1.join)(this.relativeStoragePath, applicationName);
await this.context.client.createOrUpdateMetadataFile(joinedPath, this.metadataBasename, JSON.stringify(voxRulesMetadataList));
}
catch (error) {
console.error(this.lmg.generate('ERR__CREATE_OR_UPDATE_METADATA_FAILED', this.constructor.name));
console.error(error);
}
};
this.read = async (applicationName) => {
try {
const joinedPath = (0, node_path_1.join)(this.relativeStoragePath, applicationName, this.basename);
const rawData = await this.context.client.readFile(joinedPath);
if (!rawData)
return;
// TODO: Need to refactor this part
try {
return JSON.parse(rawData);
}
catch (error) {
throw new Error(this.lmg.generate('ERR__APP_DATA_FORMAT_IS_NOT_VALID', applicationName));
}
}
catch (error) {
console.error(this.lmg.generate('ERR__READ_FAILED', this.constructor.name));
console.error(error);
}
};
this.readMetadata = async (applicationName) => {
try {
const joinedPath = (0, node_path_1.join)(this.relativeStoragePath, applicationName, this.metadataBasename);
const rawData = await this.context.client.readMetadataFile(joinedPath);
if (!rawData) {
throw new Error(this.lmg.generate('ERR__APP_BY_NAME_DOES_NOT_EXIST', applicationName));
}
// TODO: Need to refactor this part
try {
return JSON.parse(rawData);
}
catch (error) {
throw new Error(this.lmg.generate('ERR__APP_METADATA_FORMAT_IS_NOT_VALID', applicationName));
}
}
catch (error) {
console.error(this.lmg.generate('ERR__READ_METADATA_FAILED', this.constructor.name));
console.error(error);
}
};
}
}
exports.VoxRulePersistentRepository = VoxRulePersistentRepository;