salix/services/ticket/common/models/ticket-tracking.js

23 lines
1.0 KiB
JavaScript

module.exports = function(Self) {
require('../methods/ticket-tracking/filter')(Self);
Self.validatesPresenceOf('stateFk', {message: 'State cannot be blank'});
Self.observe('before save', async function(ctx) {
let models = Self.app.models;
let token = ctx.options.accessToken;
let userId = token && token.userId;
let isEmployee = await models.Account.hasRole(userId, 'employee');
let isProduction = await models.Account.hasRole(userId, 'production');
let isAlertLevel0 = await models.State.isAlertLevel0(ctx.instance.stateFk);
let ticketAlertLevel = await models.TicketState.findOne({where: {id: ctx.instance.ticketFk}, fields: ["alertLevel"]});
if ((!isProduction && !isAlertLevel0) || !isEmployee || (isEmployee && ticketAlertLevel != 0 && !isProduction))
throw new Error("You don't have enough privileges to do that");
let user = await models.Worker.findOne({where: {userFk: userId}});
ctx.instance.workerFk = user.id;
});
};