src/helpers/filter/FilterGroup.js
import { deprecate } from 'core-decorators'; // eslint-disable-line
import FilterGroupModel from '../../models/filter/FilterGroup';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
/**
* Helper class representing a filter group.
*
* @extends {FilterGroupModel}
* @extends {Helper}
*
* @example
* // To get a list of FilterGroups use clinical6.get
* import { clinical6, FilterGroup } from 'clinical6';
* clinical6.get(FilterGroup).then(c => console.log(c));
*
* // To get a single filter_group, you can also use clinical6
* clinical6.get({ id: 5, type: 'filter_groups'});
*
* // To save or insert, you can either use the .save() capability or clinical6
* myFilterGroup.save(); // insert if no id, save if id
* clinical6.insert(new FilterGroup({...}));
* clinical6.update(myFilterGroup);
*/
class FilterGroup extends aggregate(FilterGroupModel, Helper) {
/**
* Constructor for helper class representing a FilterGroup
*
* @param {Object} json - json api response from server
*/
constructor(json = {}) {
super(json);
this.deserializeRelationshipStubs(json);
this.syncRelationships(json);
}
/** @type {Filter[]} */
get filters() {
return this._relationships.filters;
}
/** @type {Filter[]} */
set filters(filters) {
/** @type {Filter[]} */
this._relationships.filters = filters;
}
/** @type {Cohort} */
get cohort() {
return this._relationships.cohort;
}
/** @type {Cohort} */
set cohort(cohort) {
/** @type {Cohort} */
this._relationships.cohort = cohort;
}
/** @type {String} - The type */
static get type() {
return 'filter_groups';
}
/**
* Saves a filter group (insert if id doesn't exist, update if it does)
* @return {Promise<FilterGroup>} - Returns a promise via ajax call.
*
* @example
* import { FilterGroup, clinical6 } from 'clinical6';
*
* // Inserts new role (no existing id)
* const filterGroup = new FilterGroup({
* "type": "filter_groups",
* "attributes": {
* "operator": "and",
* "created_at": "2018-04-19T19:16:12Z",
* "updated_at": "2018-04-19T19:16:12Z"
* },
* "relationships": {
* "cohort": {
* "data": {
* "id": "25",
* "type": "cohorts"
* }
* }
* }
* });
* filterGroup.save();
*
* // Updates existing filter group (has existing id)
* clinical6.get(FilterGroup).then(filterGroups => filterGroups[0].save());
*/
async save() {
const service = serviceFactory.get(this.type);
return (this.id) ? service.update(this) : service.insert(this);
}
}
export default FilterGroup;