module.exports = function(TicketState) { TicketState.remoteMethodCtx('changeState', { description: 'Change state of tickets', accepts: [ { arg: 'state', type: 'number', required: true, description: 'New state', http: {source: 'path'} }, ], returns: { arg: 'response', type: 'boolean' }, http: { verb: 'put', path: '/:state/changeState' } }); TicketState.changeState = function(ctx, state, cb) { var tickets = ctx.req.body.tickets; TicketState.connectToService(ctx, "client"); TicketState.app.models.Employee.findOne({where: {userFk: ctx.req.accessToken.userId}}, function(err, emp){ if(!err) changeState(emp.id, tickets, state, cb); else cb(err, null); }); TicketState.disconnectFromService("client"); }; var changeState = function(emp, tickets, state, cb){ var inserts = []; var FakeProduction = TicketState.app.models.FakeProduction; tickets.forEach(function(t) { inserts.push({ticketFk: t, stateFk: state, employeeFk: emp}); }, this); TicketState.create(inserts, function(err, res){ if(err) cb(err, null) else{ FakeProduction.updateAll({ticketFk: {inq: tickets}}, {stateFk: state}, function(err, info){ (err) ? cb(err, null) : cb(null, info); }); } }); } }