salix/modules/ticket/back/methods/state/editableStates.js

44 lines
1.4 KiB
JavaScript

module.exports = Self => {
Self.remoteMethodCtx('editableStates', {
description: 'Gets the editable states according the user role ',
accessType: 'READ',
accepts: {
arg: 'filter',
type: 'object'
},
returns: {
type: ['object'],
root: true
},
http: {
path: `/editableStates`,
verb: 'GET'
}
});
Self.editableStates = async(ctx, filter, options) => {
const models = Self.app.models;
const userId = ctx.req.accessToken.userId;
const myOptions = {};
if (typeof options == 'object')
Object.assign(myOptions, options);
let statesList = await models.State.find({where: filter.where}, myOptions);
const isProduction = await models.Account.hasRole(userId, 'production', myOptions);
const isSalesPerson = await models.Account.hasRole(userId, 'salesPerson', myOptions);
const isAdministrative = await models.Account.hasRole(userId, 'administrative', myOptions);
if (isProduction || isAdministrative)
return statesList;
if (isSalesPerson) {
return statesList = statesList.filter(stateList =>
stateList.alertLevel === 0 || stateList.code === 'PICKER_DESIGNED'
);
}
return statesList.filter(stateList => stateList.alertLevel === 0);
};
};