const {Email} = require('vn-print'); module.exports = Self => { Self.remoteMethodCtx('deliveryNoteEmail', { description: 'Sends the delivery note email with an docuware attached PDF', accessType: 'WRITE', accepts: [ { arg: 'id', type: 'string', required: true, description: 'The ticket id', http: {source: 'path'} }, { arg: 'recipient', type: 'string', description: 'The recipient email', required: true, }, { arg: 'recipientId', type: 'number', description: 'The client id', 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: '/:id/delivery-note-email', verb: 'POST' } }); Self.deliveryNoteEmail = async(ctx, id) => { 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]; const email = new Email('delivery-note', params); const docuwareFile = await Self.app.models.Docuware.download(ctx, id, 'deliveryClient', 'findTicket'); return email.send({ overrideAttachments: true, attachments: [{ filename: `${id}.pdf`, content: docuwareFile[0] }] }); }; };