src/services/AppMenuService.js
import JsonApiService from './JsonApiService';
import { validate, hasToken, isRequired } from '../utilities/ValidationUtility';
import { appMenuService } from '..';
/**
* Service handling AppMenu calls with specific endpoints.
*/
class AppMenuService extends JsonApiService {
/**
* Update type to be appMenus
*/
constructor() {
super('navigation__app_menus',
{
title: 'AppMenuService',
obj: 'appMenu',
tokenRequired: ['delete', 'get', 'insert', 'update']
});
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @throws {Clinical6Error} - If casacade param is false and there are submenus
* @param {AppMenu} appMenu - AppMenu object in which to update
* @param {Object} [options] - Options to modify the network call and storage options
* @param {String} [options.cacheMode] - Override the caching method for this call
* @param {Boolean} [options.cascade] - boolean value: if not set, defaults to false; if set to true that means there were sub-menus that can be deleted with the parent, if false, that means it can only be deleted if there are no sub-menus
* @return {Promise} - Message
*
* @example
* import { appMenuService } from 'clinical6';
*
* // You will be able to delete a appMenu using `delete`.
* appMenuService.delete({ id: 23 });
* // This will also clear the local storage of this type and id
* // cascade will also delete sub-menus if set to true
*/
async delete(appMenu, options = {}) {
validate('AppMenuService.delete',
hasToken(),
isRequired({ appMenu }));
return super.delete(appMenu, options).then(() => appMenuService.clearStorageMenus(appMenu));
}
/**
*
* @param {AppMenu} menu
* // pass parent menu in, then filter the children array to clear from storage
* // after the delete the clear function is chained as follows:
* .then(() => appMenuService.clearStorageMenus(appMenu));
* // this will clear the sub-menus if cascade = true in the options being passed on the delete, for example:
* await appMenuService.delete(parentMenu, { cascade: true });
*/
async clearStorageMenus(menu) {
if (menu.children) {
menu.children.forEach(subMenu => this.clearStorageMenus(subMenu));
}
menu.clear();
}
/**
* @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 {Number} [params.id] - Id of the appMenu
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<AppMenu[] | AppMenu>}
* - Promise object that returns one appMenu or a key:appMenu hashtable
*
* @example
* import { appMenuService } from 'clinical6';
*
* // You will be able to access these appMenus using the `get` method.
* appMenuService.get().then(appMenus => console.log(appMenus));
*
* // Additionally, you can retrieve the subcategories for a specific appMenu with the `get`
* // method, using the ID of the desired appMenu as a parameter.
* appMenuService.get({ id: 23 }).then(appMenu => console.log(appMenu));
*/
async get(params = {}, cacheMode = undefined) {
validate('AppMenuService.get',
hasToken());
return super.get(params, { cacheMode })
.then((menus) => {
if (Array.isArray(menus)) {
menus.forEach((menu) => {
menu.syncRelationships();
menu.connectChildren();
});
} else if (menus.syncRelationships && menus.connectChildren) {
menus.syncRelationships();
menus.connectChildren();
}
return menus;
});
}
/**
* 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 {Number} [params.filters] - Id of the appMenu
* @return {Promise} - Promise object that returns one appMenu or a key:appMenu hashtable
*
* @example
* import { appMenuService } from 'clinical6';
*
* // You will be able to access these action details using the `getActionDetails` method.
* appMenuService.getActionDetails().then(a => console.log(a));
*
* // Additionally, we allow for filtering of the `getActionDetails` method, using the filter key.
* // No filters are current supported on the platform
* appMenuService.getActionDetails({ filter: { 'someFilter': 'someValue' }}).then(a => console.log(a));
*/
async getActionDetails(params = {}) {
validate('AppMenuService.getActionDetails',
hasToken());
params.type = 'navigation__action_details';
return super.get(params, { cacheMode: 'networkOnly' });
}
/**
* @override
* Call a POST request on the main obj.type expecting JSON API information.
*
* @param {Object} [appMenu] - Parameters used to get information from server (such as id)
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<AppMenu>} - Inserted appMenu
*
* @example
* import { AppMenu, appMenuService } from 'clinical6';
* const appMenu = new AppMenu({...});
*
* // you can insert a appMenu using the `insert` method.
* appMenuService.insert(appMenu).then(appMenu => console.log(appMenu));
*
* // you could also just call `save` on the appMenu if it doesn't have an id, which will also
* // invoke the `insert` method
* appMenu.save();
*/
async insert(appMenu, cacheMode = undefined) {
validate('AppMenuService.insert',
hasToken(),
isRequired({ appMenu }));
return super.insert(appMenu, { cacheMode });
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {AppMenu} appMenu - AppMenu object in which to update
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<AppMenu>} - Updated appMenu
*
* @example
* import { AppMenu, appMenuService } from 'clinical6';
* const appMenu = new AppMenu({...});
*
* // you can update a appMenu using the `update` method.
* appMenuService.update(appMenu).then(appMenu => console.log(appMenu));
*
* // you could also just call `save` on the appMenu if it has an id, which will also
* // invoke the `update` method
* appMenu.save();
*/
async update(appMenu, cacheMode = undefined) {
validate('AppMenuService.update',
hasToken(),
isRequired({ appMenu }));
return super.update(appMenu, { cacheMode });
}
/**
* @override
* Call a PATCH request on the main obj.type expecting JSON API information.
*
* @param {AppMenu[]} order - Array of AppMenus to order. The desired position / parent information should be set in each AppMenu
* @param {String} [cacheMode] - Override the caching method for this call
* @return {Promise<AppMenu[]>} - Updated appMenus
*
* @example
* import { AppMenu, appMenuService } from 'clinical6';
* const appMenu1 = new AppMenu({ id: 13 });
* const appMenu2 = new AppMenu({ id: 15 });
* const appMenu3 = new AppMenu({ id: 16 });
* const appMenu4 = new AppMenu({ id: 17 });
*
* const order = [appMenu1, appMenu2, appMenu3, appMenu4];
*
* // you can update the order of appMenus using the `updateOrder` method.
* appMenuService.updateOrder(order).then(appMenus => console.log(appMenus));
*
*/
async updateOrder(order, cacheMode = undefined) {
validate('AppMenuService.updateOrder',
hasToken(),
isRequired({ order }));
order = order.reduce((m, e) => {
m[e.id] = {
position: e.position,
parent: (e.parent && e.parent.id) ? e.parent.id : null
};
return m;
}, {});
const url = `/v3/navigation/app_menus/order`;
return super.insert({
type: this.type,
attributes: order
}, { url, cacheMode });
}
}
export default AppMenuService;