Manual Reference Source Test

src/services/CallbackService.js

import Client from '../Client';
import { validate, hasToken, isRequired } from '../utilities/ValidationUtility';

/**
 * Service handling Callback calls with specific endpoints.
 *
 */
class CallbackService {
  /**
   * Returns the information associated with a callback or all callbacks if no id present.
   *
   * @throws {Clinical6Error} - If missing token or missing required parameters
   * @param  {Number} [id]    - The ID value of the callback.
   * @return {Promise}        - callback or list of callbacks
   */
  get(id) {
    return (id) ? this.getCallback(id) : this.getCallbacks();
  }

  /**
   * Validates the conditions and executes a single callback.
   *
   * @param id - {Number} The ID value of the callback.
   *
   * @return {Promise} Promise object that returns success or failure
   */
  execute(id) {
    validate('CallbackService.execute',
      hasToken(),
      isRequired({ id }));

    return new Promise((resolve, reject) => {
      Client.instance.fetch(`/api/callbacks/${id}/execute`).then(
        response => resolve(response),
        err => reject(err)
      );
    });
  }

  /**
   * Returns the information associated with a callback.
   *
   * @param id - {Number} The ID value of the callback.
   *
   * @return {Promise} Promise object that returns success or failure
   */
  getCallback(id) {
    validate('CallbackService.getCallback',
      hasToken(),
      isRequired({ id }));

    return new Promise((resolve, reject) => {
      Client.instance.fetch(`/api/callbacks/${id}`).then(
        response => resolve(response),
        err => reject(err)
      );
    });
  }

  /**
   * Returns all the callbacks.
   *
   * @return {Promise} Promise object that returns success or failure
   */
  getCallbacks() {
    validate('CallbackService.getCallbacks',
      hasToken());

    return new Promise((resolve, reject) => {
      Client.instance.fetch(`/api/callbacks`).then(
        response => resolve(response),
        err => reject(err)
      );
    });
  }
}

export default CallbackService;