Manual Reference Source Test

src/helpers/consent/ConsentGrant.js

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

/**
 * Helper class representing a Consent Grant.
 *
 * @extends {ConsentGrantModel}
 * @extends {Helper}
 *
 * @example
 *
 *
 * // To save or insert, you can either use the .save() capability or clinical6
 * myConsentGrant.save(); // insert if no id, save if id
 * clinical6.insert(new ConsenGrant({...}));
 * clinical6.update(myConsenGrant);
 *
*/
class ConsentGrant extends aggregate(ConsentGrantModel, Helper) {
  /**
   * Constructor for helper class representing a Consent Grant
   *
   * @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__grants';
  }

  /** @type {Agreement} */
  get agreement() {
    return this._relationships.agreement;
  }

  /** @type {Agreement} */
  set agreement(agreement) {
    this._relationships.agreement = agreement;
  }

  /** @type {ConsentFormVersion} */
  get formVersion() {
    return this._relationships.form_version;
  }

  /** @type {ConsentFormVersion} */
  set formVersion(version) {
    this._relationships.form_version = version;
  }

  /** @type {User} */
  get grantedBy() {
    return this._relationships.granted_by;
  }

  /** @type {User} */
  set grantedBy(grantedBy) {
    this._relationships.granted_by = grantedBy;
  }

  /** @type {User} */
  get grantedFor() {
    return this._relationships.granted_for;
  }

  /** @type {User} */
  set grantedFor(grantedFor) {
    this._relationships.granted_for = grantedFor;
  }

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

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

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

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

  /**
   * Saves a consent grant (insert if id doesn't exist, update if it does)
   * @return {Promise<ConsentGrant>} - Returns a promise via ajax call.
   *
   * @example
   * import { ConsentGrant, clinical6 } from 'clinical6';
   *
   * // Inserts new consentGrant (no existing id)
   * const consentGrant = new ConsentGrant({
   * type: "consent__grants",
   * attributes: {
   *   signing_password: "1212"
   * },
   * relationships: {
   *   granted_for: {
   *     data: {
   *       id: 12,
   *       type: "mobile_users"
   *     }
   *   },
   *   strategy: {
   *     data: {
   *       id: 1,
   *       type: "consent__strategies"
   *     }
   *   },
   *   form: {
   *     data: {
   *       id: 1,
   *       type: "consent__forms"
   *     }
   *   }
   * });
   *
   * consentGrant.save();
   *
   * // Updates existing consentGrant (has existing id)
   * clinical6.get(ConsentGrant).then(consentGrants => consentGrants[0].save());
   */
  async save() {
    const service = serviceFactory.get(this.type);
    return (this.id) ? service.update(this) : service.insert(this);
  }
}

export default ConsentGrant;