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

57 lines
1.5 KiB
JavaScript
Raw Normal View History

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'}
}
2017-06-13 11:57:40 +00:00
],
returns: {
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
}
});
Self.changeState = function(ctx, state, cb) {
2017-06-20 11:06:13 +00:00
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);
2017-06-14 06:54:45 +00:00
});
Self.disconnectFromService("client");
2017-06-13 11:57:40 +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) {
inserts.push({ticketFk: t, stateFk: state, workerFk: emp});
2017-06-14 06:54:45 +00:00
}, 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);
2017-06-28 06:57:33 +00:00
});
}
2017-06-14 06:54:45 +00:00
});
2017-06-13 11:57:40 +00:00
}
};