2023-04-04 12:09:55 +00:00
|
|
|
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;
|
|
|
|
|
|
|
|
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);
|
2023-04-04 12:09:55 +00:00
|
|
|
|
|
|
|
const body = $t('Added observation', {
|
|
|
|
user: user.name,
|
|
|
|
text: observation
|
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|