azry-leasing/server.js

import { JsonApiServer } from '../json_api'

/**
 * Facilitates interaction with the AZRY leasing server.
 *
 * @class
 */
export class AzryLeasingServer extends JsonApiServer {
  /**
   * Create a new Azry Leasing server instance.
   *
   * @constructor
   * @param {ShelfNetwork} sdk Parent SDK instance.
   * @param {string} serverUrl AzryLeasing 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.
   */
  constructor (sdk, serverUrl, opts = {}) {
    opts.alias = 'azry-leasing'
    super(sdk, serverUrl, opts)
  }

  /**
   * Get multiple leasing statements
   *
   * @param {string} opts.sort Sort requests by: created_at, updated_at, lot_end_time
   * @param {string} opts.status Filter by request status: created, confirmed
   * @param {string} opts.lotId Filter by lot id
   * @param {string} opts.clientId Filter by user account id
   */
  async getStatements (opts = {}) {
    return this._makeCallBuilder()
      .get(opts)
  }

  /**
   * Get leasing statement by ID.
   *
   * @param {string} lotId Lot ID.
   * @param {string} clientId User accound ID
   * @param {object} opts Request misc options
   * @param {string} opts.status Request status
   * @param {string} opts.sort Sort by created_at/updated_at
   *
   * @return {JsonApiResponse}
   */
  async getStatement (lotId, clientId, opts = {}) {
    return this._makeCallBuilder()
      .get({ lotId, clientId, ...opts })
  }

  /**
   * Get multiple fees.
   *
   * @param {object} attributes Leasing statement attributes.
   * @param {string} attributes.initialInstallment Initial payment.
   * @param {string} attributes.amount Estimated price of the lot.
   * @param {string} attributes.currency Leasing currency.
   * @param {string} attributes.paymentsNumber Desired number of payments.
   * @param {string} attributes.clientId User's account ID.
   * @param {string} attributes.clientNationalId User's national ID.
   * @param {string} attributes.clientName User's full name.
   * @param {string} attributes.clientPhoneNumber User's phone number.
   * @param {string} attributes.lotId Lot ID
   * @param {string} attributes.lotLink Link to the lot.
   *
   * @return {JsonApiResponse}
   */
  createStatement (attributes) {
    return this._makeCallBuilder().post({
      data: {
        type: 'statements',
        attributes
      }
    })
  }

  _makeCallBuilder () {
    return super._makeCallBuilder()
      .withCredentials()
      .appendUrlSegment('statements')
  }
}