salix/back/methods/docuware/deliveryNoteEmail.js

79 lines
2.2 KiB
JavaScript

const {Email} = require('vn-print');
module.exports = Self => {
Self.remoteMethodCtx('deliveryNoteEmail', {
description: 'Sends the delivery note email with an docuware attached PDF',
accessType: 'WRITE',
accessScopes: ['docuwareDeliveryNoteEmail'],
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The ticket id',
},
{
arg: 'recipientId',
type: 'number',
description: 'The client id',
required: true
},
{
arg: 'recipient',
type: 'string',
description: 'The recipient email',
required: false,
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
path: '/delivery-note-email',
verb: 'POST'
}
});
Self.deliveryNoteEmail = async(ctx, id, recipientId, recipient) => {
const models = Self.app.models;
const args = Object.assign({}, ctx.args);
const params = {
recipient: args.recipient,
lang: ctx.req.getLocale()
};
delete args.ctx;
for (const param in args)
params[param] = args[param];
if (!recipient) {
client = await models.Client.findById(recipientId, {fields: ['email']});
params.recipient = client.email;
}
const email = new Email('delivery-note', params);
const docuwareFile = await models.Docuware.download(ctx, id, 'deliveryNote');
return email.send({
overrideAttachments: true,
attachments: [{
filename: `${id}.pdf`,
content: docuwareFile[0]
}]
});
};
};