reporteSearch.js 881 Bytes
const createError = require('http-errors')
const su = require('superagent')
const {
  isNil, path, head, identity, ifElse, empty, 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(console.log)
  .catch(error => next(createError(500)))
}