Manual Reference Source Test

src/helpers/Device.js

import Client from '../Client';
import DeviceModel from '../models/Device';
import DeviceService from '../services/DeviceService';
import Helper from './Helper';
import { aggregate } from '../utilities/ClassUtility';

/**
 * Helper class representing a device.
 *
 * @extends {DeviceModel}
 * @extends {Helper}
 *
 * @example
 * import { Device, deviceService } from 'clinical6';
 *
 * // Typically use DeviceService.get()
 * deviceService.get().then(devices => console.log(devices));
 *
 * const device = new Device({
 *   "data": {
 *     "id": "1",
 *     "type": "devices",
 *     "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 Device extends aggregate(DeviceModel, Helper) {
  /** @type {String}  - The type */
  static get type() {
    return 'devices';
  }

  /** @type {String} */
  get uuid() {
    return this.udid;
  }

  /** @type {String} */
  set uuid(udid) {
    /** @type {String} - The udid as uuid in case used */
    this.udid = udid;
  }

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

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

  /**
   * Select this device as the main device to use for authentication and http calls
   * @return {Device} - Return this device
   */
  select() {
    Client.instance.authToken = this.accessToken;
    Client.instance.device = this;
    return this;
  }
}

export default Device;