All files / lib/resources payables.js

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94                                                                                                                                                                                           
/**
 * @name Payables
 * @description This module exposes functions
 *              related to payables.
 *
 * @module payables
 **/
 
import {
  both,
  cond,
  curry,
  has,
  T,
} from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
const findTransaction = curry((opts, body) =>
  request.get(opts, routes.payables.transaction(body.transactionId), {}))
 
const findOne = curry((opts, body) =>
  request.get(opts, routes.payables.find(body.id), {}))
 
const findAll = curry((opts, pagination) =>
  request.get(opts, routes.payables.base, pagination))
 
const findPayablesWithFormat = curry((opts, body) => {
  const { format } = body
 
  return request.get(
    opts,
    routes.payables.findWithFormat(format),
    body
  )
})
/**
 * `GET /payables`
 * Makes a request to /payables or to /payables/:id
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Object} body The payload for the request.
 * {@link https://pagarme.readme.io/v1/reference#retornando-um-recebível|API Reference for this payload}
 * @param {Number} [body.id] The payable ID. If not sent a
 * @param {Number} [body.transactionId] A transaction ID to get all
 *                                      the payables.
 * @param {Number} [body.count] Pagination option for transaction list.
 *                              Number of transaction in a page
 * @param {Number} [body.page] Pagination option for transaction list.
 *                             The page index.
*/
const find = (opts, body) =>
  cond([
    [has('id'), findOne(opts)],
    [both(has('recipient_id'), has('format')), findPayablesWithFormat(opts)],
    [has('transactionId'), findTransaction(opts)],
    [T, findAll(opts)],
  ])(body)
 
/**
 * `GET /payables`
 * Makes a request to /payables
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Object} body The payload for the request.
 * {@link https://docs-beta.pagar.me/v1/reference#retornando-recebíveis}
*/
const all = (opts, body) =>
  findAll(opts, body)
 
/**
 * `GET /payables/days`
 * Returns a company's payables  aggregated by day
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
*/
const days = (opts, body) =>
  request.get(opts, routes.payables.days, body)
 
export default {
  find,
  all,
  days,
}