Manual Reference Source Test

src/helpers/consent/ConsentFormVersion.js

import ConsentFormVersionModel from '../../models/consent/ConsentFormVersion';
import ApproverGroupAssignment from '../../helpers/consent/ApproverGroupAssignment';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
import PaperTemplate from '../agreement/PaperTemplate';

/**
 * Helper class representing a Consent Form Version.
 *
 * @extends {ConsentFormVersionModel}
 * @extends {Helper}
 *
 * @example
 * // To get a list of ConsentFormVersions use clinical6.get
 * import { clinical6, ConsentFormVersion } from 'clinical6';
 * clinical6.get(ConsentFormVersion).then(c => console.log(c));
 *
 * // To get a single ConsentFormVersion, you can also use clinical6
 * clinical6.get({ id: 5, type: 'consent_form_versions'});
 *
 * // To save or insert, you can either use the .save() capability or clinical6
 * myConsentFormVersion.save(); // insert if no id, save if id
 * clinical6.insert(new ConsentFormVersion({...}));
 * clinical6.update(myConsentFormVersion);
*/
class ConsentFormVersion extends aggregate(ConsentFormVersionModel, Helper) {
  /**
   * Constructor for helper class representing a Consent Form Version
   *
   * @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__form_versions';
  }

  /** @type {Object[]} */
  get approvers() {
    return this._relationships.approvers;
  }

  /** @type {Object[]} */
  set approvers(approvers) {
    /** @type {Object[]} */
    this._relationships.approvers = approvers;
  }

  /** @type {ApproverGroup[]} */
  get approverGroups() {
    return this._relationships.approver_groups;
  }

  /** @type {ApproverGroup[]} */
  set approverGroups(approverGroups) {
    /** @type {ApproverGroup[]} */
    this._relationships.approver_groups = approverGroups;
  }

  /** @type {Language} */
  get language() {
    return this._relationships.language;
  }

  /** @type {Language} */
  set language(language) {
    /** @type {Language} */
    this._relationships.language = language;
  }

  /** @type {AgreementTemplate | PaperTemplate} */
  get templatable() {
    return this._relationships.templatable;
  }


  /** @type {AgreementTemplate | PaperTemplate} */
  set templatable(template) {
    /** @type {AgreementTemplate | PaperTemplate} */
    this._relationships.templatable = template;
  }

  /** @type {Site} */
  get site() {
    return this._relationships.site;
  }

  /** @type {Site} */
  set site(site) {
    /** @type {Site} */
    this._relationships.site = site;
  }

  /** @type {ConsentForm} */
  get consentForm() {
    return this._relationships.consent_form;
  }

  /** @type {ConsentForm} */
  set consentForm(consentForm) {
    /** @type {ConsentForm} */
    this._relationships.consent_form = consentForm;
  }

  /**
   * Gets the approver group assignment associated to this ConsentFormVersion.
   *
   * @param  {Object} [params]            - Parameters used to get information from server
   * @param  {Number} [params.id]         - Id to get data from the server
   * @param  {String} [params.type]       - Type to be used for storage
   * @param  {Object} [params.filters]    - Filters to be used for get
   * @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 { ConsentFormVersion, clinical6 } from 'clinical6';
   * const consentFormVersion = new consentFormVersion({ id: 23 });
   * consentFormVersion.getApproverGroupAssignments({ id: 5 });
   *
   * // Combined with clinical6.get
   * clinical6.get(ConsentFormVersion).then(consentFormVersions => { consentFormVersions[0].getApproverGroupAssignments() });
   */
  async getApproverGroupAssignments(params = {}, options = {}) {
    const service = serviceFactory.get(this.type);
    const assignment = { type: ApproverGroupAssignment.type };
    if (params.id) {
      assignment.id = params.id;
    } else if (params.filters) {
      assignment.filters = params.filters;
    }
    const response = await service.getChildren(this, assignment, options);
    if (Array.isArray(response)) {
      this.assignments = response;
      this.formVersions = this.assignments.map(a => a.formVersion);
    } else if (params.id) {
      // if the developer asks for one assignment given one id, return as just an object
      this.assignments = response;
      this.formVersions = response.formVersion;
    } else {
      // otherwise, there is just one response, make it an array
      this.assignments = [response];
      this.formVersions = [response.formVersion];
    }
    return this.assignments;
  }

  /**
   * Saves a consent form version (insert if id doesn't exist, update if it does)
   * @return {Promise<ConsentFormVersion>} - Returns a promise via ajax call.
   *
   * @example
   * import { ConsentFormVersion, clinical6 } from 'clinical6';
   *
   * // Inserts new consentFormVersion (no existing id)
   * const consentFormVersion = new ConsentFormVersion(
   *   "type": "consent__form_versions",
   *   "attributes": {},
   *   "relationships": {
   *   "agreement_template": {
   *    "data": {
   *       "id": 8,
   *       "type": "agreement__templates"
   *     }
   *   },
   *   "site": {
   *     "data": {
   *       "id": 9,
   *       "type": "trials__sites"
   *     }
   *   }
   *  });
   *
   * consentFormVersion.save();
   *
   * // Updates existing consentFormVersion(has existing id)
   * clinical6.get(ConsentFormVersion).then(consentFormVersions => consentFormVersions[0].save());
   */
  async save() {
    const service = serviceFactory.get(this.type);
    return (this.id) ? service.update(this) : service.insert(this);
  }
}

export default ConsentFormVersion;