salix/modules/ticket/back/methods/ticket-tracking/changeState.js

50 lines
1.7 KiB
JavaScript
Raw Normal View History

2018-12-27 11:54:16 +00:00
const UserError = require('vn-loopback/util/user-error');
module.exports = Self => {
Self.remoteMethodCtx('changeState', {
description: 'Change the state of a ticket',
accessType: 'WRITE',
accepts: [{
arg: 'params',
type: 'object',
required: true,
description: 'ticketFk, stateFk',
http: {source: 'body'}
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/changeState`,
verb: 'post'
}
});
Self.changeState = async(ctx, params) => {
let models = Self.app.models;
let isProduction;
let isEditable = await Self.app.models.Ticket.isEditable(params.ticketFk);
2019-01-22 09:04:42 +00:00
let assignedState = await Self.app.models.State.findOne({where: {code: 'PICKER_DESIGNED'}});
let isAssigned = assignedState.id === params.stateFk;
let currentUserId;
if (ctx.req.accessToken) {
let token = ctx.req.accessToken;
2019-01-22 09:04:42 +00:00
currentUserId = token && token.userId;
isProduction = await models.Account.hasRole(currentUserId, 'production');
isSalesperson = await models.Account.hasRole(currentUserId, 'salesPerson');
}
2019-01-22 09:04:42 +00:00
if ((!isEditable && !isProduction) || (isEditable && !isAssigned && isSalesperson) || (!isSalesperson && !isProduction))
throw new UserError(`You don't have enough privileges to change the state of this ticket`);
2019-01-22 09:04:42 +00:00
if (!isAssigned) {
let worker = await models.Worker.findOne({where: {userFk: currentUserId}});
params.workerFk = worker.id;
}
return await Self.app.models.TicketTracking.create(params);
};
};