2017-06-13 11:57:40 +00:00
|
|
|
module.exports = function(TicketState) {
|
2017-06-15 09:02:50 +00:00
|
|
|
TicketState.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'}
|
|
|
|
},
|
|
|
|
],
|
|
|
|
returns: {
|
|
|
|
arg: 'response',
|
|
|
|
type: 'boolean'
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
verb: 'put',
|
2017-06-20 11:06:13 +00:00
|
|
|
path: '/:state/changeState'
|
2017-06-13 11:57:40 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2017-06-20 11:06:13 +00:00
|
|
|
TicketState.changeState = function(ctx, state, cb) {
|
|
|
|
var tickets = ctx.req.body.tickets;
|
|
|
|
|
2017-06-15 09:02:50 +00:00
|
|
|
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);
|
2017-06-15 09:59:15 +00:00
|
|
|
else
|
|
|
|
cb(err, null);
|
2017-06-14 06:54:45 +00:00
|
|
|
});
|
2017-06-15 09:02:50 +00:00
|
|
|
|
|
|
|
TicketState.disconnectFromService("client");
|
2017-06-13 11:57:40 +00:00
|
|
|
};
|
|
|
|
|
2017-06-14 06:54:45 +00:00
|
|
|
var changeState = function(emp, tickets, state, cb){
|
|
|
|
var inserts = [];
|
2017-06-28 06:57:33 +00:00
|
|
|
var FakeProduction = TicketState.app.models.FakeProduction;
|
2017-06-13 11:57:40 +00:00
|
|
|
|
2017-06-14 06:54:45 +00:00
|
|
|
tickets.forEach(function(t) {
|
|
|
|
inserts.push({ticketFk: t, stateFk: state, employeeFk: emp});
|
|
|
|
}, this);
|
|
|
|
|
|
|
|
TicketState.create(inserts, function(err, res){
|
2017-06-28 06:57:33 +00:00
|
|
|
if(err)
|
|
|
|
cb(err, null)
|
|
|
|
else{
|
|
|
|
FakeProduction.updateAll({ticketFk: {inq: tickets}}, {stateFk: state}, function(err, info){
|
|
|
|
(err) ? cb(err, null) : cb(null, info);
|
|
|
|
});
|
|
|
|
}
|
2017-06-14 06:54:45 +00:00
|
|
|
});
|
2017-06-13 11:57:40 +00:00
|
|
|
}
|
|
|
|
}
|