All files / lib/resources bulkAnticipations.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 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165                                                                                                                                                                                                                                                                                                                                         
/**
 * @name Bulk Anticipations
 * @description This module exposes functions
 *              related to the `/recipients/:recipient_id/bulk_anticipations` path.
 *
 * @module bulkAnticipations
 **/
 
import {
  T,
  cond,
  curry,
  dissoc,
  has,
} from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
 
const findOne = curry((opts, body) =>
  request.get(opts, routes.bulkAnticipations.details(body.recipientId, body.id), {})
)
 
const findAll = curry((opts, body) =>
  request.get(
    opts,
    routes.bulkAnticipations.base(body.recipientId),
    dissoc('recipientId', body)
  )
)
 
/**
 * `GET /recipients/:recipient_id/bulk_anticipations`
 * Makes a request to /recipients/:recipient_id/bulk_anticipations
 * or to /recipients/:recipient_id/bulk_anticipations/: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-todas-as-antecipações|API Reference for this payload}
 * @param {String} body.recipientId The recipient ID.
 * @param {String} [body.id] The bulkAnticipation ID. If not sent a
 *                           bulkAnticipation list will be returned instead.
 * @param {Number} [body.count] Pagination option for bulkAnticipation list.
 *                              Number of bulkAnticipation in a page
 * @param {Number} [body.page] Pagination option for bulkAnticipation list.
 *                             The page index.
*/
const find = (opts, body) =>
  cond([
    [has('id'), findOne(opts)],
    [T, findAll(opts)],
  ])(body)
 
const all = (opts, body) =>
  findAll(opts, body)
 
/**
 * `POST /recipients/:recipient_id/bulk_anticipations`
 * Creates a bulkAnticipation from the given payload.
 *
 * @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#criando-uma-antecipação|API Reference for this payload}
 * @param {String} body.recipientId The recipient ID.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const create = (opts, body) =>
  request.post(opts, routes.bulkAnticipations.base(body.recipientId), body)
 
 
/**
 * `GET /recipients/:recipient_id/bulk_anticipations/limits`
 * Get bulk anticipations limits for a recipient.
 *
 * @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#obtendo-os-limites-de-antecipação|API Reference for this payload}
 * @param {String} body.recipientId The recipient ID.
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const limits = (opts, body) =>
  request.get(
    opts,
    routes.bulkAnticipations.limits(body.recipientId),
    dissoc('recipientId', body)
  )
 
/**
 * `GET /recipients/:recipient_id/bulk_anticipations/:id/days`
 * Get bulk anticipations days.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {Object} body The payload for the request
 * @param {String} body.recipientId The recipient ID.
 * @param {String} body.id The bulkAnticipation Id
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const days = (opts, body) =>
  request.get(opts, routes.bulkAnticipations.days(body.recipientId, body.id), body)
 
/**
 * `POST /recipients/:recipient_id/bulk_anticipations/:id/cancel`
 * Cancel a bulk anticipation.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {Object} body The payload for the request
 * @param {String} body.recipientId The recipient ID.
 * @param {String} body.id The bulkAnticipation Id
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const cancel = (opts, body) =>
  request.post(opts, routes.bulkAnticipations.cancel(body.recipientId, body.id), body)
 
/**
 * `GET /recipients/:recipient_id/bulk_anticipations/simulate`
 * Performs anticipation simulation for a recipient.
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 * @param {Object} query The payload for the request
 * @param {String} query.recipientId The recipient ID.
 * @param {Number} query.requested_amount Value required to perform the simulation (in cents).
 * @param {Number} query.payment_date Anticipation payment date to be simulated (in milliseconds).
 * @param {String} query.timeframe Anticipation timeframe (start, end).
 *
 * @returns {Promise} Resolves to the result of the request or to an error.
 */
const simulate = (opts, query) =>
  request.get(
    opts,
    routes.bulkAnticipations.simulate(query.recipientId),
    dissoc('recipientId', query)
  )
 
export default {
  find,
  all,
  create,
  limits,
  days,
  cancel,
  simulate,
}