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.refFk) throw new Error('The argument refFk is required'); const refFk = reqArgs.refFk; const sales = await db.rawSqlFromDef(`${sqlPath}/sales`, [refFk]); const content = toCSV(sales); const fileName = `invoice_${refFk}.csv`; response.setHeader('Content-type', 'text/csv'); response.setHeader('Content-Disposition', `inline; filename="${fileName}"`); response.end(content); } catch (error) { next(error); } };