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;
|
2024-04-29 09:59:51 +00:00
|
|
|
const url = await Self.app.models.Url.getUrl();
|
2023-04-04 12:09:55 +00:00
|
|
|
|
|
|
|
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,
|
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`
|
2023-04-04 12:09:55 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
await models.Mail.create({
|
|
|
|
subject: $t('Comment added to client', {clientFk: defaulter.clientFk}),
|
|
|
|
body: body,
|
2024-10-20 13:59:24 +00:00
|
|
|
receiver: `${defaulter.departmentEmail}`,
|
2023-04-04 12:09:55 +00:00
|
|
|
replyTo: `${user.name}@verdnatura.es`
|
|
|
|
}, myOptions);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
};
|