52 lines
1.4 KiB
JavaScript
52 lines
1.4 KiB
JavaScript
const Report = require('./report');
|
|
const Email = require('./email');
|
|
|
|
module.exports = app => {
|
|
app.get(`/api/report/:name`, async(req, res, next) => {
|
|
const args = req.query;
|
|
const requiredArgs = ['clientId'];
|
|
|
|
const hasRequiredArgs = requiredArgs.every(arg => {
|
|
return args[arg];
|
|
});
|
|
|
|
if (!hasRequiredArgs)
|
|
res.json({message: 'Required params recipient, clientId'});
|
|
|
|
try {
|
|
const report = new Report(req.params.name, args);
|
|
const stream = await report.toPdfStream();
|
|
res.setHeader('Content-type', 'application/pdf');
|
|
stream.pipe(res);
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
|
|
app.get(`/api/email/:name`, async(req, res, next) => {
|
|
const args = req.query;
|
|
const requiredArgs = ['recipient', 'clientId'];
|
|
|
|
const hasRequiredArgs = requiredArgs.every(arg => {
|
|
return args[arg];
|
|
});
|
|
|
|
if (!hasRequiredArgs)
|
|
res.json({message: 'Required params recipient, clientId'});
|
|
|
|
try {
|
|
const email = new Email(req.params.name, args);
|
|
if (args.isPreview === 'true') {
|
|
const rendered = await email.render();
|
|
|
|
res.send(rendered);
|
|
} else {
|
|
await email.send();
|
|
res.status(200).json({message: 'Sent'});
|
|
}
|
|
} catch (e) {
|
|
next(e);
|
|
}
|
|
});
|
|
};
|