cart/server.js

import { JsonApiServer } from '../json_api'

/**
 * Facilitates interaction with the cart server.
 *
 * @class
 */
export class CartServer extends JsonApiServer {
  /**
   * Create a new cart server instance.
   *
   * @constructor
   * @param {ShelfNetwork} sdk Parent SDK instance.
   * @param {string} serverUrl cart URL.
   * @param {Object} opts
   * @param {boolean} [opts.allowHttp] Allow connecting to http servers, default: `false`. This must be set to false in production deployments!
   * @param {Object} [opts.proxy] Proxy configuration. Look [axios docs](https://github.com/axios/axios#request-config) for more info
   * @param {Object} [opts.httpBasicAuth] HTTP basic auth credentials. Look [axios docs](https://github.com/axios/axios#request-config) for more info.
   * @param {Object} [opts.customHeaders] Custom headers for request.
   * @param {boolean} [opts.withCredentials] Indicates whether or not cross-site Access-Control requests should be made using credentials.
   * @param {string} [opts.responseType='json'] Indicates the type of data that the server will respond with options are 'arraybuffer', 'blob', 'document', 'json', 'text', 'stream'.
   */
  constructor (sdk, serverUrl, opts = {}) {
    opts.alias = 'cart'
    super(sdk, serverUrl, opts)
  }

  /**
   * Create service.
   *
   * @param {string} name Service name.
   * @param {Number} price Service price.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async createService (name, price) {
    return this._makeCallBuilder()
      .appendUrlSegment('services')
      .withCredentials()
      .post({
        data: {
          type: 'services',
          id: name,
          attributes: {
            price: price
          }
        }
      })
  }

  /**
   * Update service.
   *
   * @param {string} name Service name.
   * @param {Number} price Service price.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async updateService (name, price) {
    return this._makeCallBuilder()
      .appendUrlSegment('services')
      .withCredentials()
      .patch({
        data: {
          type: 'services',
          id: name,
          attributes: {
            price: price
          }
        }
      })
  }

  /**
   * Delete service.
   *
   * @param {string} name Service name.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async deleteService (name) {
    return this._makeCallBuilder()
      .appendUrlSegment(['services', name])
      .withCredentials()
      .delete()
  }

  /**
   * Create request.
   *
   * @param {string} opts
   * @param {string} opts.service Service name.
   * @param {string} [opts.accountId] Account ID of requestor.
   * @param {string} [opts.token] Request token. The meaning of token is specific for each service.
   * @param {Number} [opts.lotId] ID of the lot that service is requested for.
   * @param {string} [opts.email] Email (for unauthorized users).
   * @param {Object} [opts.details] Request details.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async createRequest (opts) {
    return this._makeCallBuilder()
      .appendUrlSegment('requests')
      .withOptionalCredentials()
      .post({
        data: {
          type: 'requests',
          attributes: {
            service: opts.service,
            accountId: opts.accountId,
            lotId: opts.lotId,
            token: opts.token,
            email: opts.email,
            details: opts.details
          }
        }
      })
  }

  /**
   * Set documents.
   *
   * @param {Number} id Request ID.
   * @param {string[]} documents Document IDs.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async setDocuments (id, documents) {
    return this._makeCallBuilder()
      .appendUrlSegment('requests')
      .withCredentials()
      .patch({
        data: {
          type: 'requests',
          id: id,
          attributes: {
            documents: documents
          }
        }
      })
  }

  /**
   * Set links.
   *
   * @param {Number} id Request ID.
   * @param {string[]} links Links to documents.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async setLinks (id, links) {
    return this._makeCallBuilder()
      .appendUrlSegment('requests')
      .withCredentials()
      .patch({
        data: {
          type: 'requests',
          id: id,
          attributes: {
            links: links
          }
        }
      })
  }

  /**
   * Get services.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async getServices (query) {
    return this._makeCallBuilder()
      .appendUrlSegment('services')
      .get(query)
  }

  /**
   * Get carfax reports count.
   *
   * @param {string} vin Vin code.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async getCarfaxReportsCount (vin) {
    return this._makeCallBuilder()
      .appendUrlSegment(['services', 'carfax', 'records', vin])
      .get()
  }

  /**
   * Get requests.
   *
   * @param {object} [query] Request options.
   * @param {string} [query.accountId] Account ID of the requestor.
   * @param {string} [query.service] Name of the service.
   * @param {Number} [query.lotId] ID of the lot that service is requested for.
   * @param {Number} [query.page.number] Page number.
   * @param {Number} [query.page.size] Page size.
   * @param {string} [query.search] Search by 'token' field.
   * @param {string} [query.sort] Sort. Default '-created_at'.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async getPage (query) {
    return this._makeCallBuilder()
      .appendUrlSegment('requests')
      .withCredentials()
      .get(query)
  }

  /**
   * Get request by ID.
   *
   * @param {Number} id Request ID.
   *
   * @return {Promise.<JsonApiResponse>} Cart response.
   */
  async get (id) {
    return this._makeCallBuilder()
      .appendUrlSegment(['requests', id])
      .withCredentials()
      .get()
  }
}