salix/services/ticket/common/methods/ticket-tracking/changeState.js

43 lines
1.3 KiB
JavaScript
Raw Normal View History

const UserError = require('vn-loopback/common/helpers').UserError;
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);
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);
};
};