All files / lib/resources gatewayOperations.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                                                                                                                                     
/**
 * @name Gateway Operations
 * @description This module exposes functions
 *              related to events.
 *
 * @module gatewayOperations
 **/
 
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.gatewayOperations.transaction(body.transactionId), {}))
 
const findOneInTransactions = curry((opts, body) =>
  request.get(opts, routes.gatewayOperations.transactionDetails(body.id, body.transactionId), {})
)
 
const findAllInSubscriptions = curry((opts, body) =>
  request.get(opts, routes.gatewayOperations.subscription(body.subscriptionId), {}))
 
/**
 * `GET /:model/:model_id/operations`
 *
 * @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-todo-histórico-de-uma-transação|API Reference for this payload}
 * @param {Number} [body.id] The operation ID. If not sent,
 *                                             all operations of a
 *                                             transaction will be returned.
 * @param {Number} [body.transactionId] A transaction ID to get all
 *                                      the operations.
 * @param {Number} [body.subscriptionId] A subscription ID
*/
const find = (opts, body) =>
  cond([
    [both(has('transactionId'), has('id')), findOneInTransactions(opts)],
    [has('transactionId'), findAllInTransactions(opts)],
    [has('subscriptionId'), findAllInSubscriptions(opts)],
    [T, findAllInTransactions(opts)],
  ])(body)
 
/**
 * `GET /:model/:model_id/operations/:id/refuse_message`
 * Returns the refuse message for a gateway operation
 *
 * @param {Object} opts An options params which
 *                      is usually already bound
 *                      by `connect` functions.
 *
 * @param {Object} body The payload for the request.
 * @param {Number} body.id The operation ID.
 * @param {Number} [body.subscriptionId] A subscription ID
*/
const refuseMessage = (opts, body) =>
  request.get(opts, routes.gatewayOperations.refuseMessage(body.subscriptionId, body.id), {})
 
export default {
  find,
  refuseMessage,
}