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