2018-01-29 11:37:54 +00:00
|
|
|
module.exports = function(Self) {
|
|
|
|
Self.remoteMethodCtx('changeState', {
|
2017-06-13 11:57:40 +00:00
|
|
|
description: 'Change state of tickets',
|
|
|
|
accepts: [
|
|
|
|
{
|
|
|
|
arg: 'state',
|
|
|
|
type: 'number',
|
|
|
|
required: true,
|
|
|
|
description: 'New state',
|
|
|
|
http: {source: 'path'}
|
2018-01-29 11:37:54 +00:00
|
|
|
}
|
2017-06-13 11:57:40 +00:00
|
|
|
],
|
|
|
|
returns: {
|
2018-01-29 11:37:54 +00:00
|
|
|
arg: 'response',
|
2017-06-13 11:57:40 +00:00
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'put',
|
2017-06-20 11:06:13 +00:00
|
|
|
path: '/:state/changeState'
|
2017-06-13 11:57:40 +00:00
|
|
|
}
|
|
|
|
});
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
Self.changeState = function(ctx, state, cb) {
|
2017-06-20 11:06:13 +00:00
|
|
|
var tickets = ctx.req.body.tickets;
|
|
|
|
|
2018-08-01 12:01:42 +00:00
|
|
|
Self.connectToService(ctx, 'client');
|
2018-01-29 11:37:54 +00:00
|
|
|
|
|
|
|
Self.app.models.Worker.findOne({where: {userFk: ctx.req.accessToken.userId}}, function(err, emp) {
|
|
|
|
if (err)
|
2017-06-15 09:59:15 +00:00
|
|
|
cb(err, null);
|
2018-01-29 11:37:54 +00:00
|
|
|
else
|
|
|
|
changeState(emp.id, tickets, state, cb);
|
2017-06-14 06:54:45 +00:00
|
|
|
});
|
2017-06-15 09:02:50 +00:00
|
|
|
|
2018-08-01 12:01:42 +00:00
|
|
|
Self.disconnectFromService('client');
|
2017-06-13 11:57:40 +00:00
|
|
|
};
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
function changeState(emp, tickets, state, cb) {
|
|
|
|
var inserts = [];
|
|
|
|
var FakeProduction = Self.app.models.FakeProduction;
|
2017-06-13 11:57:40 +00:00
|
|
|
|
2017-06-14 06:54:45 +00:00
|
|
|
tickets.forEach(function(t) {
|
2018-01-29 11:37:54 +00:00
|
|
|
inserts.push({ticketFk: t, stateFk: state, workerFk: emp});
|
2017-06-14 06:54:45 +00:00
|
|
|
}, this);
|
|
|
|
|
2018-01-29 11:37:54 +00:00
|
|
|
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);
|
2017-06-28 06:57:33 +00:00
|
|
|
});
|
|
|
|
}
|
2017-06-14 06:54:45 +00:00
|
|
|
});
|
2017-06-13 11:57:40 +00:00
|
|
|
}
|
2018-01-29 11:37:54 +00:00
|
|
|
};
|