-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathreport.js
35 lines (27 loc) · 1002 Bytes
/
report.js
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
const fs = require('fs');
const actions = require('../helpers/actions');
const reportUtils = require('../helpers/reports');
exports.protectedOptions = function (args, res) {
res.status(200).send();
};
exports.publicGet = async function (args, res) {
const reportType = args.swagger.params.type && args.swagger.params.type.value;
if (!reportType) {
return actions.sendResponse(res, 400, { error: 'Missing report type' });
}
try {
const reportPath = await reportUtils.generateReport(reportType);
fs.readFile(reportPath, (err, data) => {
if (err) {
return actions.sendResponse(res, 400, { error: 'Error generating report' });
}
// Delete the file.
fs.unlinkSync(reportPath);
res.setHeader('Content-Type', 'text/csv');
res.setHeader('Content-disposition', `attachment; filename=export_${reportType}.csv`);
res.send(data);
});
} catch (error) {
return actions.sendResponse(res, 400, { error: error.message });
}
};