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

41 lines
1.2 KiB
JavaScript
Raw Normal View History

2019-08-08 06:33:27 +00:00
module.exports = Self => {
Self.remoteMethodCtx('editableStates', {
description: 'Gets the editable states according the user role ',
accessType: 'READ',
2020-03-26 11:00:07 +00:00
accepts: {
arg: 'filter',
type: 'object'
},
2019-08-08 06:33:27 +00:00
returns: {
type: ['Object'],
root: true
},
http: {
path: `/editableStates`,
verb: 'GET'
}
});
2020-03-26 11:00:07 +00:00
Self.editableStates = async(ctx, filter) => {
2019-08-08 06:33:27 +00:00
let userId = ctx.req.accessToken.userId;
let models = Self.app.models;
2020-03-26 11:00:07 +00:00
let statesList = await models.State.find({where: filter.where});
2019-08-08 06:33:27 +00:00
let isProduction = await models.Account.hasRole(userId, 'production');
let isSalesPerson = await models.Account.hasRole(userId, 'salesPerson');
let isAdministrative = await models.Account.hasRole(userId, 'administrative');
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);
};
};