Manual Reference Source Test

src/helpers/Notification.js

import NotificationModel from '../models/Notification';
import Helper from './Helper';
import { aggregate } from '../utilities/ClassUtility';
import { serviceFactory } from '../utilities/factories/ServiceFactory';
import {
  hasAttribute,
  validate,
} from '../utilities/ValidationUtility';


/**
 * Helper class representing a user Notification.
 *
 * @extends {NotificationModel}
 * @extends {Helper}
 *
 * // Typically use notificationService.get()
 * notificationService.get().then(notifications => console.log(notifications));
 *
 * const notification = new Notification({
 *    "id": "5",
      "type": "notification__deliveries",
      "attributes": {
        "action": null,
        "action_object": null,
        "channels": [
          "push notification"
        ],
        "message": "Some message",
        "status": "pending",
        "title": "Some title",
        "opts": null,
        "created_at": "2017-11-02T21:04:57Z",
        "updated_at": "2017-11-02T21:04:57Z"
 * });
 */
class Notification extends aggregate(NotificationModel, Helper) {
  /**
   * Constructor for helper class representing a Notification
   *
   * @param {Object} json - json api response from server
   */
  constructor(json = {}) {
    super(json);

    if (json.relationships) {
      /** @type {Object} */
      this._relationships = json.relationships;
      this.syncRelationships();
    }
  }

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

  /**
   * Sets the status of the current instance of Notification to read
   * @return {Promise<Notification>} - Returns a promise via ajax call.
   *
   * @example
   * import { Notification } from 'clinical6';
   *
   * // Read notification from manually created notification
   * notification = new Notification({ id: '16' });
   * notification.read();
   *
   * // Read notification from the list of notifications
   * notificationService.get().then(notifications => notifications[0].read());
   */
  async read() {
    /** @type {String} - a notification status (e.g. 'pending', 'completed', 'read') */
    this.status = 'read';
    const service = serviceFactory.get(this.type);
    return service.update(this);
  }

  /**
   * Updates an existing notification (insert is not currently supported)
   * @return {Promise<Notification>} - Returns a promise via ajax call.
   *
   * @example
   * import { Notification, notificationService } from 'clinical6';
   *
   * const notification = new Notification({ id: 16, ...});
   *
   * // Updates notification
   * notification.save();
   */
  async save() {
    const service = serviceFactory.get(this.type);
    validate('Notification.save',
      hasAttribute({ notification: this }, 'id'));
    return service.update(this);
  }

  /**
   * Deletes a notification
   * @return {Promise<Notification>} - Returns a promise via ajax call.
   *
   * @example
   * import { Notification, notificationService } from 'clinical6';
   *
   * const notification = new Notification({...});
   *
   * // Deletes notification
   * notification.delete();
   */
  async delete() {
    const service = serviceFactory.get(this.type);
    return service.delete(this);
  }
}

export default Notification;