Manual Reference Source Test

src/helpers/trial/Site.js

import SiteModel from '../../models/trial/Site';
import { serviceFactory } from '../../utilities/factories/ServiceFactory';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';
import SiteLanguage from './SiteLanguage';
// import SiteContact from './SiteContact';

/**
 * Helper class representing an site.
 *
 * @extends {SiteModel}
 * @extends {Helper}
 *
 * @example
 * import { Site, clinical6 } from 'clinical6';
 *
 * // Typically use clinical6.get(Site)
 * clinical6.get(Site).then(sites => console.log(sites));
 *
 * const site = new Site({
 *   data: {
 *     id: '15',
 *     type: 'trials__sites',
 *     attributes: {
 *       site_id: '922176',
 *       name: 'Frankie Hermann',
 *       email: 'lisa@kulas.info',
 *       phone_number: null,
 *       fax_number: null
 *     },
 *     relationships: {
 *       contact: {
 *         data: null
 *       },
 *       location: {
 *         data: {
 *           id: '98',
 *           type: 'locations'
 *         }
 *       },
 *       agreement_templates: {
 *         data: []
 *       }
 *     }
 *   }
 * });
 */
class Site extends aggregate(SiteModel, Helper) {
  /**
   * Constructor for helper class representing an Site
   *
   * @param {Object} json - json api response from server
   */
  constructor(json = {}) {
    super(json);
    // this._relationships = {};
    // this._relationships.site_languages = [];
    this.deserializeRelationshipStubs(json);
    this.syncRelationships(json);
  }

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

  /** @type {AgreementTemplate[]} */
  get agreementTemplates() {
    return this._relationships.agreement_templates;
  }

  /** @type {AgreementTemplate[]} */
  set agreementTemplates(agreementTemplates) {
    /** @type {AgreementTemplate[]} */
    this._relationships.agreement_templates = agreementTemplates;
  }

  /** @type {SiteContact[]} */
  get contacts() {
    return this._relationships.site_contacts;
  }

  /** @type {SiteContact[]} */
  set contacts(contacts) {
    /** @type {SiteContact[]} */
    this._relationships.site_contacts = contacts;
  }

  /** @type {Location} */
  get location() {
    return this._relationships.location;
  }

  /** @type {Location} */
  set location(location) {
    /** @type {Location} */
    this._relationships.location = location;
  }

  /** @type {SiteLanguage[]} */
  get siteLanguages() {
    return this._relationships.site_languages;
  }

  /** @type {SiteLanguage[]} */
  set siteLanguages(siteLanguages) {
    /** @type {SiteLanguage[]} */
    this._relationships.site_languages = siteLanguages;
  }

  /**
   * Gets the supported languages associated to this site.
   *
   * @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 { Site, clinical6 } from 'clinical6';
   * const site = new Site({ id: 23 });
   * site.getSiteLanguages({ id: 5 });
   *
   * // Combined with clinical6.get
   * clinical6.get(Site).then(sites => { sites[0].getSiteLanguages() });
   */
  async getSiteLanguages(options = {}) {
    const service = serviceFactory.get(this.type);
    const siteLanguage = { type: SiteLanguage.type };

    const response = await service.getChildren(this, siteLanguage, options);
    if (Array.isArray(response)) {
      this.siteLanguages = response;
    } else {
      // otherwise, there is just one response, make it an array
      this.siteLanguages = [response];
    }
    return this.siteLanguages;
  }

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

  /**
   * Gets the site members associated to this site.
   *
   * @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 { Site, clinical6 } from 'clinical6';
   * const site = new Site({ id: 23 });
   * site.getMembers({ id: 5 });
   * site.getMembers({ filters: { member_type: 'patient' }});
   *
   * // Combined with clinical6.get
   * clinical6.get(Site).then(sites => { sites[0].getMembers() });
   */
  getMembers(params = {}, options = {}) {
    const service = serviceFactory.get(this.type);
    const members = { type: 'trials__site_members' };
    if (params.id) {
      members.id = params.id;
    } else if (params.filters) {
      members.filters = params.filters;
    }
    return service.getChildren(this, members, options);
  }

  /**
   * Gets the site contacts associated to this site.
   *
   * @return {Promise}                    - Promise with data (array or object)
   *
   * @example
   * import { Site, clinical6 } from 'clinical6';
   * const site = new Site({ id: 23 });
   * site.getContacts();
   *
   * // Combined with clinical6.get
   * clinical6.get(Site).then(sites => { sites[0].getContacts() });
   */
  async getContacts() {
    const service = serviceFactory.get(this.type);
    const contacts = { type: 'trials__site_contacts' };

    return service.getChildren(this, contacts);
  }

  /**
   * Saves a site (insert if id doesn't exist, update if it does)
   * @return {Promise<Site>} - Returns a promise via ajax call.
   *
   * @example
   * import { Site, clinical6 } from 'clinical6';
   *
   * // Insert is different from other inserts.  Uses mobile application key
   * const site = new Site({
   *   "title": "You are a winner"
   * });
   * site.save();
   *
   * // After you have the authtoken and then you've logged in
   *
   * // Updates existing site (has existing id)
   * clinical6.get(Site).then(sites => sites[0].save());
   */
  save() {
    const service = serviceFactory.get(this.type);
    return (this.id) ? service.update(this) : service.insert(this);
  }
}

export default Site;