const UserError = require('vn-loopback/util/user-error'); module.exports = Self => { Self.remoteMethodCtx('updateWorkerTimeControlMail', { description: 'Updates the state of WorkerTimeControlMail', accessType: 'WRITE', accepts: [{ arg: 'workerId', type: 'number', required: true }, { arg: 'year', type: 'number', required: true }, { arg: 'week', type: 'number', required: true }, { arg: 'state', type: 'string', required: true }, { arg: 'reason', type: 'string' }], returns: { type: 'boolean', root: true }, http: { path: `/updateWorkerTimeControlMail`, verb: 'POST' } }); Self.updateWorkerTimeControlMail = async(ctx, workerId, year, week, state, reason, options) => { const models = Self.app.models; const myOptions = {}; if (typeof options == 'object') Object.assign(myOptions, options); const workerTimeControlMail = await models.WorkerTimeControlMail.findOne({ where: { workerFk: workerId, year, week } }, myOptions); console.log('workerTimeControlMail: ', workerTimeControlMail); if (!workerTimeControlMail) throw new UserError(`There aren't records for this week`); await workerTimeControlMail.updateAttributes({ state, reason: reason || null }, myOptions); if (state == 'SENDED') { await workerTimeControlMail.updateAttributes({ sendedCounter: workerTimeControlMail.sendedCounter + 1 }, myOptions); } }; };