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; const url = await Self.app.models.Url.getUrl(); if (typeof options == 'object') Object.assign(myOptions, options); for (const defaulter of defaulters) { const user = await models.VnUser.findById(userId, {fields: ['name']}, myOptions); const body = $t('Added observation', { user: user.name, text: observation, defaulterId: defaulter.clientFk, defaulterUrl: `${url}client/${defaulter.clientFk}/summary` }); await models.Mail.create({ subject: $t('Comment added to client', {clientFk: defaulter.clientFk}), body: body, receiver: `${defaulter.departmentEmail}`, replyTo: `${user.name}@verdnatura.es` }, myOptions); } }; };