salix/back/methods/docuware/deliveryNoteEmail.js

79 lines
2.2 KiB
JavaScript
Raw Normal View History

2023-01-09 14:11:53 +00:00
const {Email} = require('vn-print');
module.exports = Self => {
Self.remoteMethodCtx('deliveryNoteEmail', {
description: 'Sends the delivery note email with an docuware attached PDF',
accessType: 'WRITE',
2023-05-18 12:12:46 +00:00
accessScopes: ['docuwareDeliveryNoteEmail'],
2023-01-09 14:11:53 +00:00
accepts: [
{
arg: 'id',
2023-05-22 12:45:00 +00:00
type: 'number',
2023-01-09 14:11:53 +00:00
required: true,
description: 'The ticket id',
},
{
arg: 'recipientId',
type: 'number',
description: 'The client id',
2023-05-18 12:12:46 +00:00
required: true
},
{
arg: 'recipient',
type: 'string',
description: 'The recipient email',
required: false,
2023-01-09 14:11:53 +00:00
}
],
returns: [
{
arg: 'body',
type: 'file',
root: true
}, {
arg: 'Content-Type',
type: 'String',
http: {target: 'header'}
}, {
arg: 'Content-Disposition',
type: 'String',
http: {target: 'header'}
}
],
http: {
2023-05-18 12:12:46 +00:00
path: '/delivery-note-email',
2023-01-09 14:11:53 +00:00
verb: 'POST'
}
});
2023-05-18 12:12:46 +00:00
Self.deliveryNoteEmail = async(ctx, id, recipientId, recipient) => {
const models = Self.app.models;
2023-01-09 14:11:53 +00:00
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];
2023-05-23 05:39:43 +00:00
if (!recipient) {
client = await models.Client.findById(recipientId, {fields: ['email']});
params.recipient = client.email;
}
2023-05-18 12:12:46 +00:00
2023-01-09 14:11:53 +00:00
const email = new Email('delivery-note', params);
2023-05-18 12:12:46 +00:00
const docuwareFile = await models.Docuware.download(ctx, id, 'deliveryNote');
2023-01-09 14:11:53 +00:00
return email.send({
overrideAttachments: true,
attachments: [{
filename: `${id}.pdf`,
content: docuwareFile[0]
}]
});
};
};