All files / lib/resources events.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                                                                                                                                         
/**
 * @name Events
 * @description This module exposes functions
 *              related to events.
 *
 * @module events
 **/
 
import { cond, has, T, curry, both } from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
const findAllInTransactions = curry((opts, body) =>
  request.get(opts, routes.events.transaction(body.transactionId), {}))
 
const findOneInTransactions = curry((opts, body) =>
  request.get(opts, routes.events.transactionDetails(body.id, body.transactionId), {}))
 
const findAllInSubscriptions = curry((opts, body) =>
  request.get(opts, routes.events.subscription(body.subscriptionId), {}))
 
const findOneInSubscriptions = curry((opts, body) =>
  request.get(opts, routes.events.subscriptionDetails(body.id, body.subscriptionId), {}))
 
/**
 * `GET /:model/:model_id/events`
 *
 * @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-todos-os-eventos-de-uma-transação|API Reference for this payload}
 * @param {Number} [body.id] The event ID. If not sent a
 * @param {Number} [body.transactionId] A transaction ID to get all
 *                                      the events.
 * @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([
    [both(has('transactionId'), has('id')), findOneInTransactions(opts)],
    [has('transactionId'), findAllInTransactions(opts)],
    [both(has('subscriptionId'), has('id')), findOneInSubscriptions(opts)],
    [has('subscriptionId'), findAllInSubscriptions(opts)],
    [T, findAllInTransactions(opts)],
  ])(body)
 
/**
 * `GET /events`
 * Makes a request to /events
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Object} body The payload for the request.
*/
const findCustom = (opts, body) =>
  request.get(opts, routes.events.base, body)
 
export default {
  find,
  findCustom,
}