salix/modules/route/back/methods/route/driverRouteEmail.js

58 lines
1.8 KiB
JavaScript
Raw Normal View History

2024-02-09 12:04:27 +00:00
const UserError = require('vn-loopback/util/user-error');
2022-09-26 06:07:45 +00:00
module.exports = Self => {
Self.remoteMethodCtx('driverRouteEmail', {
description: 'Sends the driver route email with an attached PDF',
2022-10-09 18:14:14 +00:00
accessType: 'WRITE',
2022-09-26 06:07:45 +00:00
accepts: [
{
arg: 'id',
type: 'number',
required: true,
description: 'The client id',
http: {source: 'path'}
2024-02-09 11:12:10 +00:00
}, {
2022-09-26 06:07:45 +00:00
arg: 'replyTo',
type: 'string',
description: 'The sender email to reply to',
2024-02-09 11:12:10 +00:00
}, {
2022-09-26 06:07:45 +00:00
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'
}
});
2024-02-09 11:12:10 +00:00
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);
}
2024-10-03 16:20:21 +00:00
if (user?.active && account) ctx.args.recipient = user.emailUser().email;
else ctx.args.recipient = reportMail;
2024-02-09 11:12:10 +00:00
2024-10-03 16:20:21 +00:00
if (!ctx.args.recipient) throw new UserError('An email is necessary');
2024-02-12 07:34:25 +00:00
return Self.sendTemplate(ctx, 'driver-route');
2024-02-09 11:12:10 +00:00
};
2022-09-26 06:07:45 +00:00
};