const moment = require('moment'); module.exports = Self => { Self.remoteMethodCtx('resendWeeklyHourEmail', { description: 'Send the records for the week of the date provided', accessType: 'WRITE', accepts: [{ arg: 'id', type: 'number', description: 'The worker id', http: {source: 'path'} }, { arg: 'dated', type: 'date', required: true }], returns: [{ type: 'Object', root: true }], http: { path: `/:id/resendWeeklyHourEmail`, verb: 'POST' } }); Self.resendWeeklyHourEmail = async(ctx, workerId, dated, options) => { const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const yearNumber = dated.getFullYear(); const weekNumber = moment(dated).isoWeek(); const workerTimeControlMail = await models.WorkerTimeControlMail.findOne({ where: { workerFk: workerId, year: yearNumber, week: weekNumber } }, myOptions); if (workerTimeControlMail && workerTimeControlMail.state != 'SENDED') { const worker = await models.EmailUser.findById(workerId, null, myOptions); ctx.args = { recipient: worker.email, year: yearNumber, week: weekNumber, workerId: workerId, state: 'SENDED' }; return models.WorkerTimeControl.weeklyHourRecordEmail(ctx, myOptions); } return false; }; };