salix/modules/client/back/methods/defaulter/observationEmail.js

56 lines
1.8 KiB
JavaScript
Raw Normal View History

module.exports = Self => {
Self.remoteMethodCtx('observationEmail', {
description: 'Send an email with the observation',
accessType: 'WRITE',
accepts: [
{
arg: 'defaulters',
type: ['object'],
required: true,
description: 'The defaulters to send the email'
},
{
arg: 'observation',
type: 'string',
required: true,
description: 'The observation'
}],
returns: {
arg: 'observationEmail'
},
http: {
path: `/observationEmail`,
verb: 'POST'
}
});
Self.observationEmail = async(ctx, defaulters, observation, options) => {
const models = Self.app.models;
const $t = ctx.req.__; // $translate
const myOptions = {};
const userId = ctx.req.accessToken.userId;
2024-04-29 09:59:51 +00:00
const url = await Self.app.models.Url.getUrl();
if (typeof options == 'object')
Object.assign(myOptions, options);
for (const defaulter of defaulters) {
2023-04-18 07:49:23 +00:00
const user = await models.VnUser.findById(userId, {fields: ['name']}, myOptions);
const body = $t('Added observation', {
user: user.name,
2024-04-29 09:59:51 +00:00
text: observation,
2024-05-02 10:42:17 +00:00
defaulterId: defaulter.clientFk,
2024-04-29 09:59:51 +00:00
defaulterUrl: `${url}client/${defaulter.clientFk}/summary`
});
await models.Mail.create({
subject: $t('Comment added to client', {clientFk: defaulter.clientFk}),
body: body,
receiver: `${defaulter.salesPersonName}@verdnatura.es`,
replyTo: `${user.name}@verdnatura.es`
}, myOptions);
}
};
};