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

46 lines
1.5 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',
description: 'ticketFk, stateFk',
2019-01-29 15:37:59 +00:00
type: 'object',
required: true
}],
returns: {
type: 'string',
root: true
},
http: {
path: `/changeState`,
2019-01-29 15:37:59 +00:00
verb: 'POST'
}
});
Self.changeState = async(ctx, params) => {
2019-01-29 15:37:59 +00:00
let userId = ctx.req.accessToken.userId;
let models = Self.app.models;
2019-01-29 15:37:59 +00:00
let isEditable = await models.Ticket.isEditable(params.ticketFk);
let assignedState = await models.State.findOne({where: {code: 'PICKER_DESIGNED'}});
2019-01-22 09:04:42 +00:00
let isAssigned = assignedState.id === params.stateFk;
2019-01-29 15:37:59 +00:00
let isProduction = await models.Account.hasRole(userId, 'production');
let isSalesPerson = await models.Account.hasRole(userId, 'salesPerson');
let isAllowed = isProduction || isSalesPerson && isEditable && isAssigned;
2019-01-29 15:37:59 +00:00
if (!isAllowed)
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
2019-01-22 09:04:42 +00:00
if (!isAssigned) {
2019-01-29 15:37:59 +00:00
let worker = await models.Worker.findOne({where: {userFk: userId}});
2019-01-22 09:04:42 +00:00
params.workerFk = worker.id;
}
2019-01-29 15:37:59 +00:00
return await models.TicketTracking.create(params);
};
};