Manual Reference Source Test

src/helpers/ediary/EntryTemplate.js

import Helper from '../Helper';
import EntryTemplateModel from '../../models/ediary/EntryTemplate';
import Entry from './Entry';
import { aggregate } from '../../utilities/ClassUtility';

/**
 * Helper class representing an eDiary Entry Template.
 *
 * @extends {EntryTemplateModel}
 * @extends {Helper}
 *
 * @example
 * import { MobileUser, EntryTemplate } from 'clinical6';
 * const template = new EntryTemplate({
 *     id: 12,
 *     type: 'ediary__entry_templates',
 *     attributes: {
 *       category: 'manual',
 *       created_at: '2017-06-27T20:32:59Z',
 *       updated_at: '2017-06-27T20:32:59Z'
 *     },
 *     relationships: {
 *       entry_group: {
 *         data: {
 *           id: '13',
 *           type: 'ediary__entry_groups'
 *         }
 *       },
 *       flow_process: {
 *         data: {
 *           id: '145',
 *           type: 'data_collection__flow_processes'
 *         }
 *       }
 *     }
 *   });
 */
class EntryTemplate extends aggregate(EntryTemplateModel, Helper) {
  /**
   * Constructor for helper class representing an eDiary Entry Template
   *
   * @param {Object} json - json api response from server
   */
  constructor(json = {}) {
    super(json);
    this.deserializeRelationshipStubs(json);
    this.syncRelationships(json)
      .then(() => ((this.flow) ? this.flow.loadSteps() : []))
      .catch(() => {});
  }

  /** @type {String}  - The type */
  static get type() {
    return 'ediary__entry_templates';
  }

  /** @type {EntryGroup} */
  get entryGroup() {
    return this._relationships.entry_group;
  }

  /** @type {EntryGroup} */
  set entryGroup(entryGroup) {
    /** @type {EntryGroup} */
    this._relationships.entry_group = entryGroup;
  }

  /** @type {Flow} */
  get flow() {
    return this._relationships.flow_process;
  }

  /** @type {Flow} */
  set flow(flow) {
    /** @type {Flow} */
    this._relationships.flow_process = flow;
  }

  /**
   * Add eDiary Entry
   *
   * @throws {Clinical6Error}             - If missing token or missing required parameters
   * @param  {!Entry}       entry         - Hashmap to save for entry
   * @param  {MobileUser}   [mobileUser]  - The mobile user, must have id if exists
   * @param  {!Number}      mobileUser.id - The mobile user id
   * @return {Promise<Entry[] | Entry>}
   *                                      - Promise object that returns one object or hashmap
   *
   * @example
   * import { MobileUser, EntryTemplate } from 'clinical6';
   * const mobileUser = new MobileUser({ id: 5, ... });
   * const template = new EntryTemplate({ id: 5, ... });
   * const data = { date: '2018-06-02' };
   *
   * // You can add an entry using the `addEntry` method
   * template.addEntry(mobileUser, data).then(entry => console.log(entry));
   */
  async addEntry(entry, mobileUser = undefined) {
    entry = (entry instanceof Entry) ? entry : new Entry(entry);
    if (mobileUser) {
      entry.owner = mobileUser;
    }
    entry.template = this;
    return entry.save();
  }
}

export default EntryTemplate;