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) { const user = await models.Account.findById(userId, {fields: ['name']}, myOptions); 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); } }; };