2018-09-27 07:50:31 +00:00
|
|
|
const UserError = require('../../helpers').UserError;
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-05-17 11:29:18 +00:00
|
|
|
module.exports = Self => {
|
2018-08-21 05:39:01 +00:00
|
|
|
Self.remoteMethodCtx('changeState', {
|
2018-05-17 11:29:18 +00:00
|
|
|
description: 'Change the state of a ticket',
|
|
|
|
accessType: 'WRITE',
|
|
|
|
accepts: [{
|
|
|
|
arg: 'params',
|
|
|
|
type: 'object',
|
|
|
|
required: true,
|
2018-08-21 09:27:42 +00:00
|
|
|
description: 'ticketFk, stateFk',
|
2018-05-17 11:29:18 +00:00
|
|
|
http: {source: 'body'}
|
|
|
|
}],
|
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/changeState`,
|
|
|
|
verb: 'post'
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-08-21 05:39:01 +00:00
|
|
|
Self.changeState = async(ctx, params) => {
|
|
|
|
let models = Self.app.models;
|
|
|
|
let isProduction;
|
2018-05-17 11:29:18 +00:00
|
|
|
let isEditable = await Self.app.models.Ticket.isEditable(params.ticketFk);
|
|
|
|
|
2018-08-21 05:39:01 +00:00
|
|
|
if (ctx.req.accessToken) {
|
|
|
|
let token = ctx.req.accessToken;
|
|
|
|
let currentUserId = token && token.userId;
|
|
|
|
isProduction = await models.Account.hasRole(currentUserId, 'Production');
|
|
|
|
let worker = await models.Worker.findOne({where: {userFk: currentUserId}});
|
|
|
|
params.workerFk = worker.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!isEditable && !isProduction)
|
|
|
|
throw new UserError(`You don't have enough privileges to change the state of this ticket`);
|
|
|
|
|
|
|
|
return await Self.app.models.TicketTracking.create(params);
|
2018-05-17 11:29:18 +00:00
|
|
|
};
|
|
|
|
};
|