src/services/RoleService.js
import JsonApiService from './JsonApiService';
import {
hasAttribute,
hasToken,
isRequired,
isValid,
validate,
} from '../utilities/ValidationUtility';
/**
* Service handling UserRole calls with specific endpoints.
*/
class RoleService extends JsonApiService {
/**
* Update type to be user_roles
*/
constructor() {
super('user_roles');
}
/**
* @override
* Call a POST request on permission expecting JSON API information.
*
* @param {Permission} permission - Permission object in which to insert
* @param {Role} permission.role - Role object in which to insert
* @param {Object} permission.authorizable - Object in which to insert
* @param {AllowedAction} [permission.allowedAction] - Optional allowedAction object in which to insert
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Permission>} - Inserted permission
*
* @example
* import { Permission, Role, roleService } from 'clinical6';
* const permission = new Permission({...});
* permission.authorizable = {id: 117, type: 'sections'}
* permission.role = new Role({...})
*
* // you can insert a permission using the `insertPermission` method.
* roleService.insertPermssion(permission).then(permission => console.log(permission));
*
*/
async insertPermission(permission, cacheMode = undefined) {
validate('RoleService.insertPermission',
hasToken(),
isRequired({ permission }),
hasAttribute({ permission }, 'id'),
hasAttribute({ permission }, 'type'),
hasAttribute({ permission }, 'role'),
hasAttribute({ permission }, 'authorizable'));
validate('RoleService.insertPermission',
hasAttribute({ role: permission.role, authorizable: permission.authorizable }, 'id'),
hasAttribute({ role: permission.role, authorizable: permission.authorizable }, 'type'));
// validate('RoleService.insert',
// authorizable.type === 'data_collection__flow_processes' ||
// authorizable.type === 'dynamic_content__content_types' ||
// authorizable.type === 'sections'
// );
// const relationships = { allowedActions };
// permission.relationships = relationships;
return super.insert(permission, { cacheMode });
}
/**
* @override
* Call a DELETE request on the main obj.type expecting JSON API information.
*
* This method is not supported by Role
*
* @param {Object} object - Object in which to delete
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise} - Message
*/
async delete(object, cacheMode = undefined) {
validate('RoleService.delete', isValid(false, ' is not supported'));
return super.delete(object, { cacheMode });
}
/**
* @override
* Call a GET request expecting JSON API information.
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Object} [params] - Parameters used to get information from server (such as id)
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Role[] | Role>}
* - Promise object that returns one role or a key:role hashtable
*
* @example
* import { roleService } from 'clinical6';
*
* // You will be able to access these roles using the `get` method.
* roleService.get().then(roles => console.log(roles));
*
* // Additionally, you can retrieve a specific role with the `get`
* // method, using the ID of the desired role as a parameter.
* roleService.get({ id: 23}).then(role => console.log(role));
*/
async get(params = {}, cacheMode = undefined) {
validate('RoleService.get',
hasToken());
return super.get(params, { cacheMode });
}
/**
* @override
* Call a GET request expecting JSON API information to get authorizable data
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @return {Promise<[Object]>}
* - Promise object that returns an array of Flow, ContentType, and Section objects.
*
* @example
* import { roleService } from 'clinical6';
*
* // You will be able to access authorizable data using the `get` method.
* roleService.getAuthorizableResources().then(authorizables => console.log(authorizables));
*
*/
getAuthorizableResources() {
validate('RoleService.getAuthorizableResources',
hasToken());
return super.get({ type: 'authorizables' });
}
/**
* @override
* Call a GET request expecting JSON API information to get the invitable user roles of user role
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Object} [role] - Role to grab invitable user roles for
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Role[] | Role>}
* - Promise object that returns one role or a key:role hashtable
*
* @example
* import { roleService } from 'clinical6';
*
* // You will be able to access the invitable user roles using the `get` method.
* roleService.getInvitableRoles({ id: 5 }).then(roles => console.log(roles));
*
*/
async getInvitableRoles(role, cacheMode = undefined) {
validate('RoleService.getInvitableRoles',
hasToken(),
isRequired({ role }),
hasAttribute({ role }, 'id'));
const url = `/v3/user_roles/${role.id}/invitable_user_roles`;
return super.get({ id: role.id }, { url, cacheMode });
}
/**
* @override
* Call a GET request expecting JSON API information to get the role permissions
*
* @throws {Clinical6Error} - If missing token or missing required parameters
* @param {Object} [role] - Role to grab invitable user roles for
* @param {String} [options] - Modify the nature of the call and response
* @param {String} [options.url] - Override the url for this call
* @param {String} [options.cacheMode] - Override the caching method for this call
* @return {Promise<Permission[] | Permission>}
* - Promise object that returns one role or a key:role hashtable
*
* @example
* import { roleService } from 'clinical6';
*
* // You will be able to access the role permissions using the `get` method.
* roleService.getPermissions().then(roles => console.log(roles));
*
*/
async getPermissions(role, options) {
validate('RoleService.getPermissions',
hasToken(),
isRequired({ role }),
hasAttribute({ role }, 'id'));
return super.getChildren(role, { type: 'permissions' }, options);
}
/**
* @override
* Call a POST request on the main obj.type expecting JSON API information.
*
* @param {Role} role - Role object in which to insert
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Role>} - Inserted role
*
* @example
* import { Role, roleService } from 'clinical6';
* const user = new Role({...});
*
* // you can insert a role using the `insert` method.
* roleService.insert(role).then(role => console.log(role));
*
* // you could also just call `save` on the role if it doesn't have an id, which will also
* // invoke the `insert` method
* role.save();
*/
async insert(role, cacheMode = undefined) {
validate('RoleService.insert',
hasToken(),
isRequired({ role }),
hasAttribute({ role }, 'name'),
hasAttribute({ role }, 'permanentLink'));
return super.insert(role, cacheMode);
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {UserRole} role - Role object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<Role>} - Updated role
*
* @example
* import { Role, roleService } from 'clinical6';
* const role = new Role({...});
*
* // you can update a role using the `update` method.
* roleService.update(role).then(role => console.log(role));
*
* // you could also just call `save` on the role if it has an id, which will also
* // invoke the `update` method
* role.save();
*/
async update(role, cacheMode = undefined) {
validate('RoleService.update',
hasToken(),
isRequired({ role }));
return super.update(role, { cacheMode });
}
/**
* @override
* Call a DELETE request on the main obj.type expecting JSON API information.
*
* @param {Permission} permission - Permission object in which to delete
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise} - Message
*
* @example
* import { roleService } from 'clinical6';
*
* // You will be able to delete a permission using `delete`.
* roleService.delete({ id: 23 });
* // This will also clear the local storage of this type and id
*/
async deletePermission(permission, cacheMode = undefined) {
validate('RoleService.deletePermission',
hasToken(),
isRequired({ permission }));
return super.delete(permission, { cacheMode });
}
}
export default RoleService;