2019-11-18 14:04:51 +00:00
|
|
|
const Report = require('../core/report');
|
|
|
|
|
|
|
|
module.exports = app => {
|
|
|
|
app.get(`/api/report/:name`, async(req, res, next) => {
|
|
|
|
const args = req.query;
|
|
|
|
const requiredArgs = ['clientId'];
|
2019-11-19 09:32:09 +00:00
|
|
|
const argList = requiredArgs.join(',');
|
2019-11-18 14:04:51 +00:00
|
|
|
const hasRequiredArgs = requiredArgs.every(arg => {
|
|
|
|
return args[arg];
|
|
|
|
});
|
|
|
|
|
|
|
|
try {
|
2019-11-19 09:32:09 +00:00
|
|
|
if (!hasRequiredArgs)
|
|
|
|
throw new Error(`Required properties not found [${argList}]`);
|
|
|
|
|
2019-11-18 14:04:51 +00:00
|
|
|
const report = new Report(req.params.name, args);
|
|
|
|
const stream = await report.toPdfStream();
|
|
|
|
res.setHeader('Content-type', 'application/pdf');
|
|
|
|
stream.pipe(res);
|
2019-11-19 09:32:09 +00:00
|
|
|
} catch (error) {
|
|
|
|
next(error);
|
2019-11-18 14:04:51 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|