salix/services/production/common/methods/ticket-state/change-state.js

57 lines
1.5 KiB
JavaScript
Raw Normal View History

2017-06-13 11:57:40 +00:00
module.exports = function(TicketState) {
TicketState.remoteMethodCtx('changeState', {
2017-06-13 11:57:40 +00:00
description: 'Change state of tickets',
accepts: [
{
arg: 'tickets',
type: 'array',
required: true,
description: 'Array of tickets',
http: {source: 'path'}
},
{
arg: 'state',
type: 'number',
required: true,
description: 'New state',
http: {source: 'path'}
},
],
returns: {
arg: 'response',
type: 'boolean'
},
http: {
verb: 'put',
path: '/:tickets/:state/changeState'
}
});
TicketState.changeState = function(ctx, tickets, state, cb) {
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);
2017-06-14 06:54:45 +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-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){
if(!err)
cb(null, res);
});
2017-06-13 11:57:40 +00:00
}
2017-06-14 12:27:16 +00:00
2017-06-13 11:57:40 +00:00
}