Manual Reference Source Test

src/helpers/consent/ConsentForm.js

import ConsentFormModel from '../../models/consent/ConsentForm';
import ConsentFormVersion from './ConsentFormVersion';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';

/**
 * Helper class representing a Consent Form.
 *
 * @extends {ConsentFormModel}
 * @extends {Helper}
 *
 * @example
 * // To get a list of ConsentForms use clinical6.get
 * import { clinical6, ConsentForm } from 'clinical6';
 * clinical6.get(ConsentForm).then(c => console.log(c));
 *
 * // To get a single ConsentForm, you can also use clinical6
 * clinical6.get({ id: 5, type: 'consent__forms'});
 *
 * // To save or insert, you can either use the .save() capability or clinical6
 * myConsentForm.save(); // insert if no id, save if id
 * clinical6.insert(new ConsentForm({...}));
 * clinical6.update(myConsentForm);
*/
class ConsentForm extends aggregate(ConsentFormModel, Helper) {
  /**
   * Constructor for helper class representing a Consent Form
   *
   * @param {Object} json - json api response from server
   */
  constructor(json = {}) {
    super(json);
    this.deserializeRelationshipStubs(json);
    this.syncRelationships(json);
  }

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

  /** @type {ConsentFormVersion[]} */
  get consentFormVersions() {
    return this._relationships.consent_form_versions;
  }

  /** @type {ConsentFormVersion[]} */
  set consentFormVersions(consentFormVersions) {
    /** @type {ConsentFormVersion[]} */
    this._relationships.consent_form_versions = consentFormVersions;
  }

  /** @type {ConsentStrategy} */
  get consentStrategy() {
    return this._relationships.strategy;
  }

  /** @type {ConsentStrategy} */
  set consentStrategy(consentStrategy) {
    /** @type {ConsentStrategy} */
    this._relationships.strategy = consentStrategy;
  }

  /**
   * Saves a consent form (insert if id doesn't exist, update if it does)
   * @return {Promise<ConsentForm>} - Returns a promise via ajax call.
   *
   * @example
   * import { ConsentForm, clinical6 } from 'clinical6';
   *
   * // Inserts new consentForm (no existing id)
   * const consentForm = new ConsentForm(
   *   "type": "consent__forms",
   *   "attributes": {
   *     "name": "a consent form name",
   *     "enabled": true
   *   });
   *
   * consentForm.save();
   *
   * // Updates existing consentForm(has existing id)
   * clinical6.get(ConsentForm).then(consentForms => consentForms[0].save());
   */
  async save() {
    const service = serviceFactory.get(this.type);
    return (this.id) ? service.update(this) : service.insert(this);
  }

  /**
   * Gets the ConsentFormVersions associated to this ConsentForm.
   *
   * @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}                    - Promise with data (array or object)
   *
   * @example
   * import { ConsentForm, clinical6 } from 'clinical6';
   * const consentForm = new ConsentForm({ id: 23 });
   * consentForm.getVersions({ id: 5 });
   *
   * // Combined with clinical6.get
   * clinical6.get(ConsentForm).then(consentForms => { consentForms[0].getVersions() });
   */
  async getVersions(options = {}) {
    const service = serviceFactory.get(this.type);
    const consentFormVersion = { type: ConsentFormVersion.type };

    const response = await service.getChildren(this, consentFormVersion, options);
    this.consentFormVersions = response;

    return this.consentFormVersions;
  }
}

export default ConsentForm;