salix/services/loopback/common/methods/ticket-state/changeState.js

57 lines
1.5 KiB
JavaScript

module.exports = function(Self) {
Self.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'
}
});
Self.changeState = function(ctx, state, cb) {
var tickets = ctx.req.body.tickets;
Self.connectToService(ctx, 'client');
Self.app.models.Worker.findOne({where: {userFk: ctx.req.accessToken.userId}}, function(err, emp) {
if (err)
cb(err, null);
else
changeState(emp.id, tickets, state, cb);
});
Self.disconnectFromService('client');
};
function changeState(emp, tickets, state, cb) {
var inserts = [];
var FakeProduction = Self.app.models.FakeProduction;
tickets.forEach(function(t) {
inserts.push({ticketFk: t, stateFk: state, workerFk: emp});
}, this);
Self.create(inserts, function(err, res) {
if (err)
cb(err, null);
else {
FakeProduction.updateAll({ticketFk: {inq: tickets}}, {stateFk: state}, function(err, info) {
cb(err, info);
});
}
});
}
};