reporteSearch.js 882 Bytes
const createError = require('http-errors')
const su = require('superagent')
const {
  isNil, path, head, ifElse, always, prop,
} = require('ramda')


const dbhost = process.env.COUCHDB_URL
const dbname = process.env.DB_REPORTES

const headers = {
  'Accept': 'application/json',
  'Content-Type': 'application/json',
}

const findReporte = (payload) =>
  su.post(`${dbhost}/${dbname}/_find`)
    .set(headers)
    .send(JSON.stringify(payload))
    .then(path(['body', 'docs']))
    .then(head)
    .then(ifElse(isNil, always(null), prop('reporte')))

module.exports = (req, res, next) => {
  const { id, mail } = req.params
  const params = {
    selector: {
      id: `${id}`,
      usuario: { mail }
    }
  }

  findReporte(params)
    .then((reporte) => res.json({ reporte, success: true, error: false }))
    .catch(error => next(createError(500, error, { expose: true })))
}