const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('driverRouteEmail', { description: 'Sends the driver route email with an attached PDF', accessType: 'WRITE', accepts: [ { arg: 'id', type: 'number', required: true, description: 'The client id', http: {source: 'path'} }, { arg: 'replyTo', type: 'string', description: 'The sender email to reply to', }, { arg: 'recipientId', type: 'number', description: 'The recipient id to send to the recipient preferred language', } ], returns: { type: ['object'], root: true }, http: { path: '/:id/driver-route-email', verb: 'POST' } }); Self.driverRouteEmail = async(ctx, id) => { const models = Self.app.models; const {workerFk, agencyMode} = await Self.findById(id, { fields: ['workerFk', 'agencyModeFk'], include: {relation: 'agencyMode'} }); const {reportMail} = agencyMode(); let user; let account; if (workerFk) { user = await models.VnUser.findById(workerFk, { fields: ['active', 'id'], include: {relation: 'emailUser'} }); account = await models.Account.findById(workerFk); } if (user?.active && account) ctx.args.recipient = user.emailUser().email; else ctx.args.recipient = reportMail; if (!ctx.args.recipient) throw new UserError('An email is necessary'); return Self.sendTemplate(ctx, 'driver-route'); }; };