@c8y/client
Version:
Client application programming interface to access the Cumulocity IoT-Platform REST services.
148 lines • 5.33 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.SmartGroupsService = void 0;
const Service_js_1 = require("../core/Service.js");
class SmartGroupsService extends Service_js_1.Service {
constructor(client) {
super(client);
this.baseUrl = 'inventory';
this.listUrl = 'managedObjects';
this.SMART_GROUP_TYPE = 'c8y_DynamicGroup';
this.SMART_GROUP_FRAGMENT_TYPE = 'c8y_IsDynamicGroup';
this.COLUMNS_CONFIG_FRAGMENT = 'c8y_DeviceColumnsConfig';
this.FILTER_CONFIG_FRAGMENT = 'c8y_DeviceFilterConfig';
this.QUERY_STRING_FRAGMENT = 'c8y_DeviceQueryString';
}
/**
* Gets the details of managed object
*
* @param {IdReference} entityOrId Entity or Id of the ManagedObject.
* @param {object} filter Filter object.
*
* @returns Response wrapped in [[IResult]]
*
* ```typescript
*
* const id: string = '1';
* const filter: any = { withChildren: false };
*
* (async () => {
* const {data, res} = await smartGroupsService.detail(id, filter);
* })();
* ```
*/
async detail(entityOrId, filter = {}) {
return super.detail(entityOrId, filter);
}
/**
* Creates a smart group managed object with columns configuration.
*
* @param {object} smartGroup Dynamic group managed object.
* @param {object} deviceQueryString Columns configuration query string to filter the new group.
* @param {object} configurableColumns Configurable columns definitions.
* @param {object} filterConfig Columns filter configuration for the new group.
*
* @returns Response wrapped in [[IResult]]
*
* **Example**
* ```typescript
*
* const smartGroup = { name: 'newName', type: 'c8y_DynamicGroup', c8y_IsDynamicGroup: {} };
* const deviceQueryString = 'c8y_DeviceQueryString: "$filter=(name eq 'test*') $orderby=c8y_Hardware.model asc';
* const configurableColumns = [
* {headerName: 'Status', active: false, key: 'status'},
* {headerName: 'Name', active: true, key: 'name', filter: { externalFilterQuery: { name: { names: ['test*'] } } }},
* {headerName: 'Model', active: true, key: 'model'}
* ];
*
* (async () => {
* await smartGroupsService.create({ smartGroup, deviceQueryString, configurableColumns });
* })();
*
* Note: filterConfig has been removed as a default property to the input object.
* Now it is part of the configurable columns properties as it is shown above.
* You can still pass it as an optional parameter.
* ( const filterConfig = [{ name: { names: ['test*'] } }] )
* ```
*/
async create(obj) {
const entity = {
...obj.smartGroup,
[this.QUERY_STRING_FRAGMENT]: obj.deviceQueryString,
[this.COLUMNS_CONFIG_FRAGMENT]: obj.configurableColumns,
...(obj.filterConfig && { [this.FILTER_CONFIG_FRAGMENT]: obj.filterConfig })
};
return super.create(entity);
}
/**
* Updates smart group managed object with given id.
*
* @param {Partial<IManagedObject>} mo Partial managed object of the smart group.
*
* @returns Response wrapped in [[IResult]]
*
* ```typescript
*
* const smartGroup = { id: '1', name: 'newName' };
*
* (async () => {
* await smartGroupsService.update(smartGroup);
* })();
* ```
*/
async update(mo) {
return super.update(mo);
}
/**
* Removes managed object with given id.
*
* @param {IManagedObject} group Managed object of the group.
* @param {object} params Additional query params.
*
* @returns Response wrapped in [[IResult]]
*
* ```typescript
*
* const group = { id: '1' } as IManagedObject;
* const params: any = {
* withChildren: false
* }
*
* (async () => {
* await smartGroupsService.delete(group, params);
* })();
* ```
*/
async delete(entityOrId, params = {}) {
const data = (await this.detail(entityOrId, { withChildren: false })).data;
return await super.delete(data, params);
}
/**
* Checks whether a given managed object is a smart group
*
* @param {IManagedObject} mo Managed object to check.
*
* @returns boolean
*
* ```typescript
*
* const mo: IManagedObject = {id: 1, type: "c8y_DeviceGroup", c8y_IsDeviceGroup: {}} as IManagedObject;
*
* (() => {
* const isSmartGroup = smartGroupsService.isSmartGroup(mo);
* })();
* ```
*/
isSmartGroup(mo) {
const hasSmartGroupFragmentType = mo.hasOwnProperty(this.SMART_GROUP_FRAGMENT_TYPE);
const isSmartGroupType = mo && mo.type === this.SMART_GROUP_TYPE;
return hasSmartGroupFragmentType || isSmartGroupType;
}
getUrl(url = '') {
const partialUrl = url.replace(/^\/+/, '');
const baseUrl = this.baseUrl.replace(/\/+$/, '');
return `${baseUrl}/${partialUrl}`;
}
}
exports.SmartGroupsService = SmartGroupsService;
//# sourceMappingURL=SmartGroupsService.js.map