25 lines
806 B
JavaScript
25 lines
806 B
JavaScript
|
const path = require('path');
|
||
|
const db = require('vn-print/core/database');
|
||
|
|
||
|
const {toCSV} = require('../csv');
|
||
|
const sqlPath = path.join(__dirname, 'sql');
|
||
|
|
||
|
module.exports = async function(request, response, next) {
|
||
|
try {
|
||
|
const reqArgs = request.query;
|
||
|
if (!reqArgs.invoiceId)
|
||
|
throw new Error('The argument invoiceId is required');
|
||
|
|
||
|
const invoiceId = reqArgs.invoiceId;
|
||
|
const sales = await db.rawSqlFromDef(`${sqlPath}/sales`, [invoiceId]);
|
||
|
const content = toCSV(sales);
|
||
|
const fileName = `invoice_${invoiceId}.csv`;
|
||
|
|
||
|
response.setHeader('Content-type', 'text/csv');
|
||
|
response.setHeader('Content-Disposition', `inline; filename="${fileName}"`);
|
||
|
response.end(content);
|
||
|
} catch (error) {
|
||
|
next(error);
|
||
|
}
|
||
|
};
|