All files / lib/resources antifraudAnalyses.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                                                                                                                                                             
/**
 * @name Antifraud Analyses
 * @description This module exposes functions
 *              related to the `/transactions/:transactionId/antifraud_analyses` path.
 *
 * @module antifraudAnalyses
 **/
 
import {
  both,
  cond,
  curry,
  has,
  omit,
} from 'ramda'
 
import routes from '../routes'
import request from '../request'
 
const findAll = curry((opts, body) =>
  request.get(opts, routes.antifraudAnalyses.findAll(body.transactionId), {})
)
 
const findOne = curry((opts, body) => {
  const { transactionId, id } = body
  return request.get(opts, routes.antifraudAnalyses.find(transactionId, id), {})
})
 
/**
 * `GET /transactions/:transactionId/antifraud_analyses`
 * Makes a request to /transactions/:transactionId/antifraud_analyses or
 * to /transactions/:transactionId/antifraud_analyses/: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-uma-análise-antifraude|API Reference for this payload}
 * @param {Number} body.transactionId - A specific transaction ID
 *
 * @param {Number} [body.id] - The antifraud analyses' ID. If not sent,
 *                             a antifraud analyses list will be returned instead.
 */
const find = (opts, body) =>
  cond([
    [both(has('transactionId'), has('id')), findOne(opts)],
    [has('transactionId'), findAll(opts)],
  ])(body)
 
/**
 * `POST /transactions/:transactionId/antifraud_analyses`
 * Makes a request to /transactions/:transactionId/antifraud_analyses
 *
 * @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-análise-antifraude|API Reference for this payload}
 * @param {Number} body.transactionId - A specific transaction ID
 *
 * @returns {Promise} Resolves to the result of
 *                    the request or to an error.
 */
const create = (opts, body) => {
  const { transactionId } = body
  return request.post(
    opts,
    routes.antifraudAnalyses.create(transactionId),
    omit(['transactionId'], body)
  )
}
 
export default {
  find,
  create,
}