src/helpers/user/AllowedAction.js
import AllowedActionModel from '../../models/user/AllowedAction';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
/**
* Helper class representing a Permission AllowedAction.
*
* @extends {AllowedActionModel}
* @extends {Helper}
*/
class AllowedAction extends aggregate(AllowedActionModel, Helper) {
constructor(json = {}) {
super(json);
this.deserializeRelationshipStubs(json);
this.syncRelationships(json);
}
/** @type {Permission} */
get permission() {
return this._relationships.permission;
}
/** @type {Permission} */
set permission(permission) {
/** @type {Permission} */
this._relationships.permission = permission;
}
/** @type {String} - The type */
static get type() {
return 'allowed_actions';
}
/**
* Deletes an allowedAction
* @return {Promise} - Returns a promise via ajax call.
*
* @example
* import { AllowedAction, Client } from 'clinical6';
*
* // Removes allowedAction from server and local storage
* const allowedAction = new AllowedAction({...});
* allowedAction.delete();
*
* // No longer in storage
* Client.instance.storageUtility.has('allowedActions', { id: 1 });
*/
async delete() {
const service = serviceFactory.get(this.type);
return service.delete(this);
}
/**
* Saves an allowed action (insert if id doesn't exist, update if it does)
* @return {Promise<AllowedAction>} - Returns a promise via ajax call.
*
* @example
* import { AllowedAction, clinical6 } from 'clinical6';
*
* // Insert is different from other inserts.
* const allowedAction = new AllowedAction({
* "name": "manage"
* });
* allowedAction.save();
*
*
* // Updates existing allowedAction (has existing id)
* clinical6.get(AllowedAction).then(allowedActions => allowedActions[0].save());
*/
async save() {
const service = serviceFactory.get(this.type);
return (this.id) ? service.update(this) : service.insert(this);
}
}
export default AllowedAction;