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: {
|
2020-06-18 11:50:52 +00:00
|
|
|
type: 'Object',
|
2018-05-17 11:29:18 +00:00
|
|
|
root: true
|
|
|
|
},
|
|
|
|
http: {
|
|
|
|
path: `/changeState`,
|
2019-01-29 15:37:59 +00:00
|
|
|
verb: 'POST'
|
2018-05-17 11:29:18 +00:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
Self.changeState = async(ctx, params, options) => {
|
|
|
|
const models = Self.app.models;
|
|
|
|
const myOptions = {};
|
|
|
|
let tx;
|
2019-01-29 15:37:59 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
if (typeof options == 'object')
|
|
|
|
Object.assign(myOptions, options);
|
2018-05-17 11:29:18 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
if (!myOptions.transaction) {
|
|
|
|
tx = await Self.beginTransaction({});
|
|
|
|
myOptions.transaction = tx;
|
2019-03-05 11:01:53 +00:00
|
|
|
}
|
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
try {
|
|
|
|
const userId = ctx.req.accessToken.userId;
|
|
|
|
|
|
|
|
if (!params.stateFk && !params.code)
|
|
|
|
throw new UserError('State cannot be blank');
|
|
|
|
|
|
|
|
if (params.code) {
|
|
|
|
const state = await models.State.findOne({
|
|
|
|
where: {code: params.code},
|
|
|
|
fields: ['id']
|
|
|
|
}, myOptions);
|
2019-02-01 16:11:14 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
params.stateFk = state.id;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!params.workerFk) {
|
|
|
|
const worker = await models.Worker.findOne({
|
|
|
|
where: {userFk: userId}
|
|
|
|
}, myOptions);
|
2019-02-01 16:11:14 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
params.workerFk = worker.id;
|
|
|
|
}
|
2019-02-01 16:11:14 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
const ticketState = await models.TicketState.findById(params.ticketFk, {
|
|
|
|
fields: ['stateFk']
|
|
|
|
}, myOptions);
|
2018-08-21 05:39:01 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
let oldStateAllowed;
|
|
|
|
if (ticketState)
|
|
|
|
oldStateAllowed = await models.State.isEditable(ctx, ticketState.stateFk, myOptions);
|
|
|
|
const newStateAllowed = await models.State.isEditable(ctx, params.stateFk, myOptions);
|
2018-08-21 05:39:01 +00:00
|
|
|
|
2021-09-21 16:48:59 +00:00
|
|
|
const isAllowed = (!ticketState || oldStateAllowed == true) && newStateAllowed == true;
|
|
|
|
|
|
|
|
if (!isAllowed)
|
|
|
|
throw new UserError(`You don't have enough privileges`, 'ACCESS_DENIED');
|
|
|
|
|
|
|
|
const ticketTracking = await models.TicketTracking.create(params, myOptions);
|
|
|
|
|
|
|
|
if (tx) await tx.commit();
|
|
|
|
|
|
|
|
return ticketTracking;
|
|
|
|
} catch (e) {
|
|
|
|
if (tx) await tx.rollback();
|
|
|
|
throw e;
|
|
|
|
}
|
2018-05-17 11:29:18 +00:00
|
|
|
};
|
|
|
|
};
|