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

59 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',
2019-01-29 20:00:27 +00:00
accepts: [
{
arg: 'data',
description: 'Model instance data',
type: 'Object',
required: true,
http: {source: 'body'}
}
],
returns: {
type: 'string',
root: true
},
http: {
path: `/changeState`,
2019-01-29 15:37:59 +00:00
verb: 'POST'
}
});
2019-01-29 20:00:27 +00:00
Self.changeState = async(ctx, data) => {
2019-01-29 15:37:59 +00:00
let userId = ctx.req.accessToken.userId;
2019-02-01 16:11:14 +00:00
let $ = Self.app.models;
2019-01-29 15:37:59 +00:00
2019-02-01 16:11:14 +00:00
if (!data.stateFk)
throw new UserError('State cannot be blank');
2019-02-01 16:11:14 +00:00
let isProduction = await $.Account.hasRole(userId, 'production');
let isSalesPerson = await $.Account.hasRole(userId, 'salesPerson');
let ticket = await $.TicketState.findById(
data.ticketFk,
{fields: ['stateFk']}
);
let oldState = await $.State.findById(ticket.stateFk);
let newState = await $.State.findById(data.stateFk);
let isAllowed = isProduction || isSalesPerson
&& oldState.isEditable()
&& newState.isEditable();
2019-01-29 15:37:59 +00:00
if (!isAllowed)
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
2019-02-01 16:11:14 +00:00
if (newState.code != 'PICKER_DESIGNED') {
let worker = await $.Worker.findOne({where: {userFk: userId}});
2019-01-29 20:00:27 +00:00
data.workerFk = worker.id;
2019-01-22 09:04:42 +00:00
}
2019-02-01 16:11:14 +00:00
return await $.TicketTracking.create(data);
};
};