Manual Reference Source Test

src/helpers/consent/ConsentStrategy.js

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

/**
 * Helper class representing a Consent Strategy.
 *
 * @extends {ConsentStrategyModel}
 * @extends {Helper}
 *
 * @example
 * // To get a list of ConsentStrategies use clinical6.get
 * import { clinical6, ConsentStrategy } from 'clinical6';
 * clinical6.get(ConsentStrategy).then(c => console.log(c));
 *
 * // To get a single ConsentStrategy, you can also use clinical6
 * clinical6.get({ id: 5, type: 'consent__strategies'});
 *
 * // To save or insert, you can either use the .save() capability or clinical6
 * myConsentStrategy.save(); // insert if no id, save if id
 * clinical6.insert(new ConsentStrategy({...}));
 * clinical6.update(myConsentStrategy);
 *
 * // To delete, use clinical6 or .delete();
 * myConsentStrategy.delete();
 * clinical6.delete(myConsentStrategy);
*/
class ConsentStrategy extends aggregate(ConsentStrategyModel, Helper) {
  /**
   * Constructor for helper class representing a Consent Strategy
   *
   * @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__strategies';
  }

  /** @type {Cohort} */
  get cohort() {
    return this._relationships.cohort;
  }

  /** @type {Cohort} */
  set cohort(cohort) {
    /** @type {Cohort} */
    this._relationships.cohort = cohort;
  }

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

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

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

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

export default ConsentStrategy;