From 174dd1fe5c71753f5188f52f66a86e0124603926 Mon Sep 17 00:00:00 2001 From: joan Date: Tue, 15 Nov 2022 10:00:21 +0100 Subject: [PATCH] feat(invoice): send invoice from stored PDF - closes #4811 --- .../back/methods/invoiceOut/invoiceEmail.js | 25 +++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js b/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js index 83564e3abc..83cb848810 100644 --- a/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js +++ b/modules/invoiceOut/back/methods/invoiceOut/invoiceEmail.js @@ -40,19 +40,40 @@ module.exports = Self => { } }); - Self.invoiceEmail = async ctx => { + Self.invoiceEmail = async(ctx, reference) => { const args = Object.assign({}, ctx.args); + const {InvoiceOut} = Self.app.models; const params = { recipient: args.recipient, lang: ctx.req.getLocale() }; + const invoiceOut = await InvoiceOut.findOne({ + where: { + ref: reference + } + }); + delete args.ctx; for (const param in args) params[param] = args[param]; const email = new Email('invoice', params); - return email.send(); + const [stream, ...headers] = await Self.download(ctx, invoiceOut.id); + const name = headers[1]; + const fileName = name.replace(/filename="(.*)"/gm, '$1'); + + const mailOptions = { + overrideAttachments: true, + attachments: [ + { + filename: fileName, + content: stream + } + ] + }; + + return email.send(mailOptions); }; };