Manual Reference Source Test

src/helpers/consent/ConsentApprover.js

import ConsentApproverModel from '../../models/consent/ConsentApprover';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';

/**
 * Helper class representing approvers that have been created. Allows for approvers to be selected for consenting on a form or template.
 *
 * @extends {ConsentApproverModel}
 * @extends {Helper}
 *
 * @example
 * // To get a list of ConsentApprovers use clinical6.get
 * import { clinical6, ConsentApprover } from 'clinical6';
 * clinical6.get(ConsentApprover).then(c => console.log(c));
 *
 * // To get a single ConsentApprover, you can also use clinical6
 * clinical6.get({ id: 5, type: 'consent_approvers'});
 *
 * // To save or insert, you can either use the .save() capability or clinical6
 * myConsentApprover.save(); // insert if no id, save if id
 * clinical6.insert(new ConsentApprover({...}));
 * clinical6.update(myConsentApprover);
 *
 * // Typically use clinical6.get()
 * clinical6.get(ConsentApprover).then(consentApprover => console.log(consentApprover));
 *
 * const consentApprover = new ConsentApprover({
 *   data: {
 *    id: 14,
 *    type: 'consent__approvers',
 *    attributes: {
 *      first_name: 'Snoop',
 *      last_name: 'Dogg',
 *      email: 'email@address.com',
 *      password: 'password',
 *      title: 'title'
 *    },
 *    relationships: {
 *      consent_form_version: {
 *        data: {
 *          id: 1,
 *          type: 'consent__form_versions'
 *        }
 *      }
 *    }
 *  }
 * });
 */
class ConsentApprover extends aggregate(ConsentApproverModel, Helper) {
  /**
   * Constructor for helper class representing a Consent Approver
   *
   * @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__approvers';
  }

  /** @type {ConsentFormVersion} */
  get consentFormVersion() {
    return this._relationships.consent_form_version;
  }

  /** @type {ConsentFormVersion} */
  set consentFormVersion(consentFormVersion) {
    /** @type {ConsentFormVersion} */
    this._relationships.consent_form_version = consentFormVersion;
  }

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

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

  /**
   * Saves a consent approver (insert if id doesn't exist, update if it does)
   * @return {Promise<ConsentApprover>} - Returns a promise via ajax call.
   *
   * @example
   * import { ConsentApprover, clinical6 } from 'clinical6';
   *
   * // Inserts new consentApprover (no existing id)
   * const consentApprover = new ConsentApprover(
   *   "type": "consent__approvers",
   *   "attributes": {
   *     "first_name": "firstname",
   *     "last_name": "lastname",
   *     "email": "email"
   *   });
   *
   * consentApprover.save();
   *
   * // Updates existing consentApprover(has existing id)
   * clinical6.get(ConsentApprover).then(consentApprovers => consentApprovers[0].save());
   */
  async save() {
    const service = serviceFactory.get(this.type);
    return (this.id) ? service.update(this) : service.insert(this);
  }

  /**
   * Deletes an approver
   * @return {Promise<ConsentApprover>} - Returns a promise via ajax call.
   *
   * @example
   * import { ConsentApprover, Client } from 'clinical6';
   *
   * // Removes content from server and local storage
   * const consentApprover = new ConsentApprover({...});
   * consentApprover.delete();
   *
   * // No longer in storage
   * Client.instance.storageUtility.has('consentApprover', { id: 1 });
   */
  delete() {
    const service = serviceFactory.get(this.type);
    return service.delete(this);
  }
}

export default ConsentApprover;