reporteSearch.js
881 Bytes
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
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)))
}