2018-12-27 11:54:16 +00:00
|
|
|
const UserError = require('vn-loopback/util/user-error');
|
2018-08-02 07:49:00 +00:00
|
|
|
|
2018-05-17 11:29:18 +00:00
|
|
|
module.exports = Self => {
|
2018-08-21 05:39:01 +00:00
|
|
|
Self.remoteMethodCtx('changeState', {
|
2018-05-17 11:29:18 +00:00
|
|
|
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'}
|
|
|
|
}
|
|
|
|
],
|
2018-05-17 11:29:18 +00:00
|
|
|
returns: {
|
|
|
|
type: 'string',
|
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/changeState`,
|
2019-01-29 15:37:59 +00:00
|
|
|
verb: 'POST'
|
2018-05-17 11:29:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2019-03-05 11:01:53 +00:00
|
|
|
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
|
|
|
|
2019-03-05 11:01:53 +00:00
|
|
|
if (!params.stateFk && !params.code)
|
2019-02-01 16:11:14 +00:00
|
|
|
throw new UserError('State cannot be blank');
|
2018-05-17 11:29:18 +00:00
|
|
|
|
2019-03-05 11:01:53 +00:00
|
|
|
if (params.code) {
|
2019-06-05 05:41:20 +00:00
|
|
|
let state = await models.State.findOne({where: {code: params.code}, fields: ['id']});
|
2019-03-05 11:01:53 +00:00
|
|
|
params.stateFk = state.id;
|
|
|
|
}
|
|
|
|
|
2019-06-05 05:41:20 +00:00
|
|
|
let isProduction = await models.Account.hasRole(userId, 'production');
|
|
|
|
let isSalesPerson = await models.Account.hasRole(userId, 'salesPerson');
|
2019-02-01 16:11:14 +00:00
|
|
|
|
2019-06-05 05:41:20 +00:00
|
|
|
let ticket = await models.TicketState.findById(
|
2019-03-05 11:01:53 +00:00
|
|
|
params.ticketFk,
|
2019-02-01 16:11:14 +00:00
|
|
|
{fields: ['stateFk']}
|
|
|
|
);
|
|
|
|
|
2019-06-05 05:41:20 +00:00
|
|
|
let oldState = await models.State.findById(ticket.stateFk);
|
|
|
|
let newState = await models.State.findById(params.stateFk);
|
2019-02-01 16:11:14 +00:00
|
|
|
|
|
|
|
let isAllowed = isProduction || isSalesPerson
|
|
|
|
&& oldState.isEditable()
|
|
|
|
&& newState.isEditable();
|
2018-08-21 05:39:01 +00:00
|
|
|
|
2019-01-29 15:37:59 +00:00
|
|
|
if (!isAllowed)
|
|
|
|
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
|
2018-08-21 05:39:01 +00:00
|
|
|
|
2019-02-01 16:11:14 +00:00
|
|
|
if (newState.code != 'PICKER_DESIGNED') {
|
2019-06-05 05:41:20 +00:00
|
|
|
let worker = await models.Worker.findOne({where: {userFk: userId}});
|
2019-03-05 11:01:53 +00:00
|
|
|
params.workerFk = worker.id;
|
2019-01-22 09:04:42 +00:00
|
|
|
}
|
|
|
|
|
2019-06-05 05:41:20 +00:00
|
|
|
return await models.TicketTracking.create(params);
|
2018-05-17 11:29:18 +00:00
|
|
|
};
|
|
|
|
};
|