Manual Reference Source Test

src/helpers/reminder/Event.js

// import Client from '../../Client';
import EventModel from '../../models/reminder/Event';
import EventService from '../../services/EventService';
import RuleService from '../../services/RuleService';
import Helper from '../Helper';
import { aggregate } from '../../utilities/ClassUtility';

/**
 * Helper class representing a event.
 *
 * @extends {EventModel}
 * @extends {Helper}
 *
 * @example
 * import { Event, eventService } from 'clinical6';
 *
 * // Typically use EventService.get()
 * eventService.get().then(events => console.log(events));
 *
 * const event = new Event({
 *   "data": {
 *     "id": "1",
 *     "type": "events",
 *     "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 Event extends aggregate(EventModel, Helper) {
  /**
   * Constructor for helper class representing an Event
   *
   * @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__events';
  }

  /** @type {Rule} */
  get rule() {
    return this._relationships.rule;
  }

  /** @type {Rule} */
  set rule(rule) {
    /** @type {Rule} */
    this._relationships.rule = rule;
  }

  /** @type {User} */
  get mobileUser() {
    return this._relationships.mobile_user;
  }

  /** @type {User} */
  set mobileUser(mobileUser) {
    /** @type {User} */
    this._relationships.mobile_user = mobileUser;
  }

  /**
   * Saves a event (insert if id doesn't exist, update if it does)
   * @return {Promise} - Returns a promise via ajax call.
   *
   * @example
   * import { Event, Client } from 'clinical6';
   *
   * // Removes event from server and local storage
   * const event = new Event({
   *   "id": 1,
   *   "type": "events",
   *   "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
   *   }
   * });
   * event.delete();
   *
   * // No longer in storage
   * Client.instance.storageUtility.has('events', { id: 1 });
   */
  delete() {
    return (new EventService()).delete(this);
  }

  /**
   * Saves a event (insert if id doesn't exist, update if it does)
   * @return {Promise<Event>} - Returns a promise via ajax call.
   *
   * @example
   * import { Event, eventService } from 'clinical6';
   *
   * // Insert is different from other inserts.  Uses mobile application key
   * const event = new Event({
   *   "type": "events",
   *   "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
   *   }
   * });
   * event.save();
   *
   * // After you have the authtoken and then you've logged in
   *
   * // Updates existing event (has existing id)
   * eventService.get().then(events => events[0].save());
   */
  save() {
    return (this.id) ? (new EventService()).update(this) : (new EventService()).insert(this);
  }

  // old v2 methods

  /**
   * Saves the current instance of Event
   *
   * @param  {!String} ruleId       - Permanent link of hte rule of the event to update
   * @param  {!String} eventAction  - The optional event action indicates the action to mark the
   *                                  event
   * @return {Promise}              - Returns a promise via ajax call.
   */
  mark(ruleId, eventAction) {
    return (new RuleService()).markEvent(ruleId, this.id, eventAction);
  }

  // old v2 methods

  /**
   * Saves/Tracks reported actions from an event - if a mobile user token is available
   *
   * @param  {String} resource  - resource attribute
   * @param  {String} action    - action attribute
   * @param  {String} context   - result of if this was approved or not
   *
   * @return {Promise<Event>} Returns a promise via ajax call.
   */
  saveEventAction(resource, action, context) {
    return (new EventService()).trackEvents(resource, action, context);
  }
}

export default Event;