41 lines
1.2 KiB
JavaScript
41 lines
1.2 KiB
JavaScript
const path = require('path');
|
|
const db = require('vn-print/core/database');
|
|
const Email = require('vn-print/core/email');
|
|
|
|
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 invoice = await db.findOneFromDef(`${sqlPath}/invoice`, [refFk]);
|
|
const sales = await db.rawSqlFromDef(`${sqlPath}/sales`, [refFk]);
|
|
|
|
const args = Object.assign({
|
|
refFk: invoice.refFk,
|
|
recipientId: invoice.clientFk,
|
|
recipient: invoice.recipient,
|
|
replyTo: invoice.salesPersonEmail
|
|
}, response.locals);
|
|
|
|
const content = toCSV(sales);
|
|
const fileName = `invoice_${refFk}.csv`;
|
|
const email = new Email('invoice', args);
|
|
await email.send({
|
|
overrideAttachments: true,
|
|
attachments: [{
|
|
filename: fileName,
|
|
content: content
|
|
}]
|
|
});
|
|
|
|
response.status(200).json({message: 'Success'});
|
|
} catch (error) {
|
|
next(error);
|
|
}
|
|
};
|