Manual Reference Source Test

src/services/EDiaryService.js

import JsonApiService from './JsonApiService';
import {
  hasToken,
  isValid,
  validate,
} from '../utilities/ValidationUtility';

/**
 * Service handling EDiary (Entries, Groups, and Templates) calls with specific endpoints.
 */
class EDiaryService extends JsonApiService {
  /**
   * Update type to be ediary__entry_groups
   */
  constructor() {
    super('ediary__entry_groups',
      {
        title: 'EDiaryService',
        obj: 'entryGroup',
        tokenRequired: ['delete', 'get', 'insert', 'update', 'getChildren']
      });
  }

  /**
   * @override
   * Call a DELETE request on the main obj.type expecting JSON API information.
   *
   * This method is not supported by EDiary
   *
   * @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('EDiaryService.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  {String} [cacheMode] - Override the caching method for this call
   * @return {Promise<EntryGroup[] | EntryGroup>}
   *                              - Promise object that returns one object or hashmap
   *
   * @example
   * import { eDiaryService } from 'clinical6';
   *
   * // You will be able to access these entry groups using the `get` method.
   * eDiaryService.get().then(entryGroups => console.log(entryGroups));
   */
  async get(cacheMode = undefined) {
    validate('EDiaryService.get',
      hasToken());
    return super.get({ type: 'ediary__entry_groups' }, { cacheMode, key: 'permanent_link' })
      .then((entryGroups) => {
        entryGroups.forEach(entryGroup => entryGroup.syncRelationships());
        return entryGroups;
      });
  }

  /**
   * Get eDiary entry templates
   *
   * @throws {Clinical6Error}       - If missing token or missing required parameters
   * @param  {!Object}  entryGroup  - Parameters used to get information from server (such as id)
   * @param  {Object}   [filters]    - Filters to modify result (date, entry_group_id)
   * @param  {String}   [cacheMode] - Override the caching method for this call
   * @return {Promise<EntryTemplate[] | EntryTemplate>}
   *                                - Promise object that returns one object or hashmap
   *
   * @example
   * import { eDiaryService } from 'clinical6';
   *
   * // You will be able to access these entry groups using the `get` method.
   * eDiaryService.getTemplates({ id: 15 }).then(templates => console.log(templates));
   *
   * // You can also use the filter for automatic or manual
   * eDiaryService.getTemplates({ id: 15 }, { category: 'automatic' })
   *  .then(templates => console.log(templates));
   */
  async getTemplates(entryGroup, filters = undefined, cacheMode = undefined) {
    return super.getChildren(entryGroup, { type: 'ediary__entry_templates', filters }, { cacheMode });
  }

  /**
   * @override
   * Call a POST request on the main obj.type expecting JSON API information.
   *
   * This method is not supported by EDiary
   *
   * @param  {Object} object        - Object in which to insert
   * @param  {String} [cacheMode]   - Override the caching method for this call
   * @return {Promise<EntryGroup>}  - Inserted object
   */
  async insert(object, cacheMode = undefined) {
    validate('EDiaryService.insert', isValid(false, ' is not supported'));
    return super.insert(object, { cacheMode });
  }

  /**
   * @override
   * Call a PATCH request on the main obj.type expecting JSON API information.
   *
   * This method is not supported by EDiary
   *
   * @param  {Object} object        - Object in which to update
   * @param  {String} [cacheMode]   - Override the caching method for this call
   * @return {Promise<EntryGroup>}  - Updated object
   */
  async update(object, cacheMode = undefined) {
    validate('EDiaryService.update', isValid(false, ' is not supported'));
    return super.update(object, { cacheMode });
  }
}

export default EDiaryService;