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

61 lines
1.8 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: {
2020-06-18 11:50:52 +00:00
type: 'Object',
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;
2019-06-05 05:41:20 +00:00
let models = Self.app.models;
2019-01-29 15:37:59 +00:00
if (!params.stateFk && !params.code)
2019-02-01 16:11:14 +00:00
throw new UserError('State cannot be blank');
if (params.code) {
2019-06-05 05:41:20 +00:00
let state = await models.State.findOne({where: {code: params.code}, fields: ['id']});
params.stateFk = state.id;
}
2019-08-19 05:56:20 +00:00
if (!params.workerFk) {
let worker = await models.Worker.findOne({where: {userFk: userId}});
params.workerFk = worker.id;
}
2019-02-01 16:11:14 +00:00
2020-09-22 05:50:30 +00:00
let ticketState = await models.TicketState.findById(
params.ticketFk,
2019-02-01 16:11:14 +00:00
{fields: ['stateFk']}
);
2020-09-22 05:50:30 +00:00
let oldStateAllowed;
if (ticketState)
oldStateAllowed = await models.State.isEditable(ctx, ticketState.stateFk);
2019-08-19 05:56:20 +00:00
let newStateAllowed = await models.State.isEditable(ctx, params.stateFk);
2019-02-01 16:11:14 +00:00
2020-09-22 05:50:30 +00:00
let isAllowed = (!ticketState || oldStateAllowed == true) && newStateAllowed == true;
2019-01-29 15:37:59 +00:00
if (!isAllowed)
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
return models.TicketTracking.create(params);
};
};