UNPKG

@c8y/client

Version:

Client application programming interface to access the Cumulocity IoT-Platform REST services.

142 lines 5.12 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.SmartRulesService = void 0; const index_js_1 = require("../application/index.js"); const Service_js_1 = require("../core/Service.js"); class SmartRulesService extends Service_js_1.Service { static { this.microserviceName = 'smartrule'; } constructor(client) { super(client); this.baseUrl = 'service/smartrule'; this.rulesUrl = 'smartrules'; this.unupdatableFields = ['type', 'cepModuleId', 'creationTime', 'lastUpdated']; this.applicationService = new index_js_1.ApplicationService(client); } /** * Checks if the smart rules microservice is subscribed and available to user. * * **Example** * ```typescript * * (async () => { * const isAvailable = await smartRulesService.isMicroserviceAvailable(); * })(); * ``` */ async isMicroserviceAvailable() { if (this.microserviceAvailable === undefined) { this.microserviceAvailable = (await this.applicationService.isAvailable(SmartRulesService.microserviceName)).data; } return this.microserviceAvailable; } /** * Gets a list of smart rules for given managed object. * * @param {IdReference} entityOrId Entity or Id of the ManagedObject. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const id: string = '1'; * * (async () => { * const {data, res} = await smartRulesService.listByContext(id); * })(); * ``` */ async listByContext(entityOrId) { const url = this.contextRulesUrl(entityOrId); const res = await this.fetch(url, { method: 'GET' }); const rules = (await res.json()).rules; return { res, data: rules }; } /** * Deactivates smart rule for given entities list. * * @param {Partial<IRule>} rule Smart rule managed object. * @param {IdReference[]} entitiesOrIdsList List of entities or Id of the ManagedObjects. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const rule: IRule = {id: '1', enabledSources: ['2', '3'],...}; * const entityOrIdList: IdReference[] = ['2']; * (async () => { * const {data, res} = await smartRulesService.bulkDeactivateEnabledSources(rule, entityOrIdList); * })(); * ``` */ bulkDeactivateEnabledSources(rule, entitiesOrIdsList) { if (entitiesOrIdsList.length === 0) { return Promise.resolve({ res: null, data: null }); } const { enabledSources } = rule; const newEnabledSources = this.disableEnabledSources(enabledSources, entitiesOrIdsList); const ruleCopy = { ...rule }; Object.assign(ruleCopy, { enabledSources: newEnabledSources }); return this.update(ruleCopy); } /** * Updates smart rule. * * @param {Parial<IRule>} rule Smart rule managed object. * * @returns Response wrapped in [[IResult]] * * **Example** * ```typescript * * const rule: IRule = {id: '1', enabledSources: ['2', '3'],...}; * (async () => { * const {data, res} = await smartRulesService.updateSmartRule(rule); * })(); * ``` */ async update(rule) { const url = this.getSmartRulesUrl(rule); const method = 'PUT'; const body = JSON.stringify(this.removeUnclonableFields(rule, this.unupdatableFields)); const headers = { 'content-type': 'application/json', accept: 'application/json' }; const res = await this.fetch(url, { method, body, headers }); const data = await res.json(); return { res, data }; } disableEnabledSources(enabledSources = [], entityOrIdList) { return enabledSources.filter(id => !this.getListOfStringIds(entityOrIdList).includes(id)); } getListOfStringIds(entityOrIdList) { return entityOrIdList.map(entityOrId => { if (typeof entityOrId === 'object' && entityOrId.id) { return entityOrId.id.toString(); } return entityOrId.toString(); }); } getSmartRulesUrl(rule) { const contextMoId = rule && rule.c8y_Context && rule.c8y_Context.id; let url = !!contextMoId ? this.contextRulesUrl(contextMoId) : this.rulesUrl; if (rule.id) { url = `${url}/${rule.id}`; } return url; } removeUnclonableFields(rule, fieldsToRemove) { const ruleCopy = { ...rule }; fieldsToRemove.forEach(f => { delete ruleCopy[f]; }); return ruleCopy; } contextRulesUrl(entityOrId = {}) { if (typeof entityOrId === 'object' && entityOrId.id) { return `managedObjects/${entityOrId.id}/smartrules`; } return `managedObjects/${entityOrId}/smartrules`; } } exports.SmartRulesService = SmartRulesService; //# sourceMappingURL=SmartRulesService.js.map