Manual Reference Source Test

src/helpers/reminder/Rule.js

// import Client from '../../Client';
import RuleModel from '../../models/reminder/Rule';
import RuleService from '../../services/RuleService';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';

/**
 * Helper class representing a rule.
 *
 * @extends {RuleModel}
 * @extends {Helper}
 *
 * @example
 * import { Rule, ruleService } from 'clinical6';
 *
 * // Typically use RuleService.get()
 * ruleService.get().then(rules => console.log(rules));
 *
 * const rule = new Rule({
 *   "data": {
 *     "id": "1",
 *     "type": "rules",
 *     "attributes": {
 *       "udid": "this-is-a-udid-string",
 *       "technology": "ios",
 *       "access_token": "cd68fa04e458d6d1a9d29faec6a329d3",
 *       "push_id": null,
 *       "created_at": "2017-05-19T17:21:26.311Z",
 *       "updated_at": "2017-05-19T17:21:26.311Z",
 *       "app_version": null
 *     }
 *   }
 * });
 */
class Rule extends aggregate(RuleModel, Helper) {
  /**
   * Constructor for helper class representing an Rule
   *
   * @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 'reminder__rules';
  }

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

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

  /**
   * Saves a rule (insert if id doesn't exist, update if it does)
   * @return {Promise} - Returns a promise via ajax call.
   *
   * @example
   * import { Rule, Client } from 'clinical6';
   *
   * // Removes rule from server and local storage
   * const rule = new Rule({
   *   "id": 1,
   *   "type": "rules",
   *   "attributes": {
   *     "udid": "this-is-a-udid-string",
   *     "technology": "ios",
   *     "access_token": "cd68fa04e458d6d1a9d29faec6a329d3",
   *     "push_id": null,
   *     "created_at": "2017-05-19T17:21:26.311Z",
   *     "updated_at": "2017-05-19T17:21:26.311Z",
   *     "app_version": null
   *   }
   * });
   * rule.delete();
   *
   * // No longer in storage
   * Client.instance.storageUtility.has('rules', { id: 1 });
   */
  delete() {
    return (new RuleService()).delete(this);
  }

  /**
   * Saves a rule (insert if id doesn't exist, update if it does)
   * @return {Promise<Rule>} - Returns a promise via ajax call.
   *
   * @example
   * import { Rule, ruleService } from 'clinical6';
   *
   * // Insert is different from other inserts.  Uses mobile application key
   * const rule = new Rule({
   *   "type": "rules",
   *   "attributes": {
   *     "udid": "this-is-a-udid-string",
   *     "technology": "ios",
   *     "access_token": "cd68fa04e458d6d1a9d29faec6a329d3",
   *     "push_id": null,
   *     "created_at": "2017-05-19T17:21:26.311Z",
   *     "updated_at": "2017-05-19T17:21:26.311Z",
   *     "app_version": null
   *   }
   * });
   * rule.save();
   *
   * // After you have the authtoken and then you've logged in
   *
   * // Updates existing rule (has existing id)
   * ruleService.get().then(rules => rules[0].save());
   */
  save() {
    // return (this.id) ? (new RuleService()).update(this) : (new RuleService()).insert(this);
    return (new RuleService()).insert(this);
  }

  // old v2 methods

  /**
   * Returns the next event for the rule. A 404 status will be returned
   * if there isn't a next rule.
   * @return {Promise<Event>} Returns a promise via ajax call.
   */
  nextEvent() {
    return (new RuleService()).getNextEvent(this.id);
  }

  /**
   * Returns the most recent events for the rule. The optional time unit caps
   * the amount of returned events by length of time.
   *
   * @param timeUnit {String} - The time cap for recent events. Must be either 'week'
   * or 'month'. This parameter is optional.
   * @return {Promise} Returns a promise via ajax call.
   */
  recentEvents(timeUnit) {
    return (new RuleService()).getFilteredRecentEvents(this.id, timeUnit);
  }

  /**
   * Returns the events that will occur soon for the rule. The optional time unit caps
   * the amount of returned events by length of time.
   *
   * @param timeUnit {String} - The time cap for recent events. Must be either 'week'
   * or 'month'. This parameter is optional.
   * @return {Promise} Returns a promise via ajax call.
   */
  upcomingEvents(timeUnit) {
    return (new RuleService()).getUpcomingEvents(this.id, timeUnit);
  }

  /**
   * Returns all the events for the rule on the given date.
   *
   * @param date {String} - The date to search for events for. The format of the
   * date must be YYYY-MM-DD. This paramater is optional.
   *
   * @return {Promise} Returns a promise via ajax call.
   */
  eventsByDate(date) {
    return (new RuleService()).getEventsByDate(this.id, date);
  }
}

export default Rule;